java syntax primitive data types operators control statements

23
Java Syntax Primitive data types Operators Control statements

Post on 15-Jan-2016

240 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java Syntax Primitive data types Operators Control statements

Java Syntax

Primitive data types

Operators

Control statements

Page 2: Java Syntax Primitive data types Operators Control statements

Primitive data types

• Identical across all computer platforms portable

Page 3: Java Syntax Primitive data types Operators Control statements

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

Page 4: Java Syntax Primitive data types Operators Control statements

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 typese.g. while (i!=0) not while(i)

Page 5: Java Syntax Primitive data types Operators Control statements

Operators

• Additive+ -

• Multiplicative* / %

• Equality== !=

• Assignment operators= += -= *= /= %=

Page 6: Java Syntax Primitive data types Operators Control statements

Operators

• Relational operators< <= > >=

• Increment operators (postfix and prefix)

++ --

• Conditional operator?:

• String concatenation +

Page 7: Java Syntax Primitive data types Operators Control statements

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)

Page 8: Java Syntax Primitive data types Operators Control statements

Control Statements

• Similar to C/C++ syntax:– if statement

if (x != n){ …}else if {

… }else {

… }

– for statement for (int i=0; i<max; i++){

… };

Page 9: Java Syntax Primitive data types Operators Control statements

Control Statements

– while statement while (x==n ){

…};

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

Page 10: Java Syntax Primitive data types Operators Control statements

Control Statements

– switch statement switch (n){ case 1:

… break;case 2: case 3:

… break;default: break;

};

Page 11: Java Syntax Primitive data types Operators Control statements

General Points to Note

• Case sensitive

• Use lower case

– objects have capitalised first letter

Page 12: Java Syntax Primitive data types Operators Control statements

Java Reference Types

Classes

Arrays

Page 13: Java Syntax Primitive data types Operators Control statements

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”

Page 14: Java Syntax Primitive data types Operators Control statements

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

Page 15: Java Syntax Primitive data types Operators Control statements

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)

Page 16: Java Syntax Primitive data types Operators Control statements

null

• null

– is a special value indicating a reference to nothing

– can be assigned to a variable of any reference type

Page 17: Java Syntax Primitive data types Operators Control statements

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];

Page 18: Java Syntax Primitive data types Operators Control statements

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:

e.g. int 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.

Page 19: Java Syntax Primitive data types Operators Control statements

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)

Page 20: Java Syntax Primitive data types Operators Control statements

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

Page 21: Java Syntax Primitive data types Operators Control statements

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;}

}

Page 22: Java Syntax Primitive data types Operators Control statements

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

... } }

Page 23: Java Syntax Primitive data types Operators Control statements

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 // truenull instanceOf String // false