Part 1 Java
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
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
}
Output:
QSPIDERS
QSPIDERS
QSPIDERS
QSPIDERS
WAJP to print 1-N where n value is decided by user:
do
{
S.o.pln(“Qspiders”);
Count++;
}while(count<=4);
false
j<=n/2 j>=n/2
i<=n/2
J
i>=n/2
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.
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
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;
--------------
--------------
variable = new datatype[integer_size];
//ini aliza on of array
Binary Search:
In order to implement Binary Search we HSould follow the below points
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 .
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:
[-3, 2, 4, 5, 6, 8, 11]
Inser on Sort:
Coun ng Sort:
One array present inside another array is called as mul dimension array
0 1 2 0 1 2 0 1 2
0 1 2
[I@32
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
Or
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
}
Top down & le to right
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:
String:
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:
APPROACH 1
APPROACH 2
Sta c:
Sta c Members:
Any variable which is declared or declared & ini alized in global area and prefix with sta c is
called as sta c variables.
Note:
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:
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
Any sta c variable which is declared and ini alized in a same line is called as single line sta c
ini alizers.
Eg
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
resources
Non-Sta c:
The resource which is not same for all the objects should be represented by non sta c.
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:
Any non-sta c variable which is declared & ini alized in same line is called single line non
sta c ini alizers.
Eg:
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
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
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