Java complete notes upto now
Java 1st notes : click here
Java 2nd notes: click here
Java 3rd notes: click here
Java 4th notes: click here
Java 5th notes 👇👇 in pdf
Java 5 th notes in text 👇👇👇
Looping/repetitive/Iterative Statement
To executive statement more than one time we use looping statement
i) for
ii) while
iii) do while
•For and while loop are precondition checking
•Do while is post condition checking
For is used when number of iteration is know or fixed.
While is used when number of iteration is not fixed. The execution of statement depends upon boolean condition.
Do while: it is also used when of iteration is not fixed and need to execute the statement at least once without depending on the boolean condition
For loop syntax:
For(initialisation; condition; increment/decrement)
{ Body of loop;
}
Example: Program to find sum of n natural number
Program to find sum of n natural number
import java.util.*;
class Naturalsum{
public static void main (String args[]){
int i,sum=0,n;
Scanner sc=new Scanner(System.in);
System.out.println("Enter n value");
n=sc.nextInt();
for(i=1;i<=n;i++){
sum=sum+i;
}
System.out.println("sum of natural number upto"+n+"is"+sum);
}}
Nested for loop
A for loop present in another for loop is called nested for loop
Syntax:
for(initialisation; condition; increment/decrement){
for(initialisation; condition; increment/decrement){
Body of inner for loop
}}
Infinite for loop
Syntax: for(i j)
{ Statement}
Example: for (int i=1;i>=1;i++)
{ System.out.println("hai");}
For each: is used to access the elements in the array in a sequence.
• It comes in update of java version in (1.3)
Syntax: for(datatype variable_name : arrayname)
{ Statements }
Advantage of for each
• In for each no initialisation, no condition & no increment/decrement is required.
Example:
public static void main (String args[]){
int a[]={10,20,30};
for(int i: a[])
{ System.out.println(i)}}
While loop: is used when we don't know the number of iteration.
Syntax: initialisation;
while (condition)
{ Statement;
increment/decrement;
}}
Example:
public class whileloop{
public static void main (String args []){
while(int i=5){
System.out.println(i);
i++;
}}}
Infinite while loop:
while(true){
Statement; }
Example:
public class infiniteloop
public static void main (String args []){
while (int i>=1){
System.out.println("hello");
i++; } } }
Do while: this is also used when we don't know the number of iteration, and statement should be executed at least once.
Syntax: initialisation;
do{
Statement;
increment/decrement;
}
while(condition)
public class do_while{
public static void main (String args []){
int i=5;
do{
System.out.println(i);
i++;
} While(i<5);
}}
JVM performs following main task
1. Load the code
2. Verify the code
3. Execute the code
4. Provided the runtime environment
1. Class loader: is use to load class file
2. Class area: it is use to store structure of class and the methods
3.Heap: it is used for storing the object data allocated for class.
4. Stack: it is used to store local variable intermediate result and final result
5. Program counter: it contains the address of the instruction that are currently executing
6. Native method: it is used to store all the native method used in the application
7. Execution engin: it contained virtual processor interpreter and just in time compiler
8. JIT: Just in time compiler compiles the bytecode that have similar functionality at the same time
Data type:
In Java we have two types of data type
1. Primary data type: used for storing a single value.
Eg: int a=10, char ch='a'
Primitive data types are of 8 categories
1.Boolean_________> JVM specifies
2. character_______> 2 bytes
3. byte ____________> 1 byte
4.short ____________> 2 bytes
5.int _______________> 4 bytes
6.long ______________> 8 bytes
7.float _______________> 4 bytes
8. double______________> 8 bytes
2. Non-primary data type: If a variable is capable of storing more than one values is called as non primary data type
Example: array, string etc.
Variable: are name assigned to a memory is called variable
A variable contains
Data type, address, value, size, range.
Naming rule of a variable:
• Variable name cannot be a keyword
• variable name should not start with character it may contain digit letter etc.
• if a variable name is a more than of two words then second word of starting letter should be of capital letter.
Eg: int evenSum;
float avg Marks;
• variable name should be a single word cannot be separated with spaces
Types of variable
1. Local variable it is inside method
2. Instance variable it is inside a class outside of method
3. Static variable: which have static keyword is called as static variable
Example:
class A{
int data=50;// instance variable
Static int m=30; // static variable
void main(){
int n=90;//local variable
}
}
Array
Array is a collection of similar data type is called array
1 Dimensional Array declaration in Java
data type array_name[ ]; or
data type [ ] array_name;
Eg: int [ ] a; or int a [ ];
Data type array_name[ ]=new data type [size]
• size can be 0, or positive but not negative
Example: int a[ ]= new int[3]
Compile time initialisation
Syntax: datatype array_name[ ]={ list of element};
Example: int a[ ] ={10,20,30};
class sd{
public static void main (String args [ ]){
int a[ ] ={10,20,30};
for(int i=0;i<=2;i++)
System.out.println("element in array"+a.length);
{ System.out.println(a[i]);
} }
To find number of elements in array we use
a.length
Run time initialisation
package pow;
import java.util.Scanner;
public class sdri {
public static void main(String[] args) {
int i;
System.out.println("enter size of array");
Scanner sc=new Scanner(System.in);
int size=sc.nextInt();
System.out.println("Enter "+s+" integer value");
int arr[ ]=new int[size];
for( i=0;i<size;i++)
arr[i]=sc.nextInt();
System.out.println("elements in array are: );
for(i=0;i<size;i++)
{ System.out.println(a[ i ]);
}
}
2 dimensional array
Data type array_name[] [];
Eg : int a[][]
int [] []a; int [][]a;
int [] a[];
int []a[];
Creation of object
array_name=new data type [size][size];
Eg a=new int[3][2];
int a[][]= new int[3][2];
Compile time initialisation:
Syntax: datatype array_name[][]={ list of element};
Example: a[][]= {{3,2},{4,5}}
Program to demonstrate creation of 2D array at compile time initialisation
class 2Darray{
public static void main (String args[]){
int a[][]={{1,2},{3,4},{5,6}};
for(i=0;i<=2;i++){
for(j=0;j<=1;j++){
System.out.print(a[i][j]" ");
}
System.out.println();
}}}
Run time initialisation
import java.util.*;
class 2Ddemo{
public static void main (String args []){
int size1,size2;
Scanner sc=new Scanner(System.in);
System.out.println("enter size 1");
size1 =sc.nextInt();
System.out.println("enter size 2");
size2=sc.nextInt();
int a[][]=new int[size1][size2];
size2 =sc.nextInt();
int a[][]=new int [size1][size2];
System.out.println("enter"+size 1*size 2+"elements");
for(i=0;i<size1;i++){
for(j=0;j<size2;j++){
a[i][j]=sc.nextInt();
}}
System.out.println("elements in array are:");
for(i=0;i<size1;i++){
for(j=0;j<size2;j++){
System.out.print(a[i][j] " ");
System.out.println();
}}}
Declaration of multidimensional array
int a[][][]; or int [][][]a; or int [] [][]a; or int [] a[][]
Advantages of array
•Code optimisation
•Reusability random
•Access
Operators
• Arithmetic operator (+,-,/,*,%)
• relational operator(<,<=,>,>=,==,!=)
• logical operator(&&,||,!!)
• assignment operator(=,+=,-=,*=,/=)
• increment or decrement operator
• bitwise operator(&, |, !)
• conditional operator (?:)
• special operator(instance of)
0 Comments