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

Jav Appt

Uploaded by

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

Jav Appt

Uploaded by

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

computer

Hardware software

System s/w application s/w


• Mandatory
eg: os : windows,linux optional apps
Unix,mac etc.,

Note: operating system is acts as interface b/w user & computer


introduction to java
What is Java?
Java is a object oriented and secure programming language, created in 1995.
it is owned by Oracle, and more than 3 billion devices run Java.
it is used for:
•Mobile applications (specially Android apps)
•Desktop applications
•Web applications
•Web servers and application servers
•Games
•Database connection
Why Use Java?
•Java works on different platforms (Windows, Mac, Linux, Raspberry Pi,
etc.)
•it is one of the most popular programming language in the world
•it has a large demand in the current job market
•it is easy to learn and simple to use
•it is open-source and free
•it is secure, fast and powerful
•it has a huge community support (tens of millions of developers)
•Java is an object oriented language which gives a clear structure to
programs and allows code to be reused, lowering development costs
•As Java is close to C++ and C#, it makes it easy for programmers to switch
to Java or vice versa
Application Software
Standalone applications:
• it is not depend on network.
Eg: ms office , calender , clock etc
• Web app:
• it is depend on network.
eg:chrome etc.,
History of Java
• in America there was company called sun micro system. They are working
on setup box project but they faced so many problems major issue is code
reusability problem.
• So that they form a new group and develop a new language in 1991 and
named it as “OAK”.
• Because some license issues again renamed as “green”.

• 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

• What is the latest version of java?


 Java SE 20 or jdk 20
PLATFORMS OF JAVA
• j2SE (java to standard edition) :
it is not depending on network.
ex: calculator , calender, clock core java etc.

• J2EE (java to enterprise edition) :


it is depending on network.
ex: all web apps,advance java.

• J2me (java to micro edition) :


it is used to develop mobile applications.
ex: keypad phones.
Languages
• Low-level Language:
it is understand by only computer it is in the form of 0’s and 1‘s.

• 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.

 Problem: it is not understand by computer


 Solution : Translators
Translators
Compiler:
• compilers translates the code from one level to another level at a
time in one shot.
• This process is fast.
ex: c, c++ .
Interpreter:
• interpeter translates the code from one level to anotherby line by
line.
• This process is slow
ex: python.
Features of java
• Simple
• Secure
• Object Oriented
• Platform independent
• Portable
• Robust
• High performance
Tokens :
• Keywords
• Identifiers
• Literals
• Separators
• Concatenation
 INDENTIFIER:

• Identifiers: Identifiers are the name given to java


components .(class,methods,variables)
• Identifier rules:
• It contains alphabets and numbers
• It cannot start with numbers
• Symbols are not allowed except $ , _
• Keywords you cannot use as identifiers.
 Literals:
literals are nothing but data or values
• They are four types:
1. numbers :a. integers(ex:1,2,3,5,34)
b. Decimal(ex:2.3,5.67)
2. character: ex: ’e’, ’6’ ,’&’ ….
3.String: ex: “hello”
4.Boolean : true , false
 Separators:
• {} : it will separate blocks
• ; : it will separate statements.

 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 .

 JDK (Java Development Kit ):


• The JDK is a collection of software tools that you can use for developing
Java applications.

 JVM(java Virtual Machine):


• The JVM is software that runs the Java program line by line.
Datatypes
 Data type is nothing but which type of data need to be stored.

 Types of datatypes : 1. primitive datatypes.


2. Non-primitive datatypes.

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.

Characteristics of variables: size - 4bytes (based upon the datatype)


1. Name
2. Type int a = 10 ;
3. Size variable values
4. Value
datatype variable name

Variables involved in 3 steps:


5. Variable declaration
6. Variable initialization
7. Variable utilization.
Variables involve in 3 steps:
Variable declaration:
Syntax: datatype variable name;
Ex : String name;
int roll no;
char grade;

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.

if you perform logical operators resultant will be Boolean 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

