CSE 110: Programming Language I Matin Saad Abdullah UB 1222.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Constants and Data Types Constants Data Types Reading for this class: L&L,
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Declaring Variables You must first declare a variable before you can use it! Declaring involves: – Establishing the variable’s spot in memory – Specifying.
Data types and variables
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Chapter 2 Data Types, Declarations, and Displays
Hello, world! Dissect HelloWorld.java Compile it Run it.
String Escape Sequences
L EC. 02: D ATA T YPES AND O PERATORS (1/2) Fall Java Programming.
Objectives You should be able to describe: Data Types
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Chapter 2: Introducing Data Types and Operators.  Know Java’s primitive types  Use literals  Initialize variables  Know the scope rules of variables.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
System development with Java Lecture 2. Rina Errors A program can have three types of errors: Syntax and semantic errors – called.
C Tokens Identifiers Keywords Constants Operators Special symbols.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CPS120: Introduction to Computer Science
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
 Character set is a set of valid characters that a language can recognise.  A character represents any letter, digit or any other sign  Java uses the.
August 6, 2009 Data Types, Variables, and Arrays.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
Chapter 2 Variables.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
CSI 3125, Preliminaries, page 1 Data Type, Variables.
CPS120: Introduction to Computer Science Variables and Constants.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Review by Mr. Maasz, Summary of Chapter 2: Starting Out with Java.
1.  Algorithm: 1. Read in the radius 2. Compute the area using the following formula: area = radius x radius x PI 3. Display the area 2.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 2 Variables.
Chapter 2 Basic Computation
Java Variables and Types
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Sachin Malhotra Saurabh Choudhary
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Object Oriented Programming
Multiple variables can be created in one declaration
Variables and Primative Types
Type Conversion, Constants, and the String Object
Chapter 2.
IDENTIFIERS CSC 111.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Variables.
Chapter 2: Java Fundamentals
Expressions and Assignment
elementary programming
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Java Basics Data Types in Java.
In this class, we will cover:
Primitive Types and Expressions
Chapter 2 Variables.
Presentation transcript:

CSE 110: Programming Language I Matin Saad Abdullah UB 1222

Comments Java programs can have three kinds of comments:  Single-Line Comments  //  Block Comments  General: /* */  Documentation Comments: /** */

Case Sensitive It simply means that the case of the letters in your Java programs matter. Java sees all of the following as different things:  Level  level  LeveL  level

Keywords In the Java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language.

Identifiers Identifiers are names Identifier may be any sequence of upper & lower case letters, numbers, or ‘_’, ‘$’  Cannot be Keywords  They must not begin with a number Valid AcoountBalance index str1 $Test hello_world Invalid 2stryes/no low-high

Naming Conventions ThisIsSomeClass thisIsSomeMethod() thisIsSomeVariable some_variable  preferred by some for private/local variables, NOT recommended CONSTANT_SOMETHING $someAutomaticallyGeneratedName

Literals A constant value in Java is created by using a literal representation of it.  These are strings of symbols that represent “literal” data values. Integer Literals  Like, 123 is an integer literal  These are all decimal value (Base 10)  Other two bases can be used – octal, hexadecimal  Octal values are denoted by a leading ‘zero’, like – 0123  Hex values are denoted by leading ‘0x’ with numbers in the range of 0-15, [A through F are substituted for 10 to 15]  Long literal are denoted by appending upper or lowercase ‘L’.

Literals Floating point literals  expressed either in standard or scientific notation.  Standard notation consists of decimal and fractional parts  e.g. 12.0,  Scientific notation uses decimal, fractional number plus a suffix (power of 10)  e.g E12, 12334e-5 Floating point literals in Java default to double precision  To specify, a float literal, append a ‘F’ or ‘f’ to the constant.

