0% found this document useful (0 votes)
9 views

Del java day 2

Uploaded by

pragatiraichan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Del java day 2

Uploaded by

pragatiraichan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 56

Date 4/5/2023

java has 4 categories


1. core java
2. advanced java(JDBC)
3. frameworks
4. android application development

core java
section 1(basic programming)
1. java components(javac, jvm, jre)
2.installation of java
3.sample program, compilation, execution of the java program
4.variables and operators
5. control statements
6. function
7. array(only introduction)
8. string(only introduction)

section 2(object oriented programming language(OOPs))


1. class and object
2. blocks
3. constructor
4. composition
5.inheritance
6. method overloading
7. method overriding
8. type casting
9. abstract class and abstract methods
10.java interface
11. abstraction
12. encapsulation
13. polymorphism
14. java bean classes
15. singleton class

section 3(java library)


1. members of object class
2. indepth of strings
3. Exception handling
4. indepth of arrays
5. collection framework
6. multi threaded programming

Topic 1
java components(javac, jvm, jre)
JDK(java development kit(javac+jre))

JRE-Java runtime environment

There are two types of java runtime environment


1. Private JRE(it runs on a single machine or on a single computer)
2. Public JRE(It runs on a server machine or on multiple devices)

Java is platform independent

NOTE: Java is platform independent and dependent on JRE

JAVA editions

1. J2SE(core java)-----java 2nd version Standard Edition--------standalone


application(notepad,VLC,Icon,Internet Explorer)

2. J2EE(advance java)-----java 2nd version Enterprice Edition----web application(which requires


internet)
3. J2ME(android application development) java 2nd version micro edition---mobile applications

JDK versions
1.0
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8(most recommended version of jdk)
.
.
.
1.19

Text editors
1. Notepad
2. Notepad ++
3. Edit Plus

IDE(integrated Development Environment)

1. Eclipse(most recommended)
2. Netbeans

Topic 2 .installation of java DONE

1.Variable name-----JAVA_HOME
Varibale value-----address of jdk

2.Variable name----JRE_HOME
Variable value-----address of jre

3.Varibale name-----path
Variable value-----address of bin folder under jdk

Stpes to be followed

 Go to this Pc
 Right click on the white screen
 Click on properties
 Click on advanced system setting
 Click on environment variables
 In system variable section
 Click on new and use first variable name and value as given
 And follow all the three varibales

Topic 3.sample program, compilation, execution of the java


program

Class ClassName//declaration of the class


{//class body started

Public static void main(String args[])//declaration of main method


{//main method body started

//Java statements;

}//main method body ended

}//class body ended

{
Body/contaxt/definition block
}

System.out.println(value);

This is a java statement which is used to print the value on the screen

Value types

1. Number
A. Int
Eg. 123,45
B. Float
Eg. 12.34
2. Char
Eg
‘a’,’5’,’*’

3. Boolean
Eg.
True/false
4. String
Eg.
“abc123”,”123abc”,”**@#)”

Eg 1. sample program(open notepad and write the same program)

Class Program1
{
Public static void main(String arg[])
{

System.out.println(“main method started”);


System.out.println(1234);
System.out.println(12.34);
System.out.println(‘j’);
System.out.println(“Java”);
System.out.println(true);

System.out.println(“main method ended”);


}
}

After writing this program


Save this program to desktop by name Program1.java

Go to command prompt typing cmd to the search box


1. Change the dorectory
Cd desktop
Desktop>

Compile the program

Desktop>javac Program1.java
Desktop>java Program1
Topic 4. Variables and operators

Variable?

Used to store the value

Declaration of varibale

Syntax

Data_type identifier;

Eg.

Int id;
 Data_type represents the type of value is to be
stored in the variable

 Identifier represents the variable name

Data types in Java language

1. Byte
2. Short
3. Int
4. Long

All the above data types represents integer values

5. Float
6. Double

5, 6 represents the floating point values

7. Char

Represents character value

8. Boolean
Represents boolean value

NOTE:
String is value type or a class in java but not a datatype

Variable initialization

Syntax

Variable_name =value;

Eg. Of variable initialization

Id=12;

Eg of variable declaration and initialization

Class Program2
{
Public static void main(String arg[])
{
System.out.println(“main method started”);
//variable declaration
Int empId;
String Empname;
Double EmpSalary;
//varibale initialization

empId=1234;
EmpName=”ramesh”;
EmpSalary=25000.00;

//displaing all the values on the screen

System.out.println(empId);
System.out.println(EmpName);
System.out.println(EmpSalary);

System.out.println(“main method ended”);

}
}