TRUE TRUE TRUE

FALSE TRUE FALSE

TRUE FALSE FALSE


FALSE FALSE FALSE

Condition1 Condition2 Condition1 OR Condition2

TRUE TRUE TRUE

FALSE TRUE TRUE

TRUE FALSE TRUE

FALSE FALSE FALSE


Ternary Operator/conditional/Miscalleneous
Syntax:
1.variable = (condition) ? expression1 : expression2

public class TernaryOperatorExample


{
public static void main(String args[])
{
int x, y;
x = 20;
y = (x == 1) ? 61: 90;
System.out.println("Value of y is: " + y);
y = (x == 20) ? 61: 90;
System.out.println("Value of y is: " + y);
}
}
Bitwise Operator
• Bitwise exclusive OR (^)

public class BitwiseXorExample


{
public static void main(String[] args)
{
int x = 9, y = 8;

System.out.println("x ^ y = " + (x ^ y));


}
}
Bitwise inclusive OR (|)

public class BitwiseinclusiveOrExample


{
public static void main(String[] args)
{
int x = 9, y = 8;

System.out.println("x | y = " + (x | y));


}
}
Bitwise Complement (~)

public class BitwiseComplimentExample


{
public static void main(String[] args)
{
int x = 2;
System.out.println("~x = " + (~x));
}
}
Right Shift Operator (>>)

public class SignedRightShiftOperatorExample


{
public static void main(String args[])
{
int x = 50;
System.out.println("x>>2 = " + (x >>2));
}
}
Left Shift Operator (<<)
public class SignedLeftShiftOperatorExample
{
public static void main(String args[])
{
int x = 12;
System.out.println("x<<1 = " + (x << 1));
}
}
Unary Operator

• Pre –increment : ++a


• P0st-increment: a++
• Post-decrement : a--
• Pre-decrement: --a

public class OperatorExample


{
1.public static void main(String args[])
{
int x=10;
System.out.println(x++);//10 (11)
System.out.println(++x);//12
System.out.println(x--);//12 (11)
System.out.println(--x);//10
}
}
Control statement

• it is used to control the execution flow of the program

Types:
1. Decision making statements.
2. Jumping statements.
3. Looping statements.

Decision making statements: it is used to make a decision whether the


particular set of code want to execute or not.

Types:
4. if
5. if else
6. elseif
7. Nested –if
if statement :

• it is the most basic statement among all control flow


statements in Java. it evaluates a Boolean expression and
enables the program to enter a block of code if the
expression evaluates to true.
• Syntax of if statement is given below.
Syntax:
if(condition)
{
statement 1; //executes when condition is true
}
Example:

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:

• The if-else statement is an extension to the if-statement, which


uses another block of code, i.e., else block. The else block is
executed if the condition of the if-block is evaluated as false.

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:

• The if-else-if statement contains the if-statement followed by


multiple else-if statements.
• Syntax:
if(condition 1) {
statement 1; //executes when condition 1 is true
}
else if(condition 2) {
statement 2; //executes when condition 2 is true
}
else {
statement 2; //executes when all the conditions are false
}
Example:

