Ref : The complete Reference Java 4th Edition
Ms.Nagasundari Department of ISE, PESIT 1
Data types and other tokens:
Snippets to demonstrate
Boolean variables
Int, long, char
Arrays
Literals, assigning values
Creating and destroying objects
Access specifiers
Ms.Nagasundari Department of ISE, PESIT 2
Boolean Variables
class booltest
{
public static void main(String args[])
{
boolean b=true;
if(b==true) system.out.println(“b is ” + b);
b=false;
system.out.println(“b is ” + b);
}
}
Ms.Nagasundari Department of ISE, PESIT 3
Int, long, char
class datatypes
{
public static void main(String args[])
{
int a=2,b=1;
char ch=‘a’;
long x=5;
system.out.println(“ch is ” + b);
system.out.println(“a+b is ” + (a+b))
system.out.println(“volume is ” + (x*x*x));
}
}
Ms.Nagasundari Department of ISE, PESIT 4
Arrays
class arrays
{
public static void main(String args[])
{
int num[]={1,5,8,9,3,7,3};
int sum=0;
for(i=0;i<7;i++)
sum = sum + num[i];
system.out.println(“Sum is ” + sum);
}
}
Ms.Nagasundari Department of ISE, PESIT 5
Arrays (cont…)
class arrays
{
public static void main(String args[])
{
int num [][] = { {1,5,6,7},
{8,9,7,9},
{3,7,3,5},
{9,5,1,2}
};
int sum=0;
for(i=0;i<4,i++)
for(j=0;j<4;j++)
sum = sum + num[i][j];
system.out.println(“Sum is ” + sum);
}
}
Ms.Nagasundari Department of ISE, PESIT 6
Character Arrays (Strings)
class str_arr
{
public static void main(String args[])
{
string str=“Hello World”;
system.out.println(str);
}
}
Ms.Nagasundari Department of ISE, PESIT 7
Literals
class literals_demo
{
public static void main(String args[])
{
long a=0xffff; // 0x denotes hexadecimal
double k=55.09F; // F denotes float
double j=102E50; // E denotes exponent
boolean a=true,b=false; //True = 1, False = 0
string str = “Hello \n World”; // \n is for new line
}
}
Ms.Nagasundari Department of ISE, PESIT 8
Creating and destroying object
class objects
{
public static void main(String args[])
{ //new is used to create objects
int a[] = new int[10];
char b[][] = new char[4][2];
//delete is used to delete objects
delete [] a;
delete [][] b;
}
}
Ms.Nagasundari Department of ISE, PESIT 9
Access Specifiers
class access_spec
{
private:
int a;
char b;
protected:
double x;
public:
int add(int,int);
};
Ms.Nagasundari Department of ISE, PESIT 10
Operators and Expressions
Snippets to demonstrate:
Arithmetic Operators
Bitwise operators
Relational operators
The Assignment Operator
The ? Operator
Type casting
Ms.Nagasundari Department of ISE, PESIT 11
Arithmetic Operators:
class arith
{
public static void main(String args[])
{
System.out.println("Integer Arithmetic");
int a = 1 + 1 * 3 / 5;
a+=5;
a++;
System.out.println("a = " + a);
System.out.println("\\nFloating Point Arithmetic");
double d = 1.5 * 3 / 2 -5;
System.out.println(“d = " + d);
}
}
Ms.Nagasundari Department of ISE, PESIT 12
Bitwise Operators:
Class bitwise
{
public static void main(String args[])
{
int a = 4 | 5; //OR
int b = 3 & 6; // And
Int c = 4 ^ 2; // Xor
System.out.println(" a = " + binary[a]);
System.out.println(“ b = " + binary[b]);
System.out.println(" a = " + binary[c]);
int x=64;
i = x << 2; // Left Shift
System.out.println("Original value of x: " + x);
System.out.println(“i : " + i);
i = x >> 2; // Right Shift
System.out.println("Original value of x: " + x);
System.out.println(“i : " + i);
}
}
Ms.Nagasundari Department of ISE, PESIT 13
Relational Operators:
class rel_op
{
public static void main(String args[])
{
int a=5,b=3,c=7,d=9;
if ( a == b) System.out.println(“a and b equal");
else if ( a < b) System.out.println(“a less then b");
else System.out.println(“a greater then b");
if( a>b && a>c) System.out.println(“a is greater");
if( a>b || a>c) System.out.println(“a maybe greater");
}
}
Ms.Nagasundari Department of ISE, PESIT 14
Assignment Operator:
class assign
{
public static void main(String args[])
{
int a,b;
a=b=4;
System.out.println(“a is ” +a “b is ” +b);
}
}
Ms.Nagasundari Department of ISE, PESIT 15
The ? Operator:
class ternary
{
public static void main(String args[])
{
int a;
bool res;
res = (a % 2 == 0) ? 1 : 0 ;
if(res !=0)
System.out.println(“a is Odd");
else
System.out.println(“a is even");
}
}
Ms.Nagasundari Department of ISE, PESIT 16
Type Casting
class arith
{
public static void main(String args[])
{
int a = 1, b = 5;
double c=6.9 , d;
long e = 96548 , f;
d = a * b + c; // Automatic Conversion
f = (long) (c * b); // Casting
}
}
Ms.Nagasundari Department of ISE, PESIT 17
Control Statements:
Snippets to demonstrate:
Selection Statements
Iteration Statements
Jump Statements
Ms.Nagasundari Department of ISE, PESIT 18
Selection Statements
class arith
{
public static void main(String args[])
{
if(i == 10) {
if(j < 20) a = b;
if(k > 100) c = d; // Nested If
else a = c;
}
else a = d;
}
}
Ms.Nagasundari Department of ISE, PESIT 19
Selection Statements (cont…)
class testing
{
public static void main(String args[])
{
String season;
if(month == 12 || month == 1 || month == 2) // If else ladder
season = "Winter";
else if(month == 3 || month == 4 || month == 5)
season = "Spring";
else if(month == 6 || month == 7 || month == 8)
season = "Summer";
else if(month == 9 || month == 10 || month == 11)
season = "Autumn";
else
season = “Invalid";
}
}
Ms.Nagasundari Department of ISE, PESIT 20
Selection Statements (cont…)
class switch_statement
{
public static void main(String args[])
{
switch(i) {
case 0: System.out.println("i is zero.");
break;
case 1: System.out.println("i is one.");
break;
case 2: System.out.println("i is two.");
break;
default: System.out.println("i is greater than 2.");
}
}
Ms.Nagasundari Department of ISE, PESIT 21
Iterative Statements
class iter_while
{
public static void main(String args[])
{
int i, j;
i = 100;
j = 200;
// find midpoint between i and j
while(++i < —j) ; // no body in this loop
System.out.println("Midpoint is " + i);
}
}
Ms.Nagasundari Department of ISE, PESIT 22
Iterative Statements (cont…)
class iter_dowhile
{
public static void main(String args[])
{
int n=0;
do
{
System.out.println(“n= " + n);
} while(++n > 10); //Prints from 1-10
}
}
Ms.Nagasundari Department of ISE, PESIT 23
Iterative Statements (cont…)
class iter_for
{
public static void main(String args[])
{
int n;
for(n=0 ; n<=10 ; n++)
System.out.println(“n= " + n);
} //Prints from 1-10
}
Ms.Nagasundari Department of ISE, PESIT 24
Iterative Statements (cont…)
class iter_nested
{
public static void main(String args[])
{
int i , j , a[4][4];
for(i=0 ; i<=4 ; i++)
for(j=0 ; j<=4 ; j++)
System.out.println(“element: " + a[i][j]);
} //Prints matrix elements of order 4*4
}
Ms.Nagasundari Department of ISE, PESIT 25
Jumping Statements
class jmp_continue
{
public static void main(String args[])
{
int i , j , a[10], odd=0;
for(i=0 ; i<=10 ; i++)
{
if(a%2==0) continue;
odd++;
}
System.out.println(“No of Odd nums = " + odd);
} //Prints number of odd numbers in the array
}
Ms.Nagasundari Department of ISE, PESIT 26
Jumping Statements (cont…)
class jmp_break
{
public static void main(String args[])
{
int i , j , a[10], odd=0;
for(i=0 ; i<=10 ; i++)
if(a<0)
{
flag=0; break;
}
if(flag==1)
System.out.println(“Array has only positive numbers”);
else
System.out.println(“Array has some negative numbers”);
}
}
Ms.Nagasundari Department of ISE, PESIT 27
Jumping Statements (cont…)
class jmp_return
{
public static int main(String args[])
{
// Some code
return 1; //Transferring control to OS with
//argument 1.
}
}
Ms.Nagasundari Department of ISE, PESIT 28
Next Section:
Classes in JAVA
Ms.Nagasundari Department of ISE, PESIT 29