OutPut
Print statement in Java

There are two types of print


statement in java

1.
System.out.println(‘j’);
System.out.println(‘k’);
System.out.println(‘l’);
Output
J
K
L
|(cursor will be here) in case of
Println

2. System.out.print(‘j’);
System.out.print(‘k’);
System.out.print(‘l’);

Output
Jkl_(cursor will beat next to the
character)

1. Assignment
Print

123Number
Number
Is
One two three

Systyem.out.println(“123Number”)
System.out.println(“Number”);
System.out.println(“is”);
System.out.print(“one”);
System.out.print(“Two”);
System.out.print(“three”);

3. Assignment
Print

Ramesh
Kumar
Sharma

Using single Print Statement

System.out.println(“Ramesh\
nKumar\nSharma”);

\n-----is used to break the line

4. Assignment
Print as

Ramesh Kumar Sharma


System.out.println(“Ramesh\
tKumar\tSharma”);

\t----for leaving one tab

1 tab=4 spaces

5. Assignment

Print as

I am a “software” engineer

System.out.println(“I am “software”engineer”);//error

Correct way is

System.out.println(“I am a \”software\” Engineer”);


Operators

+ (plus operator)

1. Addition
2. Concatnation

Eg.
1. 10+10=20( addition operator)
2. “abc”+”xyz”=abcxyz(Concatnation
operator)
3. 123+”pqr”=123pqr(concatnation)
4. 20+20+”number”=40Number(add
& concatnation)
5. “number”+20+20=number2020(c
oncatnation)
6. “number”+(20+20)=Number40(ca
ncatnation and addition)
Eg. Of plus(+) operator

System.out.println(10+10);//20
System.out.println(123+”abc”);//
123abc
System.out.println(“abc”+”pqr”);//
abcpqr
System.out.println(20+20+”Number
”);//40Number
System.out.println(“number”+20+2
0);//Number2020
System.out.println(“Number”+(20+
20));//Number40
Operators in Java
1. Arithmetic operator
2. Relational operator
3. Logical operator
4. Unary Operator
5. Bitwise operator(will be taught
later)

1. Arithmetic operator(+,-,*,/,%)

Eg.

Int a=10;
Int b=5;

System.out.println(a+b);//15
System.out.println(a-b);//5
System.out.println(a*b);//50
System.out.println(a/b);//2
System.out.println(a%b);//
0(remainder)

2&3
Relational and Logical operators

Note:
1. result of both the Relational and
Logical operator is Boolean true or
false.
2. Relational operator can work on
any type of value;
3. But logical operator works on
true or false values
Eg. Of relational
operator(<,>,<=,>=,==,!=)

Int a=10;
Int b=5;

System.out.println(a<b);//false
System.out.println(a>b);//true
System.out.println(a==b)//false
System.out.println(a<=b);//false
System.out.println(a>=b);//true
System.out.println(a!=b);//true

Logical
operators(AND(&&),OR(||),NOT(!))
Truth Table of AND Operator

Input && input Output


True && True = True
True && False=False
False && True=False
False && False=False

Note: in case of AND (&&) operator


we are getting True on if all the
values are true else false

Truth Table of Or(||) operartor


Input || input Output
True || True = True
True || False= True
False || True=True
False || False=False

NOTE: in case of Or operator we will


get true if either or all of the input
are true else false

We will get false if all the input are


false

Not(!)
Note : we will get opposite result to
the input

If input is true will we will get false


And if input is false we will get true.

OKay guys?

Eg. Of Logical Operator

Int a=5;
Int b=10;
Int c=5;

System.out.println(a==c &&
c==a);//True
System.out.println(a<b &&
c<b);//true
System.out.println(b>a &&
c<b);//True
System.out.println(a>b &&
b>c);//false
System.out.println(a==c
||c==a);//True
System.out.println(a<b ||
c<b);//true
System.out.println(b>a
||c<b);//True
System.out.println(a>b ||
b>c);//false

4. Unary operator
1. ++(increment (will increment the
value by 1))
2. --(decrement(will decrement the
value by 1))
Both comes in two types

1. Pre
2. Post

Like
1. pre inrement
2. Post increment

3. Pre decrement
4. Post decrement

Eg

Int x=0;
Int y=0;

System.out.println(x);//0
System.out.println(y);//0

X++;
++y;
System.out.println(x);//1
System.out.println(y);//1

X--;

--y;

System.out.println(x);//0
System.out.println(y);//0

NOTE:

It doesn’t matter if we are using the


