JAVA
Programming Language
It is a set of instruction which is used to communicate with machine or computer
is known as Programming language
Types of Programming
Low Level Language
Mid Level Language
High Level Language
Low Level Language
The language which is easy to read and understand by the machine is known
as low level language
eg
Binary Language
Low Level language is difficult to understand by human beings to solve this
problem mid level language come into picture
Mid level languauge
The language which contains mnuemonics
Compare to Low Level Language ,Mid Level Language is understand by
human beings
Since Mid Level Language cannot be understand by computer we can install
Software as assembler Which will take the i/p as MDLL and converts into
Low Level Language
By using MDLL we cannot create a Application to solve this problem
High Level Language
The language which is similar to english is known high level language
It is easy to understand by the human begins when compared with mid level
language
eg
JAVA,Python,C,C++…
JAVA
• Java is a high level programming language and it was introduced by James
Gosling in the year 1995
• When he was working for sun microsystem company and this company was
acquired by create and hence ownership is shifted to Oracle company
• Using java we can create application
• Java Language Cannot be understand by machine
java 1 Page 1
• Java Language Cannot be understand by machine
• So we can install software like compiler or interpreter to talk with machine
Structure of java
class Classname{
Class is a component and it should be written in lower case
Every class have class name
Class name can be anything
As a programmer write the class name first letter in capital for undersatnd
Steps to write and run the java program
1. Open any one of the application -Notepad, VS code, Eclipse, Ms work ,
Editplus etc..
2. Write the java code according to the structure of java
3. save the file with .java extension
The file which contains java instruction is known as java file
4. Call the Compiler
Compiler is a software and the name of the compiler is javac
Compiler takes the input as java file and generate the output as Classfile
Compiler will generate class file if and only if the java file syntactically
and semantically correct
How to call compiler
Syntax
Javac [Link]
Where will to call compiler?
Comment Prompt
Comment prompt is a application we can read (compile) and execute
(run the java program) based on the your comment
Class File
The file Which contains byte code is known as class file
Bytecode
Byte code is the intermediate code(Somethings are readable Somethings
are not readable) and it cannot be understand by any one expect JVM
java 1 Page 2
are not readable) and it cannot be understand by any one expect JVM
5. Call JVM to convert byte code into low level language
Syntax
java Classname
This command has a python i.e Main method should be present in the
java file also we get RTE saying
Error: Main method not found in class Notes, please define the main
method as:
public static void main(String[] args)
or a JavaFX application class must extend [Link]
6. Main method
Main method is the starting point of execution and it is called by JVM
Syntax
class ClassName{
public static void main (String[] args){
}
}
CTE-COMPILE TIME ERROR
RTE-RUN TIME ERROR
Where all this process are carried out?
JDK software
JDK is a software and this JDK stands for java
Development kit
This JDk contains compiler JRE,JVM additional
development tools
If we install JDK in our system we can compile java file
and execute class file
JRE-architecture of java
► JRE is a software and JRE stands for JAVA RUNTIME
ENVIRONMENT
java 1 Page 3
ENVIRONMENT
► This JRE contains JVM,JIT,Interpreter etc…
► JRE Provides an environment (memory) to convert JVM
byte code into Low Level Language
► If we Install JRE software inside our system are we
cannot compile the java code
► but we can execute the code
JVM
► JVM stands for Java Virtual machine
► JVM contains JIT and interpreter it is only
component in java which can be understand bitecode
and converts its into low level language
JIT
► JIT stands for Just In Time compiler
► It is responsible to covert Bytecode
and Interpreter
► It executes byte code line by line
Features of java
► Java is a platform independent language
► It follow WORA principle (Write Once, Run Anywhere)
► Java is high secured Language
► Java support multi threading
► Java is robust
► Java is Strictly typed language
► Java is strongly typed language
Understanding platform dependent language
java 1 Page 4
Understanding platform dependent language
The code which is written and compiled in one
platform will not be executed in any other
platform for this we called this as platform
depended language
platform independent language
The code which is written and compiled in one
platform will be executed in any other supported
platform (JVM) that is called platform
independent language
Java is platform independent language but it is
dependent on jvm
NOTE
JAVA FOLLOW WORA that means Write Once Run Anywhere
Structure of java code
class ClassName{
public static void main (String[] args){
//STATEMENTS
}
}
Statements
► A computer instruction is known as statements
► It can be three types
1. Printing statement
2. Declaration statement
3. initialization statement
► Printing Statement
Printing statement are statements which is used
to print the data on the console (output shown).
Two most important statements
i) [Link]([data]);//data is optional
ii) [Link](data));//data is mandatory
eg
class Java{
public static void main(String[] args) {
[Link]("Hai Sanga!\t");
[Link]("how are you?");
[Link]();
[Link]("Hai Manga!\t");
[Link]("I am fine.");
java 1 Page 5
}
}
output
Hai Sanga! how are you?
Hai Manga! I am fine.
conclusion
◊ [Link]-
First it prints the data and after
printing it move the cursor to next line
◊ [Link]-
It prints the data and after printing the
cursor remains in the same line
Tokens
◊ Tokens are the basic building blocks of Java
programming language
◊ there are five types of tokens
1. Keywords
2. Identifiers
3. Separators
4. Literals
5. Operators
◊ Keywords
Keyword are the predefined words of java
programming language
Each and every keywords will be having
their own meaning
being a programmer we can use the keywords
we cannot modify the meaning of the
keywords
we should represent keywords in lower case
we are having 49+ keywords
keywords are also called as reserved words
we are having keywords such as public ,
static , void , final , abstract ,
private , byte , short.
◊ Identifiers
In java we are having components such as
class variables methods interface enum
called as identifiers
java 1 Page 6
called as identifiers
For the above java identifiers we can
assign the names inorder to identify
Rules of identifiers
– we cannot start with numbers but we
can use numbers in between or last
– Except dollar and underscore no often
special character is allowed
– space between the letters are not
allowed
– keywords cannot be named for the
identifiers
◊ Separators
We use separators to separate java
instruction
we are having separators such as
{},[],(),…,:,;etc…
◊ Literals
In Java, a Literal is a value of boolean, numeric,
character, or string data. Any constant value that can
be assigned to the variable is called a literal.
Types of Literals
1. Integer data / Literal
eg: 6 ,53
2. Floating data / Literal
eg: 6.3 ,53.56
3. Character data / Literal
eg: 'S','$'-->cts
'SA','$#'-->cte
4. String data / Literal
eg: "SANGA","MANGA"
5. Boolean data / Literal
eg: true (or) false
WAJP to print the all literals
class Java{
public static void main(String[] args) {
[Link]("Literals");
[Link]("Integer Literals:
" + 100);
[Link]("Floating-point
Literals: " + 3.14);
java 1 Page 7
Literals: " + 3.14);
[Link]("Character
Literals: " + 'S');
[Link]("String Literals:
" + "SANGA");
[Link]("Boolean Literals:
" + true);
}
}
OUTPUT
Literals
Integer Literals: 100
Floating-point Literals: 3.14
Character Literals: S
String Literals: SANGA
Boolean Literals: true
► Variables
Variables are the containers to store the data
► Datatypes
Datatype helps to understand what to store inside
the variable
Integer
◊ byte
◊ short
◊ long
◊ int
Floating
◊ float
◊ double
Character
◊ char
String
Boolean
► WAJP to store and print student information
class Java{
public static void main(String[] args) {
int id=10;
float percenatge=96.9F;
long ph=9894127532L;
java 1 Page 8
long ph=9894127532L;
char grade='A';
String name="Sanga";
boolean isTopper=true;
[Link]("Id:"+id);
[Link]("Percentage:"+percenatg
e);
[Link]("Phone:"+ph);
[Link]("Grade:"+grade);
[Link]("Name:"+name);
[Link]("Is Topper:"+isTopper);
}
}
OUTPUT
Id:10
Percentage:96.9
Phone:9894127532
Grade:A
Name:Sanga
Is Topper:true
► Classification of datatypes
We can classify datatypes into two types based on
the number of values that variable can accept
Note
□ In java we are having only 8 primitive datatypes but
n number of non-primitive datatypes(because we can
create many objects).
what is primitive data type ?
□ Any variable or memory having capacity to store
single values should be represented by primitive
datatype.
□ in java we are having 8 primitive data types which
java 1 Page 9
□ in java we are having 8 primitive data types which
is mentioned above
NOTE
whenever we execute the java program internally jvm
will create 3 threads that is
threat |
main |-->Schedular
garbage collector |
Important statement
Datatype Variable_name; --->Declaration statement
Variable_name=value/exp;--->Initialization statement
eg
class Java{
public static void main(String[] args) {
int a; //Declaration statement
a=6; //Initialization statement
[Link]("a= " + a); //Print
statement
String name="Sanga"; //Declaration and
initialization statement
[Link]("name= " + name);
//Print statement
}
}
Output
a= 6
name= Sanga
What is global area and global variable ?
the area present inside the class block is called as global area
Any variable which is declared or initialized in the global area
is called global variable
Note
◊ Global variable can be used by all the methods
SYNTAX
class ClassName {
//global areas
[static] datatype variableName ;//global
variable with declaration
[static] datatype variableName =
value ;//global variable with initialization
java 1 Page 10
value ;//global variable with initialization
public static void main(String[] args) {
}
}
What is local area and local variable?
□ the area present inside the method block is called
as local area
□ Any variable which is declared or initialized in
local area is called local variable
Note
► Local variable can be used only by that method where
the variable is present
► eg
class ClassName {
public static void main(String[] args) {
datatype variableName ;//local variable with
declaration
datatype variableName = value ;//local
variable with initialization
//Local area
}
}
► can we use static variable inside the local area
no,we cannot use static keyword inside the local
area
► Note
We can have local variables declared in but we
cannot use it without initialization.
Local variable do not have default value
► WAJP to print the default values of primitive
datatypes and string
class Java{
static byte a;
static short b;
static int c;
static long d;
static double f;
static char e;
static float g;
static String h;
public static void main(String[] args) {
[Link]("Defalut value of Primitive data types
and String");
[Link]("byte "+a);
[Link]("Short "+b);
java 1 Page 11
[Link]("Short "+b);
[Link]("Int "+c);
[Link]("Long "+d);
[Link]("double "+f);
[Link]("char "+e);
[Link]("float "+g);
[Link]("String "+h);
}
}
► output
byte 0
Short 0
Int 0
Long 0
double 0.0
char
float 0.0
String null
RANGE DIAGRAM
Typecasting
The process of converting one data type to
another datatype is called as typecasting
Based on the type of conversion we can classify
typecasting into 2 types
java 1 Page 12
typecasting into 2 types
Primitive type casting
► The process of converting one primitive datatype to
another primitive datatype is called as primitive
type casting
► Here we are having two types
Widening
Narrowing
► What is widening ?
The process of converting lower range primitive
datatype to higher range primitive datatype is
called widening.
It is a compile time success (it is implicity
possible)
WAJP FOR WIDENING
class Java{
public static void main(String[] args) {
byte a=63;
[Link]("Byte ="+a);
short b=a;
[Link]("Short ="+b);
java 1 Page 13
[Link]("Short ="+b);
int c=b;
[Link]("Int = "+c);
long d=c;
[Link]("Long ="+d);
float e=d;
[Link]("Float ="+e);
double f=e;
[Link]("double ="+f);
char g='s';
[Link]("Char ="+g);
int i=g;
[Link]("char-->int ="+i);
}
}
OUTPUT
Byte =63
Short =63
Int = 63
Long =63
Float =63.0
double =63.0
Char =s
char-->int =115
Narrowing
The process of converting higher range PDT to lower range
PDT is known as Narrowing
It is compile time error
We can narrowing success by using typecast operator
(narrowing is possible only explicity)
Typecast operator
It is unary operator
it helps to achieve narrowing
Syntax
(type) variable_name;
WAJP for Narrowing
class Java{
public static void main(String[] args) {
double a=63.98;
java 1 Page 14
double a=63.98;
[Link]("Double ="+a);
float b=(float) a;
[Link]("double -->float =
"+b);
long c=(long)b;
[Link]("Float -->Long "+c);
int d=(int)c;
[Link]("Long -->Int "+d);
char e=(char) d;
[Link]("Int -->Char "+e);
short f=(short) d;
[Link]("Int -->Short "+f);
byte g=(byte) f;
[Link]("Short -->Byte "+g);
}
}
OUTPUT
Double =63.98
double -->float = 63.98
Float -->Long 63
Long -->Int 63
Int -->Char ?
Int -->Short 63
Short -->Byte 63
Arithmetic Operator
It is a binary operator
we are having following arithmetic operators
Additional operator "+"
◊ It is a binary operator
java 1 Page 15
◊ It is a binary operator
◊ It is having 2 special behaviour that is
Addition
Concatenation
Expression table for + operator
LHS OPERATOR RHS RESULT
byte + byte int
short + short int
int + int int
long + long long
float + float float
double + double double
char + char int
boolean + boolean complition error
string + String String
value + String String
String + value String
class Java{
public static void main(String[] args) {
[Link]("Java"+ 10 + 20);
[Link]("Java"+ (10 + 20));
[Link](10 + 20 + 30 +"Java"+
10 + 20);
[Link](10 + 20 + 30 +"Java"+
(10 + 20));
[Link](10 + 20 + 30 +"Java"+
10 + 20+"java"+(10+53));
}
}
OUTPUT
Java1020
Java30
60Java1020
60Java30
60Java1020java63
ASSIGNMENT
Assume Ramesh was initially having 2000 rupees in his wallet
he deposit 300 rupees to his wallet Create the program to store
java 1 Page 16
he deposit 300 rupees to his wallet Create the program to store
the data and print data
SUBTRACTION OPERATOR "-"
◊ It is binary operator
LHS OPERATOR RHS RESULT
byte - byte int
short - short int
int - int int
long - long long
float - float float
double - double double
char - char int
boolean - boolean CTE
String - String CTE
Multiplication "*"
◊ It is the binary operator
◊ We use this operator to perform
multiplication
Division "/"
◊ It is a binary operator
◊ We use this operator to perform division and
get the answer in quotient
◊ We can represent m/n as below
m/n
eg
73/6=12
Quotient will be the answer
Modulus "%"
◊ We use modulus operator to get the answer as
a reminder
◊ we can represent m%n as given below
eg
73%6=1
Reminder will be the answer
java 1 Page 17
eg
class Java{
public static void main(String[] args) {
int a=6;
int b=3;
[Link]("\t\tARITHMETIC
OPERATORS");
[Link](a+"+"+b+"="+(a+b))
;
[Link](a+"-"+b+"="+(a-
b));
[Link](a+"*"+b+"="+(a*b))
;
[Link](a+"/"+b+"="+(a/b))
;
[Link](a+"%"+b+"="+(a%
b));
}
}
• Increment & Decrement Operator
• Increment
▪ we use this operator to increase or decrease the value of
the variable by one unit.
▪ we are having two types in increment operator as given
below
▪ pre increment ++variable
here we increase the value of the variable by one unit
before use the variable
int a=63
► ++a //64
▪ post increment variable++
here we increase the value as the variable after using
the variable
► a++//
eg
java 1 Page 18
class Java{
public static void main(String[] args) {
int a=6;
int b=3;
[Link]("\t\tIncrement and
decreament OPERATORS");
[Link]("a = "+a);
[Link]("a++ = "+(a++));
[Link]("After a++ = "+a);
[Link]("b = "+b);
[Link]("++b = "+(++b));
[Link]("a = "+ a);
[Link]("a-- = "+(a--));
[Link]("After a-- = "+a);
[Link]("b = "+b);
[Link]("--b = "+(--b));
}
}
Output
Increment and decreament OPERATORS
a=6
a++ = 6
After a++ = 7
b=3
++b = 4
a=7
a-- = 7
After a-- = 6
b=4
--b = 3
Relational operator
▪ It is a binary operator
▪ we use relational operator to compare the values
▪ The result of relational operator will be true or false
▪ we are having the following relational operator given below
java 1 Page 19
eg
class Java{
public static void main(String[] args) {
int a=6;
int b=3;
[Link]("\t\tRelational operator");
[Link](" a =="+" b = "+(a==b));
[Link](" a !=+ b = "+(a!=b));
[Link](" a > b = "+(a>b));
[Link](" a < b = "+(a<b));
[Link](" a =< b = "+(a<=b));
[Link](" a => b = "+(a<=b));
}
}
OUTPUT
Relational operator
a == b = false
a !=+ b = true
a > b = true
a < b = false
a =< b = false
a => b = false
LOGICAL OPERATOR
It is used for the decision making
we are having 3 types of logical operators
java 1 Page 20
Assignment operator
It is the combination of arithmetic and assignment operator
We are having the following assignment operator
eg
class Java{
static void main() {
int a=63;
[Link]("a = "+a);
[Link]("a += 6 :: a = a+6 :: "+(a+=6));
[Link]("a = "+a);
[Link]("a -= 6 :: a = a-6 :: "+(a-=6));
[Link]("a = "+a);
[Link]("a *= 6 :: a = a*6 :: "+(a*=6));
[Link]("a = "+a);
[Link]("a /= 6 :: a = a/6 :: "+(a/=6));
[Link]("a = "+a);
[Link]("a %= 6 :: a = a%6 :: "+(a%=6));
}
}
OUPUT
a = 63
a += 6 :: a = a+6 :: 69
a = 69
a -= 6 :: a = a-6 :: 63
a = 63
a *= 6 :: a = a*6 :: 378
java 1 Page 21
a *= 6 :: a = a*6 :: 378
a = 378
a /= 6 :: a = a/6 :: 63
a = 63
a %= 6 :: a = a%6 :: 3
CONDITIONAL OR TERNARY OPERATOR
Syntax
Condition?Statement1_if_true:Statement2_if_false;
• the result of the conditional operator is depends on operand1
and operand 2
• eg
class Java{
static void main() {
int age1=21;
String a= (age1>18)?"eligible for vote":"not
eligible for vote";
[Link](a);
int age2=6;
[Link]((age2>18)?"eligible for
vote":"not eligible for vote");
}
}
• Output
eligible for vote
not eligible for vote
• NOTE
x?y:z;
▪ If x is true then answer is y if x is false then the answer is z
▪ if we store the result maintain y and z as homogenous
▪ if we print it can be heterogenous or homogenous
• Dynamic Reading
The process of taking the input from the user is called as dynamic reading
we can achieve dynamic reading with the help of scanner
Scanner class is a built in class present inside java library
Steps to achieve dynamic reading
I. Bring Scanner from java library to editor
we should type in 1st line as
import [Link];
(or)
import [Link].*;
II. built one house (object )for Scanner
static Scanner sc=new Scanner([Link]);
java 1 Page 22
static Scanner sc=new Scanner([Link]);
III. Display message using print statement and ask user to enter the
data
IV. Read the data using Scanner methods
Scanner methods for all data types
[Link]()-->BYTE
[Link]()-->SHORT
[Link]()-->INT
[Link]()-->LONG
[Link]()-->FLOAT
[Link]()-->DOUBLE
[Link]()-->BOOLEAN
eg
import [Link].*;
class Java{
static Scanner sc=new Scanner([Link]);
public static void main(String [] args) {
[Link]("Enter the value byte : ");
byte a=[Link]();
[Link](a);
[Link]("Enter the value short : ");
short b=[Link]();
[Link](b);
[Link]("Enter the value int : ");
int c=[Link]();
[Link](c);
[Link]("Enter the value long : ");
long d=[Link]();
[Link](d);
[Link]("Enter the value float : ");
java 1 Page 23
[Link]("Enter the value float : ");
float e=[Link]();
[Link](e);
[Link]("Enter the value double : ");
double f=[Link]();
[Link](f);
[Link]("Enter the value char : ");
char g=[Link]().charAt(0);
[Link](g);
[Link]("Enter the value boolean : ");
boolean h=[Link]();
[Link](h);
}
}
OUPUT
Enter the value byte : 6
6
Enter the value short : 3
3
Enter the value int : 63
63
Enter the value long : 989412753
989412753
Enter the value float : 63.9
63.9
Enter the value double : 63.98
63.98
Enter the value char : S
S
Enter the value boolean : true
true
Control Flow Statements
• it is used to control the execution flow of the program
• Here we are having two types of control statements that is
1. Decision making
2. helping statement
• Decision making statement
○ It helps to decide whether it executes or skip
○ we are having two types 4 types of decision making
statements
if
if-else
java 1 Page 24
if-else
if-else-if
switch-statement
○ if statement
Syntax
if(condition){
Statement
}
Working
if(true) if(false)
{ {
executed skip
} }
Case1:-
class Java{
public static void main(String [] args) {
[Link]("START");
if(true){
[Link]("Sanga");
[Link]("Manga");
}
[Link]("END");
}
}
OUTPUT
START
Sanga
Manga
END
case 2:-
class Java{
public static void main(String [] args) {
[Link]("START");
if(false){
[Link]("Sanga");
[Link]("Manga");
}
[Link]("END");
}
}
OUTPUT
START
END
java 1 Page 25
END
○ If-else Statement
Syntax
if(condition){
Statement 1;
}
else{
Statement 2;
}
WORKING
Case1:-
if(true)
{
Execute
}
else
{
Skip
}
Case2:-
if(false)
{
Skip
}
else
{
Execute
}
eg
import [Link].*;
class Java{
public static void main(String [] args) {
Scanner sc =new Scanner([Link]);
[Link]("Enter the year : ");
int year=[Link]();
if(year%4==0 && year%100!=0 || year%400==
0){
[Link](year+" is a leap
year");
}
else{
[Link](year+" not is a
leap year");
}
java 1 Page 26
}
}
}
○ else-if ladder
SYNTAX
if(condition)
{
Statement 1;
}
else if(condition)
{
Statement 2;
}
.
.
.
else if(condition)
{
Statement 2;
}
else
{
Statement 3;
}
note
in else-if ladder else block is optional
○ Switch
We use switch statement to match switch input to case
Syntax
switch(value/variable/exp){
case value/exp:{
Statement;
[break;]
}
case value/exp:{
Statement;
[break;]
}
.
.
.
default:{
Statement;
[break;]
java 1 Page 27
[break;]
}
}
NOTE
For Switch we cannot pass long,float,double and
boolean type of data
Default block is optional for switch
what is break?
it is a keyword
it is also called as control transfer statement.
it helps to transfer the control outside the switch
blocks outside the loop
We can use break statement inside the switch or loop
eg
import [Link].*;
class Java{
public static void main(String [] args) {
Scanner sc =new Scanner([Link]);
[Link]("Enter the number : ");
switch([Link]()){
case 1:{
[Link]("monday");
break;
}
case 2:{
[Link]("tuesday");
break;
}
case 3:{
[Link]("wednesday");
break;
}
case 4:{
[Link]("thursday");
break;
}
case 5:{
[Link]("friday");
break;
}
case 6:{
[Link]("saturday");
java 1 Page 28
[Link]("saturday");
break;
}
case 7:{
[Link]("sunday");
break;
}
default :{
[Link]("invalid");
break;
}
}
}
}
OUTPUT
Enter the number : 6
Saturday
WAJP to generate otp
class Java{
public static void main(String [] args){
int otp=(int)([Link]()*99999+99999);
[Link](otp);
}
}
output
636624
• METHOD
• What is method or function?
○ It is a block of instruction which is used to perform
particular task
○ SYNTAX for creating the method
[ACCESS MODIFIERS][MODIFIER]return_type method_name([formal
arg])
○ NOTE
here access modifier ,modifier and formal argument are
optional.
○ We are having two types of method
NO argument method
parameterized method
java 1 Page 29