Java Unit 1
Java Unit 1
Class Example
{
Public static void main(String args[])
{
System.out.println(“Welcome to Java”);
}
}
Output:
Welcome to Java
Compilation:
C:/> javac Example.java
- To run the program use java application launcher java, pass the class
name Example as command line argument
C:/> java Example
Comment:
Single line comment : // This is example program
Multi line comment: /* This is example program
File name Example.java */
Data Types
Integers:
Name Width
Long 64
Int 32
Short 16
Byte 8
Floating Point:
Name Width
Double 64
Float 32
Character:
-Size is 16 bit
-uses Unicode to represent characters to achieve global portability
Boolean:
- It has one of two possible values TRUE or FALSE
- Mainly used in relational operators and conditional operators
Variables
Declaring variable:
Type identifier[=value] [,identifier[=value]…];
Ex:
Int a;
Int a=5;
Int a=5,b;
Int a=5,b=5;
Dynamic initialization:
Value of variable can be allocated at runtime
Ex:
Int a = sum(10,20);
Int a = math.sqrt(25);
Arithmetic operators:
+ for addition
- for subtraction
* for multiplication
/ for dividsion
% for modulus
++ for increment
+= addition assignment
-= subtraction assignment
*= multiplication assignment
/= division assignment
%= modulus assignment\
-- increment
Bitwise operators:
~ unary NOT
& bitwise AND
| bitwise OR
^ bitwise ex-OR
>> Right Shift
>>> Right shift zero fill
<< left shift
&= AND assignment
|= OR assignement
^= Ex-OR assignment
>>= , <<=, >>>= shift assignments
Relational Operators:
== equal to
!= does not equal to
> greater than
< less than
>= greater than equal to
<= less than equal to
Method Overloading
Type Casting
- automatic conversion of values from one data type to other
o the two types should be compatible
o destination type should be larger than source type
- for conversion between incompatible type casting must be used
o (target_type) value
int a ,
byte b;
b = (byte) a;
Automatic type promotion:
- When two byte , short values are evaluated the result automatically
converted to short
ARRAYS
- It is a group of like-typed variables that are referred by a common name
- It means grouping related information
day = new int[12]; // now 12 elements have been created all are assigned to
zero.
Multi-dimensional array:
- It is arrays of array
- Declaration int twoD[][]= new int[4][5];
00 01 02 03 04
10 11 12 13 14
20 21 22 2 3 24
30 31 32 33 34
Alternate syntax:
type [] name;
type [] day1, day2, day3; // creates 3 arrays
CONTROL STATEMENTS
- Type of control statements
i) If
ii) If else if
iii) Switch
iv) Nested switch
If :
Syntax
If (condition)
Statement1;
Else
Statement2;
Ex:
If (a>b)
Print value of a if greater;
Else
Print value of b is greater;
If else If:
Syntax
If (condition)
Statement;
Else if(condition)
Statement;
Else if (condition)
Statement;
-------
-------
Else
Statement;
Switch:
Switch(expression)
{
Case value1:
Statement;
Break;
Case value2:
Statement;
Break;
…………………………
…………………………
Default:
Default statement;
}
Nested switch:
Switch(expression)
{
Case value1:
Switch(expression)
{
Case value1:
Statement;
Break;
Case value2:
Statement;
Break;
…………………………
…………………………
Default:
Default statement;
Break;
Case value2:
Statement;
Break;
…………………………
…………………………
Default:
Default statement;
}
Iteration Statements:
While:
-it repeats the block of statements
While(condition)
{
// body of loop
}
Do-while:
-it first execute the statements and then check for the condition
Do
{
// body of loop;
}while(condition);
For loop:
For(initialization;condition;iteration)
{
//body;
}
For each variation of for:
Syntax
For(type itr-var:collection)
{
//statement block;
}
Itr-var iteration variable
Collection from which itr-var get value in cycling manner
Ex:
For (int i=1;i<=10;i++)
{
Sum=sum+i;
}
Same code can be written using for each as follows
Int val[]={1,2,3,4,5,6,7,8,9,10};
For (int x:val)
Sum=sum+i;