Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Syntax Primitive data types Operators Control statements.

Similar presentations

Presentation on theme: "Java Syntax Primitive data types Operators Control statements."— Presentation transcript:

1 Java Syntax Primitive data types Operators Control statements

2 Primitive data types Identical across all computer platforms  portable

3 Primitive data types char (16 bits) a Unicode character byte (8 bits) int (32 bits) a signed integer short (16 bits) a short integer long (64 bits) a long integer

4 Primitive data types float (32 bits) a real number double (64 bits) a large real number boolean (8 bits) –values are true or false (keywords) –cannot be converted to and from other data types e.g. while (i!=0) not while(i)

5 Operators Additive + - Multiplicative * / % Equality == != Assignment operators = += -= *= /= %=

6 Operators Relational operators >= Increment operators (postfix and prefix) ++ -- Conditional operator ?: String concatenation +

7 Logical Operators Not ! Logical AND && Logical OR || Boolean logical AND& Boolean logical inclusive OR| Boolean logical exclusive OR^ (true if only one operand is true)

8 Control Statements Similar to C/C++ syntax: –if statement if (x != n){ … } else if { … } else { … } –for statement for (int i=0; i<max; i++){ … };

9 Control Statements –while statement while (x==n ){ … }; –do statement do { … } while( x<=y && x!=0);

10 Control Statements –switch statement switch (n){ case 1: … break; case 2: case 3: … break; default: break; };

11 General Points to Note Case sensitive Use lower case –objects have capitalised first letter

12 Java Reference Types Classes Arrays

13 Reference Types Classes and arrays are composite types –no standard size –contain other elements Manipulated “by reference’’ to the object or array Primitive data types manipulated “by value”

14 Reference vs Primitive Types A reference is a value that refers to the object or array A primitive datatype holds the value directly Difference to primitive types effects the way values are copied and compared Setting Object A = Object B only sets the reference and does not set the contents Comparing Object A and Object B, A will not be equal to B even if they have the same contents

15 References in Java Note: –Java does not support the & address-of or -> and * de-reference operators of C and C++ –the. operator in Java is more like the -> operator of C++ –references in Java cannot be manipulated (e.g. incremented or decremented)

16 null –is a special value indicating a reference to nothing –can be assigned to a variable of any reference type

17 Arrays in Java Array declaration: type arrayId[] = new type[limit]; type arrayId[] = new type[] {values}; Multi dimensional array: type arrayId[][] =new type[rowlimit][colLimit] Examples: int frequencies[]= new int[20]; String countryCode[]= new String[176]; double table[]=new double[4][5]; Times timePeriods[]=new Times[8];

18 Arrays in Java Arrays can be formed from any data type or class Arrays are indexed from 0. Arrays are fixed size but the size can be allocated at run time: array1[];// declare array … … int size=n;// get array size, array1 = new int [size];// allocate array Assigning one array to another array copies the reference and does not copy full array.

19 Arrays in Java Accessing an array element that does not exist will result in an error The length of an array can be accessed using a read-only property called length that is associated with every array. e.g. for (i=0; i<frequencies.length; i++)… Arrays can be passed as parameters to methods e.g. main (String [] args)

20 Class & Array Interaction There are 3 ways that classes and arrays can interact (1) An array of a class of objects: Student students[] =new Student[8]; (2) A class containing an array and methods that act on it (3) A class containing methods that operate on array parameters

21 Class & Array Interaction (2) class IntArray { private int arrayOfInt[]; IntArray(int size){ //constructor arrayOfInt=new int [size]; } int get(int index){ // get value at index return arrayOfInt[index]; } void set (int index, int value){ //set value arrayOfInt[index]=value; } public String toString(){ // print out array String s =””; for (int i=0; i<arrayOfInt.length;i++) s += “ “+arrayOfInt[i]; return s; } }

22 Class & Array Interaction (3) class ArrayUtilities { static int max( int arrayA [] ) { // finds the max element of arrayA // and returns it... return arrayA[i]; } static void sort (int [] arrayA) { //sorts the elements of arrayA... } }

23 instanceOf operator Used only with arrays and objects, not primitive types value instanceOf referenceType –returns true if the object or array on left is an instance of the type on the right –returns false otherwise –Used for checking types before casting (see later - working with inheritance) “My string” instanceOf String // true “” instanceOf string // true null instanceOf String// false

Download ppt "Java Syntax Primitive data types Operators Control statements."

Similar presentations

Ads by Google