Jav Appt
Jav Appt
Hardware software
• Lastly the java is inspired from java coffee bean island in indonesia in 1995.
Stable version is released in 1996.
• Who is the father of java?
James gasling
• Mid-level Languages:
it is in form English and numbers English is easy but it is confusing in numbers.
• High-level Languages:
in high level languages are written simple English & symbols. it is understand by
humans.
Concatenation :
• Combining or merging two or more data is called
concatenation
• To achieve concatenation by using + symbol
• System.out.println(10+100);
• System.out.println(100 + ‘a’);
• System.out.println(“hello” + 100);
Java Architecture
Compiler
JRE
Jdk
+
Classes
& BL/
Predefin
Source packages ML/
ed Byte code
code HL
syntaxs
JVM
Class loader
+
Byte code
verifier
+
Interpreter
+
JITC
Compilation
Execution
JRE ( Java Runtime Environment):
• It provides runtime environment in java .it consist of set libraries,for any
Java program execution minimum required is jre .
Primitive datatypes:
primitive datatype are predefined datatypes. They are 8
primitive datatypes those 8 are our keywords.
Non-primitive datatypes:
Non-primitive datatype are called as classtypes .
Ex :String,arrays
Number
byte – 1 byte – 8 –bits.
short - 2 bytes – 16 bits
int 4 bytes – 32 bits
long 8 bytes 64 bits
float 4 bytes – 32 bits
double 8 bytes – 64bits
char 2 bytes – 16 bits
boolean 1 bit
Variables
Variables are memory blocks.
Variable initialization:
Syntax: variable = value/data;
Ex : name = “nation”;
rollno = 240;
grade = ‘a’;
Variable utilization:
System.out.println(name);
System.out.println(name);
System.out.println(name);
Example:
Class Demo
{
Public static void main(String[] args)
{
String name;//declaration
int roll no;
char grade;
name = “nation”;//initialization
rollno = 240;
grade = ‘a’;
System.out.println(name);
System.out.println(rollno);
System.out.println(grade); 0lp:
Nation
240
a
}
}
Reinitialization
Where user or programmer have to change the existing value in the memory block then the user
have to go for reinitialization.
Class Demo
{
Public static void main(String[] args)
{
String name;
int roll no;
char grade;
name = “abc”;
rollno = 240;
grade = ‘a’;
System.out.println(name); //abc
System.out.println(rollno); //240
System.out.println(grade); //a
name = “def”;
System.out.println(name); //def
}
}
Final keyword:
When user or programmer have to fix the value for any variable then the user have to declare
final keyword at the of variable declaration.
Ex
Class Demo
{
Public static void main(String[] args)
{
final String name = “abc”;
int roll no = 270;
System.out.println(name);
System.out.println(rollno);
}
}
Note: once we declare variable as final keyword we cannot change the value if you try to change
the value you get compile time error.
Operators
• Operators are special symbols which is used to perform some specific task
(or) operation
1. Arithmatic operator.
2. Assignment operator.
3. Relational/comparison operator.
4. Logical operator.
5. Ternary/conditional/miscellaneous operation
6. Bitwise operator
7. Unary operator
Arithematic operators:
• + it performs addition
• - it performs subtraction.
• * it performs multiplication
• % remainder
• / Quotient
Class Demo
{
Public static void main(String[] args)
{
System.out.println(10+100); // 110
System.out.println(100-10); //90
System.out.println(4*10); // 40
System.out.println(100%10); //0
System.out.println(24/2); //12
}
}
Assignment Operator:
Assignment Operator is used to assign the value to the variable by using ‘ = ‘ symbols.
Class Demo
{
Public static void main(String[] args)
{
int a = 30;
int b = 20;
a+=10;
b-=10;
System.out.println(a);
System.out.println(b);
}
}
Relational Operators / Comparison Operator
• it is used to compare two operands when we perform relational operator resultant output
will be Boolean.
• < = less than
• > = grater than
• <= less than or equal
• >= grater than or equal
• == = equals
• != = not equals to
Class Demo
{
Public static void main(String[] args)
{
int n1 = 10;
int n2 = 20;
System.out.println(n1> n2); //false
System.out.println(n1<n2); //true
System.out.println(n1<=n2); //true
System.out.println(n1>=n2); //false
System.out.println(n1==n2); //false
System.out.println(n1!=n2); //true
}
}
Logical operators
Logical operators can be defined as a type of operators that help us to combine multiple conditional
statements. There are three types of logical operators in Java: AND, OR and NOT operators.
class Main {
public static void main(String[] args) {
// && operator
System.out.println((5 > 3) && (8 > 5)); // true
System.out.println((5 > 3) && (8 < 5)); // false
// || operator
System.out.println((5 < 3) || (8 > 5)); // true
System.out.println((5 > 3) || (8 < 5)); // true
System.out.println((5 < 3) || (8 < 5)); // false
// ! operator
System.out.println(!(5 == 3)); // true
System.out.println(!(5 > 3)); // false
Condition1 Condition2 Condition1 && Condition2
Types:
1. Decision making statements.
2. Jumping statements.
3. Looping statements.
Types:
4. if
5. if else
6. elseif
7. Nested –if
if statement :
class Student {
public static void main(String[] args)
{
int x = 10;
int y = 12;
if((x+y ) > 20)
{
System.out.println("x+y”+ is greater than 20");
}
}
}
if-else statement:
Syntax:
if(condition) {
statement 1; //executes when condition is true
}
else{
statement 2; //executes when condition is false
}
Example:
class Student {
public static void main(String[] args) {
int x = 10;
int y = 12;
if(x+y < 10) {
System.out.println("x + y is less than 10");
}
else {
System.out.println("x + y is greater than 20");
}
}
}
if-else-if ladder:
Whenever a certain set of code want to execute for several no of times or n no of times
then the user or programmers have to perform looping statement
Types of looping :
• While
• Do-while
• For
• For each
While:
When the no of iteration is not fixed user will use while
Syntax: while(condition)
{
}
do-while:
• When the no of iterations is not fixed user will use do-while
Syntax:
do
{
}
while(condition);
Class Looping
{
do
{
system.out.println("program start");
a++;
Differnce between while and do while
While Do-while
1. While block it will check 1.It will execute the statement first
condition first and statement then condition will check
will get executed
2.The min iteration is one
2. The min iteration is zero
3. We have to use semicolon(;) while
3.We cannot use semicolon(;) while declaration in do while block
declarartion in while block
4. If we are forget to use (;) in
4.If we are using (;) in declaration declaration of do while block will get
of while block will get infinite loop compile time error.
For loop
• When the no of iterations are fixed then we have to use for loop . It is bi-directional.
Syntax:
• for(initialization ; condition; increment/decrement)
Example:
public class ForExample
{
public static void main(String[] args)
{
for(int i=1;i<=10;i++)
{
System.out.println(i);
}
}
}
For-each loop:
Syntax:
• Syntax:
• Switch(expression)
{
case 1 :
case 2:
Break :
• The Java break statement is used to break loop or switch statement. It breaks
the current flow of the program at specified condition. In case of inner loop, it
breaks only inner loop.
• We can use Java break statement in all types of loops such as for loop,
while loop and do-while loop.
Syntax:
Jumping-statement;
Break;
Example:
class Switch
{
public static void main(String[] args)
{
Int i = 1;
switch(i)
{
case 1 : System.out.println("case 1");
break;
case 2: System.out.println("case 2");
break;
case 3: System.out.println("case 3");
break;
case 4: System.out.println("case 4");
}
}
Default: Default is a keyword whenever all the cases/options were not
matching at the time default statement will be executed.
Example:
class Switch
{
public static void main(String[] args)
{
Int i = 1;
switch(i)
{
case 1 : System.out.println("case 1");
break;
case 2: System.out.println("case 2");
Default : System.out.println(“options are not matching “);
}
Scanner class:
• Scanner class is used to read the value from the user during the runtime of the program.
• It is present in java util package.
• If you want to use scanner class in our program we have to write two major statements.
class Switch
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("enter first number");
int i = scan.nextInt();
System.out.println("enter second number");
int j = scan.nextInt();
Int k = i+j;
System.out.println(k);
}
}
Casting:
Class Casting
{
public static void main(String[] args)
{
double d = 10;
System.out.println(d);
}
}
a. explicit: converting one primitive datatype into another
primitive datatype by the user or programmer is called explicit
casting.
• There is a loss of conversion so it is called narrowing
• Narrowing: converting larger datatype into smaller datatype is
narrowing.
• Example:x
Class Casting
{
public static void main(String[] args)
{
int d = (int) 10.34;
System.out.println(d); //10
Difference b/w SOP & SOPln
Sop sopln
• After printing the statement it After printing the statement it
goes to next space. goes to next line
• Example:
Class Hacker
{
public static void main(String[] args)
{
int a = 10;
int b = 20; (a+b)
System.out.println(a+b); A
int a = 23;
int b = 10; (a-b)
System.out.println(a-b);
Structured way of programming:
• Writing the code once and executing the same piece of multiple times is called structured way of
programming.
• Example
Class Hacker
{
public static void main(String[] args)
{
public static void m1()
{
int a = 10;
int b = 20; (a+b)
System.out.println(a+b);
}
Object:
A Java object is a member (also called an instance) of a Java class. Each
object has an identity, a behavior and a state.
Class
{
State // state is also called data members
&
Behaviour // it is also called as function members/ methods.\
}
Static Members:
• Syntax: object.membername;
Example:
Class Hacker
{
int a = 12;
public void test()
{
System.out.println(“non static method”);
System.out.println(a); // directly access the static variable
}
public static void main(String[] args)
{
new Hacker().test();
Example:
Class Hacker
{
int a = 12;
public void test()
{
Constructor :
• Constructors are used to initialize the non static variables of the class.
• Constructor is called or invoke when object is created
• At the time of calling the constructor non static members are loaded inside
the object.
Example
Class Sample
{
int a = 12;
Sample()
{
Types of constructors:
1. Default constructors
2. User defined constructor
a. User defined parameterized constructor
b. User defined non - parameterized constructor
Constructor Overloading:
Developing multiple constructor with in the same class by passing different arguments list is called as
constructor overloading. It will follow the polymorphism principle.