java syntax primitive data types operators control statements
Post on 15-Jan-2016
240 views
TRANSCRIPT
Java Syntax
Primitive data types
Operators
Control statements
Primitive data types
• Identical across all computer platforms portable
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
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)
Operators
• Additive+ -
• Multiplicative* / %
• Equality== !=
• Assignment operators= += -= *= /= %=
Operators
• Relational operators< <= > >=
• Increment operators (postfix and prefix)
++ --
• Conditional operator?:
• String concatenation +
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)
Control Statements
• Similar to C/C++ syntax:– if statement
if (x != n){ …}else if {
… }else {
… }
– for statement for (int i=0; i<max; i++){
… };
Control Statements
– while statement while (x==n ){
…};
– do statement do {…} while( x<=y && x!=0);
Control Statements
– switch statement switch (n){ case 1:
… break;case 2: case 3:
… break;default: break;
};
General Points to Note
• Case sensitive
• Use lower case
– objects have capitalised first letter
Java Reference Types
Classes
Arrays
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”
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
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)
null
• null
– is a special value indicating a reference to nothing
– can be assigned to a variable of any reference type
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];
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.
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)
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
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;}
}
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
... } }
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