Del java day 2
Del java day 2
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)
Topic 1
java components(javac, jvm, jre)
JDK(java development kit(javac+jre))
JAVA editions
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
1. Eclipse(most recommended)
2. Netbeans
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
//Java statements;
{
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”,”**@#)”
Class Program1
{
Public static void main(String arg[])
{
Desktop>javac Program1.java
Desktop>java Program1
Topic 4. Variables and operators
Variable?
Declaration of varibale
Syntax
Data_type identifier;
Eg.
Int id;
Data_type represents the type of value is to be
stored in the variable
1. Byte
2. Short
3. Int
4. Long
5. Float
6. Double
7. Char
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;
Id=12;
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;
System.out.println(empId);
System.out.println(EmpName);
System.out.println(EmpSalary);
}
}
OutPut
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
System.out.println(“Ramesh\
nKumar\nSharma”);
4. Assignment
Print as
1 tab=4 spaces
5. Assignment
Print as
I am a “software” engineer
System.out.println(“I am “software”engineer”);//error
Correct way is
+ (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
Not(!)
Note : we will get opposite result to
the input
OKay guys?
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:
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;
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
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
1. If
Syntax
If(condition)
{
// if body statements
}
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”);
}
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”);
}
Output
x is smaller then 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”);
}
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
}
Eg of nested if
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”);
}
Syntax
Switch(expression)
{
Case 1:
//statement
Break;
Case 2:
//statement
Break;
Case n:
//statement
Break;
Deafult:
statement
}
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”);
Looping
What is looping?
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
For(initialization; condition;inc/dec)
{
//for loop body statements
}
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
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
{
Okay?
For example
Input parameters---------1(int)
Output parametrs---------1(float)
Float AOfcircle(int r)
{
Float pi=3.14f;
Float Area=pi*r*r;
Return Area;
Assignment(home)
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
}
}
Recursion
NOTE: recursion are the best when based on condition and also
faster then normal function.
Class classname
{
Stactic Int fact(int n)
{
If(n==0)
Return 1;
Else
Return (n*fact(n-1));
Int num=5;
F=fact(num);
}
}
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
1. 1D array
2. 2D array
Syntax
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;