operator in pre or post form
The result will be all alike
But if there is an expression than
the result will be changed and will
not be like previous examples

What is an expression?

An expression is a arithmetical
equation with more then one
operator

For example

Int x=0;
Int y=0;

Y=x++;//this is an expression
So here the result will be
completely changed

System.out.println(x);1
System.out.println(y);0

Eg. 2

Int x=0;
Int y=0;

Y=x++ + x + x++ + ++x;

System.out.println(x);//3
System.out.println(y);//5
Eg.3
Int x=0;
Int y=0;

X=x++;
X=x++;
X=x++;

System.out.println(x);//0

NOTE: if both the sides we have


same name variable then operation
will take place but increment and
decrement will be ignored
Eg. 4

Int x=0;
Int y=0;

X=++x;
X=++x;
X=++x;

System.out.println(x);//3

Control statements
There are two types of control
statements

1. Branching
2. Looping

Branching has two types


1. If
2. Switch
Looping has basically 3 types but we
will understand two of them

1. For (recommended to learn)


2. While(recommended to learn)
3. Do while loop

NOTE:there is an extra type of loop


in java that is Advanced Loop or
Enhanced loop or for each loop
Control statements

1. If

Syntax

If(condition)
{
// if body statements
}

NOTE: if body statements will be executed if the


condition is true.

2. If and else
Syntax

If(condition)
{
//if body statements
}
Else
{
//else body statements
}
NOTE: if the condition is true then if body statements
will be executed otherwise if the condition is false then
else body statements will be executed

Eg. Of if statement

Class ProgramName
{
Public static void main(String arg[])
{
System.out.println(“Main method started”);
Int x=5;

If(x>4)
{
System.out.println(“x is greater then 4”);
}

System.out.println(“Main method ended);


}
}

Output is
x is greater then 4

Eg of if and else
Class ProgramName
{
Public static void main(String arg[])
{
System.out.println(“Main method started”);
Int x=5;

If(x>7)
{
System.out.println(“x is greater then 7”);
}
Else
{
System.out.println(“x is smaller then 7”);
}

System.out.println(“Main method ended);


}
}

Output
x is smaller then 7

3. Else if Ladder(if multiple condition exists)

Eg. of else if ladder


Class ProgramName
{
Public static void main(String arg[])
{
System.out.println(“Main method started”);
Int x=7;

If(x>7)
{
System.out.println(“x is greater then 7”);
}
Else if(x==7)
{
System.out.println(“x equal to 7”);
}

Else
{
System.out.println(“x is smaller then 7”);
}

System.out.println(“Main method ended);


}
}

Output

X is equal to 7
4. Nested if(if inside if)

Syntax

If(condition1)-----outer if
{
//outer if body statements

If(condition2)------inner if
{
//inner if body statements
}
Else
{
//inner else body statements
}

}
Else
{
//outer else body statements
}

NOTE: if condition1 is true then Outer if Body


statements will be executed other wise if
condition1 is false then Outer else body
statements will be executed

If condition 2 is true then inner if body statements


will be executed else inner else body statements
will be executed.

Eg of nested if

Case: client has to withdraw money from bank


Condition is he can withdraw only if the pin code
number is correct and amount is less then account
balance

Variables required

1.accBal
2.Amt
3.Pin

Class ProgramName
{
Public static void main(String arg[])
{
System.out.println(“Main method started”);
double accBal=20000.00;
double amt=2000.00;
Int pin=1234;

If(pin==1234)
{
System.out.println(“pin is correct please enter the
amount”);

If(amt<accBal)
{
accBal=accBal-amt;
System.out.println(“withdrawal successful”);
}
Else
{
System.out.println(“insufficient balance”);
}

}
Else
{
System.out.println(“pin is incorrect”);
}

System.out.println(“Main method ended);


}
}
Switch Statement(if multiple conditions exists)

Syntax

Switch(expression)
{
Case 1:
//statement
Break;

Case 2:
//statement
Break;

Case n:
//statement
Break;

Deafult:
statement
}

Eg. Of switch statement


