Week 2
Week 2
2. import statement similar to header files in C and C++ and All the pre-defined classes will be available in this. Methods and
interfaces available in classes
3. package statement we can create our own user defined packages
Ex: package student;
4. interface statement same as classes but it contains only method declarations and useful in order to implement multiple
inheritance
5. Class definition must write the entire logic in class only
6. main method class main() method should contain in one class
main method definition
Simple JAVA program
//sample program to print a content
class SamplePgm
{
public static void main(String args[])
{
System.out.println(“Hello World”);
}
}
Save: SamplePgm.java Compilation: javac SamplePgm.java
Execution: java SamplePgm Output: Hello World
Main Method
public static void main(String args [])
“Write-Once Run-Anywhere”
Changes in system resources will not force any change in the program.
The Java Virtual Machine (JVM) hides the complexity of working on a particular
platform
Exception handling
if((a>b)&&(a>c))
System.out.println(a + " the greatest number among all");
if((a>b)||(a>c))
System.out.println(b + " may be the greatest number among all");
if(!(a>b)&&(a>c))
System.out.println(c + " may be the greatest number among all");
}
}
Bitwise operator
A=0011 1100 B=0000 1101
Output:
public class BitWise {
public static void main(String args[]) 7 ~ operation:
{ 4 a=6 0000 0110
int a=6; 3 Filp bits 1111 1001(1)
int b=5; -7 -7 in 2’s complement form
System.out.println(a|b); 24 7 0000 0111
System.out.println(a&b); 1 Flip bits 1111 1000
System.out.println(a^b); Add 1 1111 1001 (2)
System.out.println(~a); (1),(2) are same
System.out.println(a<<2);
System.out.println(a>>2);
<< operation:
}
a=6 0000 0110
}
a<<1 0000 1100 12
a<<2 0001 1000 24
>> operation:
a=6 0000 0110
a>>1 0000 0011 3
a>>2 0000 0001 1
Unary operator
• The increment and decrement operators are arithmetic and operate on one operand
• The increment operator (++) adds one to its operand
• The decrement operator (--) subtracts one from its operand
• The statement count++;
• is functionally equivalent to count = count + 1;
• If count currently contains 45, then the statement
total = count++;
assigns 45 to total and 46 to count
• If count currently contains 45, then the statement
total = ++count;
assigns the value 46 to both total and count
Assignment operator
• The right hand side of an assignment operator can be a complex expression
• The entire right-hand expression is evaluated first, then the result is combined with the original
variable