public class Student {


public static void main(String[] args) {
if(city == "Meerut") {
System.out.println("city is meerut");
}else if (city == "Noida") {
System.out.println("city is noida");
}else if(city == "Agra") {
System.out.println("city is agra");
}else {
System.out.println(city);
}
}
}
Nested if-statement:
in nested if-statements, the if statement can contain a if or if-else statement inside another
if or else-if statement.
Syntax :
if(condition 1)
{
statement 1; //executes when condition 1 is true
if(condition 2)
{
statement 2; //executes when condition 2 is true
}
Else
{
statement 2; //executes when condition 2 is false
}
}
class Nestedif
{
public static void main(String[] args){
int s =29;
if(s<40)
{
if(s%2==0)
{
System.out.println("even");
}
else
{
System.out.println("not even");
}
}
else
{
System.out.println("if is not executed");
}
}
Looping Statements:

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:

for( datatype refvar : refvar)


Jumping Statements / Switch Statements:

• When a user or programmer have to check multiple


options/cases then user have to go for jumping statements.
• Switch will not check for condition rather it will check for
option/case

• 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.

• Scanner sc = new Scanner(System.in);


• If user wants to give

nextBoolean() Reads a boolean value from the


user
nextByte() Reads a byte value from the user
nextDouble() Reads a double value from the user
nextFloat() Reads a float value from the user
nextInt() Reads a int value from the user
nextLine() Reads a String value from the user
nextLong() Reads a long value from the user
nextShort() Reads a short value from the user
Example:
import java.util.Scanner;

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:

• Converting one primitive datatype to another primitive datatype is called


typecasting.
• 2 types of casting:
1. Primitive casting
a. Implicit casting
b. Explicit casting
2. Non – primitive casting.

1. Primitive casting : converting one primitive datatype into another primitive


datatype is called primitive casting.
a. Implicit:converting one primitive datatype into another primitive
datatype by the compiler is called implicit casting.
• There is no loss of conversion so it is called widening.
• Widening: converting smaller datatype into larger datatype is
widening.
Example:

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

• We can’t print empty space We can print empty space

• It print in the form of horizontal It print in the form of vertical


Unstructured way of programming:
• Writing the same piece of code multiple times in the main method is called
unstructured way of programming.

• 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);
}

public static void m1()


{
int a = 10;
int b = 20; (a-b)
System.out.println(a-b);
}
Class :
A class in Java is a logical template to create objects that share common
properties and methods.

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.

• State: represents the data (value) of an object.

• Behavior: represents the behavior (functionality) of an object


• Ex: mobile
• State: name,color, features,camera etc,
• Behaviour: calling, text, taking pictures etc.

• The state of an object is stored in fields (variables), while methods


(functions) display the object's behavior.
Syntax:

Class
{
State // state is also called data members
&
Behaviour // it is also called as function members/ methods.\
}
Static Members:

• Static members should be declare with static keyword


• Static variable should be declare inside the class but
outside the methods.
• Static members are also known as class members.
• Static members will have a single copy memory . It will get
loaded in to the static pool area.
• Static can access in two ways.
a.Directly
b.With help of class name.
Example:
Class Hacker
{
Static int a = 12;
public static void test()
{
System.out.println(a); // directly access the static variable
}
public static void main(String[] args)
{
test();

System.out.println(Hacker.a); // with help of class name


}
}
Explain take two classes and access the static variables.
Non-Static Members:

• Non static members should be declare with out any keyword


• Non static variable should be declare inside the class but outside the
method.
• Non static members are also known as Instance members.
• Non static members will have a multiple copies in the memory .
• It will get loaded inside the object and objects are present in heap area.
• Non static can access in one way by creating an object

• Syntax: object.membername;

to create an object : syntax


new className();
Example:

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();

System.out.println(new Hacker().a); // with help of class name


}
}
Static member N0n Static member
• Static members should declare with • Static members should declare
static keyword . with-out any static keyword .
• Static members are also known as • Static members are also known as
class members. instance members.
• Static members will have single • Non-Static members will have
copy memory multiple copy memory .
• Static members are loaded in static • Non-Static members are loaded in
pool area the object and that object stored in
heap area.
• Any change made the impact will • Any change made the impact will
happen to the entire class happen to that particular object.
• Static members we can access In 2 • Non-Static members we can access
ways only by creating an object.
• Directly
• With help of class name
Reference variable :
• Reference variable is a non primitive type
• All Reference variable are class types
• Reference variable will hold address of an object
Syntax:
classname refrencevar = new classname();
Demo d = new Demo();

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.

Rules to declare constructor


• Constructor name should be always same as class name.
• Constructor will not follow any return type not even void also.
• Constructor can be declare any level of access modifier.

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.

Rules for constructor overloading:


• Maintain constructor name as classname
• Constructor will not follow any returntype.

You might also like