Class ClassName
{
Public static void main(String arg[])
{
System.out.println(“main method started”);
Char Grade=’E’;

Switch(Grade)
{
Case ‘A’:
System.out.println(“First division”);
Break;

Case ‘B’:
Systement.out.println(“second division”);
Break;

Case ‘C’:
System.out.println(“third division”);
Break;

Case ‘D’
System.out.println(“you are failed”);
Break;

Case ‘E’:
System.out.println(“Get lost”);
Break;
Default:
System.out.println(“case mismatched”);

System.out.println(“main method ended”);


}
}

Looping

What is looping?

Answer: repeating the same task again and again


till the condition is true

Types of loops

1. For loop
2. While loop
3. Do while loop(do it yourself)
4. Advanced loop will be taught in the collection
framework topic

1. for loop syntax

For(initialization; condition;inc/dec)
{
//for loop body statements
}

Process of for loop

Eg. Of for loop

Print
hello java
5 times

Class ClassName
{
Public static void main(String arg[])
{

For(int I=0;I<5;I++)
{
System.out.println(“hello java”);
}

}
}
Output

Hello java
Hello java
Hello java
Hello java
Hello java

2. Assignment
Print as
12345
For(int I=1;I<=5;I++)
{
System.out.print(i+” ”);
}
3. Assignment
Print as
1
2
3
4
5
For(int I=1;I<=5;I++)
{
System.out.println(i);
}

4. Assignment
Print as
*****

For(int I=0;I<5;I++)
{
System.out.print(“*”);
}

5. Assigment
Print as
*
*
*
*
*

For(int I=0;I<5;I++)
{
System.out.println(“*”);
}

6. Assignment

Print as
*****
*****
*****
*****
*****

Class ClassName
{
Public static void main(String arg[])
{
For(int I=0;I<5;I++)
{
For(int j=0;j<5;j++)
{
System.out.print(“*”);
}
System.out.println()
}
}
}

7. Assignment
Print as
*****
* *
* *
* *
*****

Function

What is Function in java?


Answer: A set of java statements which are used to perform
some specific task is called as function/method

Types of functions
1. Built in functions(provided by the java language)
2. User defined functions(created by the programmer)

Part of function
There are two parts of functions
1. Declaration
2. Definition
Syntax of a function in java
<access specifier><modifier><return type>FunctionName(Arguments)//declaration
part
{

//definition of the function

Return parameter/output parameter;


}

NOTE: in the above syntax access specifier and modifier are


not mandatory also the arguments

But return type and functionName is mandatory

Okay?

For example

Create a function to calculate the area of Circle


Pi*r*r

Input parameters---------1(int)

Output parametrs---------1(float)

Float AOfcircle(int r)
{
Float pi=3.14f;

Float Area=pi*r*r;

System.out.println(“area of circle is”+Area);

Return Area;

Assignment(home)

1. Write a function to convert c to f.


2. Write a function to calculate the angle of a time (1:50) in an
analog watch
.

Eg. Of function

Class className
{
Public static void main(String args[])//main method
{
Display();//calling of function Hello
System.out.println(“we can call function any number of
times”);
Display();//calling of function Hello
}
Static void display()//function declaration(user defined
method)
{
System.out.println(“Hello”);//function definition
}
}

 Here in the above example there is a main method and one


user defined method in the class

 Where main method is a caller method which is calling the


display method

 And display method is a called method which is called in


the main method body.

Recursion

When a function calls itself is called as recursion function


NOTE: if the function is calling itself then the stack of JVM will
be overflow and it will generate stack overflow error

So if we re using recursion we must provide some conditions to


stop the function call .

NOTE: recursion are the best when based on condition and also
faster then normal function.

Example of Recursion(factorial example)

Class classname
{
Stactic Int fact(int n)
{
If(n==0)

Return 1;

Else
Return (n*fact(n-1));

Public static void main(String args[])


{
Int I, f=1;

Int num=5;

F=fact(num);

System.out.println(“factorial of the number is”+f);

}
}

array(only introduction)

What is an array
1. Array is a collection of similar
data
2. we have to declare the size of an
array before using it
3. It will store elements in an index
manner
4. The index ranges from 0 to size -1

There are two types of array

1. 1D array
2. 2D array

Declaration of the array

Syntax

Int arrayName[];//declared the


array
arrayName=new
int[size];//allocated the memory

NOTE: we can do declaration and


allocation of the memory of array in
the same line

Int arrayName[]=new int[5];

Initialization of array

arrayName[0]=10;
arrayName[1]=20;
arrayName[2]=30;

arrayName[3]=40;

arrayName[4]=50;
System.out.println(arrayName.lengt
h);//5

Example of array

Class ClassName
{
Public static void main(String)
{
//declaration of array
Int a[];
A=new int[5];

A[0]=10;
A[1]=20;

A[2]=30;

A[3]=40;
A[4]=50;

//print the forth element of the


array
System.out.println(a[3]);
//print the length of the array
System.out.println(a.length);//5

//print all the elements of the array


//using loop
For(int I=0;I<a.length;I++)
{
System.out.println(a[i]);
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy