Part 1 JAVA
Switch Statement:
We use switch statement to match switch input with the case.
The case which matches the switch input will be executed.
SYNTAX:
switch (variable/value/expression)
{
case value/expression :{
task
[break];
}
case value/expression :{
task
[break];
}
.
.
default :{
task
[break];
}
}
PROGRAM (SWITCH CASE WITH OUT BREAK):
class switchwithoutbreak
{
public sta c void main(String[] args)
{
System.out.println("Hello World!");
int a=2;
switch (a)
{
case 1:{
System.out.println("Java");
}
case 2:{
System.out.println("JavaScript");
}
case 3:{
System.out.println("JavaEE");
}
default:{
System.out.println("Invalid");
}
}
}
}
What is Break?
1. It is a keyword
2. We can use break inside the switch block or inside the loop.
3. Break is a control transfer statement which will transfer the
control of the main thread outside the switch block or outside the
loop.
Example with break keyword:
class switchwithbreak
{
public sta c void main(String[] args)
{
String food="Idly";
switch (food)
{
case "idly":{
System.out.println("Earth");
}
case "Vada":{
System.out.println("Mars");
}
case "Idly":{
System.out.println("Jupiter");
break;
}
default:{
System.out.println("Invalid");
}
}
}
}
OUTPUT:
Jupiter
//EXAMPLE PROGRAM SWITCH:
import java.u l.*;
class task1
{
sta c Scanner sc=new Scanner(System.in);
public sta c void main(String[] args)
{
System.out.print("Enter the selec on (1-7):");
int a=sc.nextInt();
switch (a)
{
case 1:{
System.out.println("Sunday");
break;
}
case 2:{
System.out.println("Monday");
break;
}
case 3:{
System.out.println("Tuesday");
break;
}
case 4:{
System.out.println("Wednesday");
break;
}
case 5:{
System.out.println("Thursday");
break;
}
case 6:{
System.out.println("Friday");
break;
}
case 7:{
System.out.println("Saturday");
break;
}
default:{
System.out.println("Invalid");
break;
}
}
}
}
Note:We cannot pass Boolean ,double, float , and long type of data
to switch it is CTE.
Switch will only accept byte, short, int ,char and string type of data.
JAVA CLASS
Looping Statement:
We use Looping statements to achieve repeata on.
In looping statement we are having 4 types:
1. While
2. Do-while
3. For loop
4. Advanced for /for- each loop.
While:
Syntax:
While(condi on)
{
//Task
}
WAJP to print QSPIDERS 4 mes:
class task5
{
public sta c void main(String[] args)
{
int i=1;
while (i<=4)
{
System.out.println("QSPIDERS");
i++;
}
}
}
Output:
QSPIDERS
QSPIDERS
QSPIDERS
QSPIDERS
WAJP to print 1-N where n value is decided by user:
WAJP To convert binary to decimal?
WAJP To convert decimal to binary?
DO-WHILE LOOP:
Syntax:
Do
{
Task
}
While(condi on);
WAJP to print qspiders 4 mes?
do
{
S.o.pln(“Qspiders”);
Count++;
}while(count<=4);
false
JAVA CLASS 19-Feb-25
For loop:
Syntax:
for((declara on&ini aliza on/ini aliza on) ; (condi on) ; (upda on))
{
//Task
}
Note:
All the 3 segments of the for loop is op onal.
WAJP to print Qspiders 5 mes:
WAJP to print 1 to N where N value is decided by user.
WAJP to print 1 to N where N value is even and decided by user.
WAJP to print 1 to N where N value is odd and decided by user.
WAJP to add the N natural numbers:
WAJP to print the tables:
WAJP to find the factorial of the number?
WAJP to find the factors .
WAJP TO check whether given number is perfect number or not?
WAJP TO check whether given number is prime or not?
Range of prime
Strong Num?
Armstrong Num?
Java 24-feb Pa ern Programming.
0 1 2 3 4 5 j
i 0
1
2 i=n/2
3
4
i+j==n-1 J=n/2 i==j
j<=n/2 j>=n/2
i<=n/2
J
i>=n/2
Java 25-feb Pa ern Programming
0 1 2 3 4 j5
i 0
1
2
3 i==j+n/2-1
4
i==j+n/2-1
Methods:
Defeni on:
Method is a block of instruc on which is used to perform some par cular
task.
Syntax:
[Access modifier] [modifier] returnType methodName([Formal Arguments])
{
//Task
}
Note:
Here Access modifier, Modifier and formal Arguments are
op onal.
But return type and method name is mandatory.
We are having Two types of methods:
o No argument Method (No formal Arguments)
o Parameterized method.(contains formal arguments)
WAJP which contains no argument and parameterized method.
Method Call Statement:
Main method will call all other methods.
Syntax:
methodName([data]);
Note:
Method will be only executed if we call the methods.
Whenever we are calling the methods the actual argument should be
accepted by formal arguments.
We cannot call void methods inside prin ng statement.
Sheela(); ❌
Sheela(‘$’,true); ❌
Sheela(2.3,5); ❌
Sheela(14,2.3,8) ❌
Sheela(18,2.6); ✔ (int,double)
26-Feb-25
What is method Signature?
The combina on of method name along with formal arguments is called as
method signature.
What is method declara on?
Access modifier ,modifier ,return type along with method signature is called as
method declara on.
What is method implementa on?
Method declara on along with the body of the method is called as method
implementa on.
Access modifier:
In java ,access modifiers decides the visibility of the member
such as class , variables, methods ,constructors.
We are having 4 access modifiers which is given below,
o Public
o Protected
o Default
o Private
Modifier:
In java ,modifiers decides the characteris c of the member such
as class ,variable ,methods .
We are having modifiers such as ,
o sta c
o final
o abstract
o synchronized
o transient
o na ve
o vola le
o etc.,
Note:
For the void methods ,if a programmer forget to add return
keyword compiler at the me of compile me will add return
keyword.
Return keyword will help the programmer for the proper exit
from the method.
Every method should have return keyword but for the void
method it is not mandatory.
Return type:
It is one of the component present in the method by seeing the
return type programmer can easily understand what method
will return at the end.
In the place of return type we can write void/Primi ve
datatype/ Non primi ve datatype.
return :
It is a keyword.
It is also called as control transfer statement.
It helps for the proper exit from the method with data or
without data based on the return type men oned.
We should use return statement as the last statement else we
get unreachable statement error.
Method eg:
Example 1:
public sta c void dinga(int a){
-----------------
-----------------
return ;
}
Example 2:
String res=dinga(15);
public sta c int dinga(int a){
-----------------
-----------------
return “java”;
}
Example 3:
char res=dinga(8,3.14);
public sta c char dinga(int a,double b){
-----------------
-----------------
return ‘$’;
}
WAJP to find the largest of two numbers using method .
If(50>12)
Return x
largestO wo
Program start
Enter a: 50
Enter b:12
Int res=largestOfTwo(10,20)
Largest of two:50
Program ends
Main method
Note:
Void method cannot be called inside the print statement.
Method eg:
Recursion:
It is the process where the method is calling itself is
called as recursion.
During recursion there is a possibility we can get
run me error called as stack overflow error.
//RTE
WAJP to print 1 to 5 without using loop.
WAJP to find factorial without using loop:
ARRAYS:
Array is a con nuous memory block used to store
homogenous elements. (OR)
It is a linear data structure used to store homogeneous
elements.
NOTE:
Array is a non-primi ve data type.
Arrays will be created inside the heap area.
Each and every memories of array contains index (iden ty)
We can create arrays in two ways.
1. Dynamic
2. Sta c
Dynamic way of array crea on :
Syntax:
datatype[] variable; //declara on of array
datatype[] variable=new datatype[int_size]//D&I
datatype[] variable;
--------------
--------------
variable = new datatype[integer_size];
//ini aliza on of array
Binary Search:
In order to implement Binary Search we HSould follow the below points
1. .Array should be sorted either in Acending order or decending order
2. mark the lower index Middle index and higher index.
3. Search the key element in the middle or le from the middle right from the
middle.(key==m(or)key<m(or)key>m)
Method Concept:
NOTE :
1.
public sta c int m1()
{
System.out.println(“hi ”);
} //CTE missing return statement.
2.
public sta c int m2()
{
return 7,5;
}//CTE more than one data in return keyword
3.
public sta c int m3()
{
return 7;
return 5;
}//CTE more than one return keyword( unreachable statement)
4.
public sta c void m4()
{
System.out.println(“hi sheela”);
System.out.println(“hi leela ”);
return 10;
}//CTE In void can’t return the data
5.
public sta c void m5()
{
System.out.println(“hi mala”);
return;
}//CTS;
6.
public sta c int m6()
{
System.out.println(“hi ajay”);
return;
}//CTE int return type should return a integer data.
7.
public sta c int , boolean m5()
{
System.out.println(“hello”);
return 7,true;
}//CTE it has more than one return type and return keyword
8.
public sta c void 7Leela()
{
System.out.println(“QSP”);
}//CTE method name can’t have symbol on star ng.
9.
public sta c int m8()
{
System.out.println(“Developement”);
return 10;
System.out.println(“tes ng”);
}//CTE unreachable statement .
DATA TYPE MOCK
float a=10;
System.out.println(a);//10.0
float b=10.3;
System.out.println(b);//CTE F is missing (ie:10.3F)
double a=20;
System.out.println(a);//20.0
double b=20.05
System.out.println(b);//20.05
System.out.println(a);//CTS
Selec on Sort:
o/p: [8, 11, 5, 4, -3, 2, 6]
[-3, 2, 4, 5, 6, 8, 11]
Inser on Sort:
Coun ng Sort:
MULTI DIMENSION ARRAYS:
One array present inside another array is called as mul dimension array
[I@12 [I@14 [I@17
0 1 2 0 1 2 0 1 2
0 1 2
[I@32
Mul dimension array represent matrix
We can create 2D array in two way
o Dynamic way
o Sta c way
DYNAMIC WAY OF CREATING MULTI DIMENSION ARRAYS:
Datatype [] [] variable ; //Declara on of MDA
Datatype [] [] variable=new datatype [row size] [column size]; //D&I of MDA
Datatype [] [] variable;
o Variable = new datatype [row size] [column size]; //Ini aliza on of MDA
1.int [] [] a=new int [3][3];
0 0 0
0 0 0
0 0 0
Or
[I@10 0 0 0 0
[I@11 1 0 0 0
[I@12 2 0 0 0
1.string [] [] a=new string [3][3];
Null Null Null
Null Null Null
Null Null Null
Or
[I@10 0 Null Null Null
[I@11 1 Null Null Null
[I@12 2 Null Null Null
Jagged arrays:
Any mul dimension array which contains unequal rows and columns is called as Jagged arrays
In mul dimension array column size is op onal.
[I@10 0 0 0 10 0 0 //5
[I@11 1
[I@12 2 0 0 0 0 0 0 0 //7
Traversing technique for Mul dimension Array:
Top down & right to le
for (int i=0;i<a.length;i++){
for (int j=0;j<a[i].length;j++;){
}
Top down & le to right
for (int i=0;i<a.length;i++){
for (int j=a[i].length-1;j>=0;j++;){
Down Top & le to right
for (int i=a.length-1;i>=0;i++){
for (int j=0;j<a[i].length;j++;){
Down Top & right to le
for (int i=a.length-1;i>=0;i++){
for (int j=a[i].length-1;j>=0;j++;){
Wajp to insert the elements according to the user in 3X3 matrix and print the elements from top to
bo om and le to right.
Sta c way of MDA crea on:
Syntax:
Datatype[][]variable={ {V1,V2,V3……}
{V1,V2,V3…..}
{V1,V2,V3….}
.
.
.
};
Eg:
Int [][]a={ {10,20,30}
{40,50,60}
{70,80,90}
};
10 20 30
40 50 60
70 80 90
i@32->a
I@14 10,20,30
I@13 40,50,60
I@14 70,80,90
WAJP to copy one array elements into another:
Wajp to add two matrix:
WAJP TO FIND THE FOLLOWING (SPIRAL MATRIX):
I/P: 1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
O/P:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
String:
Anything present inside double quotes is called as string.
eg: "hello" "java script" "1234" "&*#"
String contains index.
eg: "A B C D" , "H e l l o"
0123 01234
String is also a built in class present inside java.lang package
String class contains some built in methods which helps in String programming.
WAJP to print the length of the String.
WAJP to print the string using new keyword.
WAJP to extract character from the string
WAJP to read the string data
WAJP to compare the String and check both the string same or not
WAJP to check the String is palindrome or not
WAJP to convert string into Character.
WAJP to convert string into Character (using built in func on)
WAJP to check whether the given strings are anagram or not.
WAJP to convert string into uppercase as well as lower case
WAJP to trim the spaces:
WAJP to extract sub string:
WAJP to find index of sub string
LONGEST COMMON PREFFIX PROGRAM
INPUT: S23YE1D
OUTPUT: 6
Pangram program (write a java program to check the given string is pangram or not.
I/p:abacc
o/p:mbmcc
I/p: hi@how#are
o/P:era@awo#hih
WAJP to get shortest path to the des na on:
I/P: AbMBdr
O/P: RDbmBA
i/p: s1= banana,s2=apple ,s3=apple
NOTE:
Current string > passed string answer is +ve
Current string < passed string answer is -ve
Current string =passed string answer is 0
APPROACH 1
APPROACH 2
[5168, 93, 463, 1235, 14]
[1235, 14, 463, 5168, 93]
Sta c:
It is a keyword as well as it is a modifier.
The resource which remains same for all the objects should be represented by sta c.
Eg:
o Country
o Qspiders
o Prime Minister
o Kitchen
Sta c Members:
We are having 3 sta c members
o Sta c Variable
o Sta c Methods
o Sta c Ini alizers
Single-line Sta c Ini alizer
Mul -line Sta c Ini alizer
Sta c variable:
Any variable which is declared or declared & ini alized in global area and prefix with sta c is
called as sta c variables.
Note:
Sta c resources cannot be created in local areas.
We can access sta c variable directly in the same class or in other classes.
Create 10 sta c variables and access in same class and also in other class
Sta c method:
Any method prefix with sta c is called as sta c method.
Eg:
What is Ini alizers?
Ini alizers are beginning instruc ons which need to be executed at the beginning stage.
We are having two types of sta c ini alizers
o Single line sta c ini alizers
o Mul line sta c ini alizers
Single line sta c ini alizers:
Any sta c variable which is declared and ini alized in a same line is called as single line sta c
ini alizers.
Eg
Mul line sta c ini alizers:
Any block which is only prefixed with only sta c keyword is called as mul line sta c
ini alizers
Eg:
Note:
Class keyword will create class block & inside class block we will be having sta c resources.
loading
class Class
loader
Sta c class block
resources
Sta c pool or sta c area
Non-Sta c:
The resource which is not same for all the objects should be represented by non sta c.
school name,id,ph <-non sta c
name,id,ph <-non sta c
name,id,ph <-non sta c
. car1 name,color,price ->non sta c
car1 name,color,price ->non sta c
car1 name,color,price ->non sta c
With respect to non-sta c we have 3 members
o Non sta c variables.
o Non sta c methods.
o Non sta c ini alizers.
Non sta c variables:
Any variable which is declared or declared & ini alized in global area and not prefix with
sta c is called as non sta c variable
Eg:
Note:
We cannot say local variable as non sta c variables they are just local variables.
Non sta c methods:
Any method which is not prefix with sta c is called as non sta c method.
Eg:
Non sta c ini alizers:
We are having 2 types of non sta c ini alizers
o Single line non sta c ini alizers.
o Mul line non sta c ini alizers.
What is single line non sta c ini alizers?
Any non-sta c variable which is declared & ini alized in same line is called single line non
sta c ini alizers.
Eg:
What is Mul line Non staic ini alizers?
Any block which is not having any iden ty is called as Mul line non sta c ini alizers.
Eg:
heap
New keyword
Non –sta c
resources
Object block
Class Loading Process:
STEP 1: Whenever we execute any program at the run me following memory blocks will be
created.
o Method area
o Class sta c area
o Stack area
o Heap area
STEP 2: A block of memory is created inside the class sta c area along with the class name
called as class block.
STEP 3: All the methods and mul line ini alizers will be loaded inside the method area
along with the address.
STEP 4: The sta c members of the method area will be loaded inside the class block.
STEP 5: All the sta c variables will be loaded inside the class block with the default values.
STEP 6: All the sta c ini alizers will be executed from top to bo om.
STEP 7: main method is called execu on starts and execu on ends.
{…} -10x a=0 Hello World
JRE
main(S[]a) -11x b=false true
F=0 main(s[]args)
run() -12x main(s[]ar) -11x
sta c{…} -13x sta c{…} -13x Hi I am SMLI
F=0 sta c{..} -13x
Method area C.S.A or sta c pool Stack Area Heap area
Print(“I am M1()”);
F=1 m1()
Class bock Print(“a:”+a); a=30;
M1();
Sta c{..} -10x a=null java Print(“bye”);
{..} -11x sta c{..} -10x F=0 main(s[]ar)12x
JRE
main{st[]arg)-12x Print(“JDBC”);
main(s[]ar) -12x
m1() -13x F=0 sta c{…}15x
m1() -13x
m2() -14x Print(“a:”+a); a=10;
sta c{..} -15x Print(“a:”+Demo1.a)
sta c{..} - 15x ;
Demo111 F=0 sta c{..} -10x
Method area C.S.A or sta c pool Stack Area Heap area
Print(“I am M1()”);
F=1 m1()
Class bock Print(“a:”+a); a=30;
M1();
Sta c{..} -10x a=null java Print(“bye”);
{..} -11x sta c{..} -10x F=0 main(s[]ar)12x
JRE
main{st[]arg)-12x Print(“JDBC”);
main(s[]ar) -12x
m1() -13x F=0 sta c{…}15x
m1() -13x
m2() -14x Print(“a:”+a); a=10;
sta c{..} -15x Print(“a:”+Demo1.a)
sta c{..} - 15x ;
Demo111 F=0 sta c{..} -10x
Method area C.S.A or sta c pool Stack Area Heap area
JAVA 26-03
Hi I am A class SMLI
sta c{..} - 12x F=1 sta c{..} -12x
x=0 10 Hi I am class B main
demo() -13x
JRE
sta c{..} -10x X:A.x =10
main(s[]ar)-14x Y:20
main(S[]arg)-14x X:A.x Bye from cls B
A
main(s[]args)-10x F=0 main(s[]args)-11x
y=0 20
sta c{..} -11x Hi I am B class SMLI
sta c{..} -10x
main(s[]ar)-11x
B F=0 sta c{..} -10x
Method area C.S.A or sta c pool Stack Area Heap area