Literals Boolean Literals  only two logical values – true, false  Difference with C, ‘true’ is not equal to ‘1’ and ‘false’ is not ‘0’ Character Literals  includes Unicode character set  represented inside a pair of single quote, like ‘d’, String Literals  Enclosed between a pair of double quotes  "123" is a String literal as is "class" but class is a keyword and Class is an identifier

Data types and variables Data types - simple to complex  int - for integers or whole numbers  double - for numbers with fractional parts  String - for text  Button - a button on a GUI  Point - for representing points in a plane Variables store data in named locations  every variable must have a declared type

Primitive types Java has eight primitive types:  byte, short, int, long, float, double, char, boolean Primitive types have literal values and can be manipulated with built-in operators. E.g

Eight Simple Data Types byte, short, char, int, long, float, and double char is 16 bit unsigned –  Unicode, not ASCII (range )  fully international character set, represent all of the characters found in all human languages example: char ch = ‘X’ ;

Integer Types integer(no unsigned types)  8-bit byte  16-bit short  32-bit int  64-bit long

Ranges Name Width Range byte to 127 short ,768 to 32,767 float e-038 to 3.4e+038 double e-308 to 1.7e+308

Floating - Point Types Floating Point type float 32-bit 3.4 e-038 to 3.4 e+038 double 64-bit 1.7 e-308 to 1.7 e+308

Boolean Types standard type for only logical values has values true and false no conversion between ‘int’ and ‘boolean’

Declaring Variables All variables must be declared before can be used type identifier [= value] ; int count, total; String sentence; boolean done;

Initializing Variables Initializing is mandatory before accessing a variable int count = 10, total = 0; String sentence = "Hello there."; boolean done = false; Floating point literals in Java default to double

Initial Java Program 1.public class Test1 { 2. public static void main(String[] args) { 3. int x; 4. int y; 5. x = 2; 6. y = 3; 7. System.out.println (x + y); 8. } 9.}

1.// StringVsId.java 2.// contrast strings and identifiers 3.class StringVsId { 4. public static void main(String[] args) { 5. String hello = "Hello, world!"; 6. String stringVary; 7. stringVary = hello; 8. System.out.println(stringVary); 9. stringVary = "hello"; 10. System.out.println(stringVary); 11. } 12.} String AIRPO RT GULSH AN hellostringVa ry “Hello World” “hello” AIRPO RT GULSH AN

Scope of variables Java allows variables to be declared within a block A block defines Scope  Each time stating a new block, new scope created Generally two types of scopes  defined by ‘classes’ & ‘methods’ Variables declared inside a scope are not visible outside. Scope can be nested.

Scope of variables (cont..) 1.public class Scope { 2. public static void main(String[] args) { 3. int x; 4. x = 10; 5. { 6. int y = 20; // known only in this block 7. System.out.println (x + y); 8. } 9. y = 100; // ERROR !! y not known here 10. x = 200; // x is still known here 11. } 12.}

Scope of variables (cont..) public class Scope2 { public static void main(String[] args) { int x = 0; { int y = 100; System.out.println (x + y); y = 10; int y = 200; // Compile Error- already defined } int y ; // creates new variables ‘y’ }

Type Conversion Java’s automatic conversion occurs when -  Two types are compatible  e.g numeric types(int, float) are not compatible with ‘char’ or ‘boolean’  ‘char’ and ‘boolean’ are not compatible with each other.  Destination type as larger than source type  e.g. ‘int’ type is always large enough to hold ‘byte’ type This type of conversion is ‘widening conversion’

Incompatible Type Casting If it necessary to assign an ‘int’ to a ‘byte’  This conversion will not perform automatically, as ‘byte’ is smaller than ‘int’  This type of conversion is called ‘narrowing conversion’ A cast has general form (target-type) value

Type casting (example) byte b; int i = 257;// i = 257 ( ) 2 double d = ;// 323 ( ) 2 b = (byte) i; System.out.println(b);// b = 1 i = (int) d; System.out.println(i);// i = 323 b = (byte) d; System.out.println(b);// b = 67 ( ) 2