Merged Ppt of Java Programming Language
Merged Ppt of Java Programming Language
Java Introduction
Learning Objectives
To know about
Java
Java programming was developed by
James Gosling at Sun Microsystems in
1995.
Features of Java
• Simple
• Object Oriented Programming
• Secure
• Portable
• Robust
• Multithreading
• Platform independent
• Distributed
▪ Compiler
▪ Interpreter
▪ javac helloworld.java
▪ java helloworld
Path
Thank you
Thank you
Thank you
A. True
B. False
language
103 Java Programming
1.0 Java introduction
Subject Name
A.JRE
B.JVM
C.JDK
D.JIT
A.True
B.False
B. .txt
C. .class
D. .java
B.False
Learning Objectives
To know about
Class Vs Structure
Sr. No Class Structure
1 defined using ‘class’ defined using ‘struct’
2 stored in memory as a Every member is provided with a
reference. unique memory location.
3 Variables can be initialized Variables cannot be initialized during
during the declaration, the declaration,
4 all the members are private all the members are public
5 Reference type value type
Class Vs Structure
Sr. No Class Structure
6 variable can have null values no null values in any structure member
7 The reference type (before creating an values allocated to structures are stored in
object) is allocated on heap memory. stack memory.
8 have constructors and destructors. doesn’t have a constructor or destructor.
9 It can use inheritance to inherit doesn’t support inheritance
properties from base class.
10 The ‘protected’ access modifier can be doesn’t support protected access modifier.
used with the data members defined
inside the class.
Data Types
Data Type Size Description
byte 1 byte Stores whole numbers from -128 to 127
short 2 bytes Stores whole numbers from -32,768 to 32,767
int 4 bytes Stores whole numbers from -2,147,483,648 to
2,147,483,647
long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808
to 9,223,372,036,854,775,807
The if Statement
Syntax for if statement is :
if (condition1) {
// Statements to be executed if condition1 is true
} else if (condition2) {
// Statements to be executed if the condition1 is false and
condition2 is true
}
else {
// Statements to be executed if the condition1 is false and
condition2 is false
}
103 Java Programming(Dr Anuprita Deshmukh)
1.0 Java introduction
Subject Name
Operator
Type of Operator Operator
Arithmetic Operators +, - , *, /, %
Assignment Operators =, +=, -=, *=, /=, %=, ^=
Relational Operators <, >, ==, !=, <=, >=
Ternary Operators ?:
Increment and Decrement ++, --
Operators
Logical Operators &&, ||, !
Bitwise Operators &, |, ^, ~
Shift Operators <<, >>
103 Java Programming(Dr Anuprita Deshmukh)
1.0 Java introduction
Subject Name
Thank You
Functions / methods in
Java
Learning Objectives
▪ Need of functions/methods
What is function\method
Advantage of Method
• Code Reusability
• Code Optimization
• Improved readability
• Encapsulation
• Separation of concerns
• Improved testability
• Improved modularity
103 Java Programming (Dr Anuprita Deshmukh)
1.3 Functions\methods in Java
Subject Name
Method declaration
class helloworld
{
1. Modifier public static void main(String args[])
2. The return type {
3. Method Name System.out.println("Hello,
World!");
4. Parameter list
System.out.println("Hi...."+"
5. Exception list Anuprita");
6. Method body }
}
▪ Instance method
▪ Static method
Types of method
▪ Predefined method
▪ User method
Static method
Calling function
class Math1
{
public static void main(String args[])
{
System.out.println(“Hello");
Greating();
System.out.println(“ EveryOne");
}
public static void Greating()
{
System.out.println("Welcome");
}
}
103 Java Programming (Dr Anuprita Deshmukh)
1.3 Functions\methods in Java
Subject Name
Method overloading
Method Overloading
class Math1 {
public static void main(String args[]) {
int a = 5;
int b = 4;
int c = 7;
int ans1, ans2;
ans1 = Add(a, b);
ans2 = Add(a, b, c);
System.out.println("The First Addition is " + ans1 + “ The Addition is " + ans2);
}
public static int Add(int n1, int n2) {
return n1 + n2;
}
public static int Add(int n1, int n2, int n3) {
return n1 + n2+n3;
}
}
Quiz
A. Int
B. Float
C. Void
D. double
103 Java programming (Dr Anuprita Deshmukh)
1.3 functions\methods in Java
Subject Name
B. Float
C. Void
D. double
A. main method
B. finalize method
C. static method
D. private method
A. Function overriding
B. Function overloading
C. Function doubling
B. Function overloading
C. Function doubling
Summary
Thank you
Loops in Java
Learning Objective
While Loop
A while loop is a control flow statement that allows
code to be executed repeatedly based on a given Boolean
condition. The while loop can be thought of as a repeating
if statement.
// Initialization
while (test condition)
{
// code block to be executed
// increment or decrement (Optional)
}
Do –While Loop
A while loop is a control flow statement that allows
code to be executed repeatedly based on a given Boolean
condition. The while loop can be thought of as a repeating
if statement.
do
{
// code block to be executed
}
while (condition);
For Loop
for loop provides a concise way of writing the loop structure. Unlike a while
loop, a for statement consumes the initialization, condition and
increment/decrement in one line thereby providing a shorter, easy to debug
structure of looping.
for (Initialization; testing _condition; increment/decrement)
{
// Statements(Code) to be executed
}
Initialization is executed (only ones) before the execution of the code block.
testing condition is the condition for executing the code block.
increment/decrement is executed (every time) after the code block has been
executed.
For-each Loop
The for-each loop is used to traverse array or collection in
Java. It is easier to use than simple for loop because we
don't need to increment value and use subscript notation.
It works on the basis of elements and not the index. It
returns element one by one in the defined variable.
Syntax
for(data_type variable : array_name)
{
//code to be executed
}
Break Statement
It terminate from the loop immediately.
Continue statement
It skips the current iteration of a loop.
Can be used inside any types of loops such as for,
while, and do-while loop.
It will continue the loop but do not execute the
remaining statement after the continue statement.
Quiz
B. while loop
C. do-while loop
D. break statement
B. while loop
C. do-while loop
D. break statement
next iteration
A. return statement
B. continue statement
C. break statement
D. exit statement
B. continue statement
C. break statement
D. exit statement
B. for (traditional)
C. for-each
D. while
B. for (traditional)
C. for-each
D. while
Summary
Practical implementation of for loop.
Thank You
Learning Objective
▪ Traditional Programming
▪ Sequential Programming
▪ Structural Programming
• Person
• Car
Objects
Encapsulation
wrapping code and data together into a single unit,
Class
Variables / Data
Methods / Functions
Abstraction
Polymorphism
Inheritance
Quiz
B. Encapsulation
C. Polymorphism
D. Compilation
103 Java Programming (Dr Anuprita Deshmukh)
2.1 classes and object concept
Subject Name
B. Encapsulation
C. Polymorphism
B. Inheritance
C. Polymorphism
D. Abstraction
A. Encapsulation
B. Inheritance
C. Polymorphism
D. Abstraction
103 Java Programming (Dr Anuprita Deshmukh)
2.1 classes and object concept
Subject Name
Summary
Thank You
Learning Objective
Quiz
context
A. To indicate that the method can only be called from a static context
Summary
Thank You
Learning Objectives
Method Declaration
1. Modifier class helloworld
{
2. The return type public static void main(String args[])
{
System.out.println("Hello, World!");
3. Method Name System.out.println("Hi...."+" Anuprita");
}
4. Parameter list }
5. Exception list
6. Method body
Method declaration
class Box
{
int width;
int height;
int depth;
void volume()
{
System.out.print("Volume is ");
System.out.println(width * height * depth);
}
}
Method Declaration
class BoxDemo3 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;
mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;
mybox1.volume();
mybox2.volume();
}
}
Method Declaration
class Box
{
int width;
int height;
int depth;
int volume()
{
return width * height * depth;
}
}
mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;
int v1 = mybox1.volume();
System.out.println("Volume is " + v1);
}
}
103 Java Programming (Dr Anuprita Deshmukh)
2.3 Instance methods
Subject Name
Method Declaration
class Box
{
int width;
int height;
int depth;
void setDim(int w, int h, int d)
{
width = w;
height = h;
depth = d;
}}
Method Declaration
class BoxDemo3
{
public static void main(String args[])
{
Box mybox1 = new Box();
this keyword
this is a keyword in Java.
Call by reference
class PassObjRef
class Test {
{ public static void main(String args[])
int a, b; {
Test(int i, int j) Test ob = new Test(15, 20);
{
a = i; System.out.println("ob.a and ob.b before
b = j; call: " + ob.a + " " + ob.b);
}
void meth(Test o) ob.meth(ob);
{
o.a = 2; System.out.println("ob.a and ob.b after call:
o.b = 2; " + ob.a + " " + ob.b);
} }
} }
this keyword
class Box
{
int width;
int height;
int depth;
void setDim(int width, int height, int depth)
{
this.width = width;
this.height = height;
this.depth = depth;
}
}
Quiz
B. null
C. false
A. 0
B. null
C. false
D. All of these
D. All of these
summary
Thank you
Dr Anuprita Deshmukh
• A process is heavyweight.
• A thread is lightweight.
t.join();
• New
• Active
• Blocked / Waiting
• Timed Waiting
• Terminated
Dr Anuprita Deshmukh
Character
Byte Stream
Stream
Output
Input Stream Reader Writer
Stream
Classes Classes Classes
Classes
403 Java Programming (Dr Anuprita Deshmukh)
5.1 File Handling
Byte Stream
• import java.io.FileInputStream;
• import java.io.FileOutputStream;
• import java.io.IOException;
FileInputStream in = null;
FileOutputStream out = null;
finally {
try {
in = new FileInputStream("xanadu.txt");
if (in != null) {
out = new FileOutputStream("outagain.txt"); in.close();
int c; }
if (out != null) {
while ((c = in.read()) != -1) { out.close();
out.write(c); }
} }
}
403 Java Programming (Dr Anuprita Deshmukh) }
5.1 File Handling
byte streams to copy xanadu.txt, one byte at a time
loop reads the input stream and writes the output stream, one byte at a time
• import java.io.FileReader;
• import java.io.FileWriter;
• import java.io.IOException;
PipeInputStream
PipeArrayInputStream
Object InputStream
SequenceInputStream
ObjectInputStream
StringBufferInputStream BufferedInputStream
FilterInputStream PushBackInputStream
DataInputStream DataInput
403 Java Programming (Dr Anuprita Deshmukh)
5.1 File Handling
Hierarchy of OutputStream Classes
Object
OutputStream
FileOutputStream
PipedOutputStream
ByteArrayOutputStream
ObjectOutputStream
FilterOutputStream
BufferedOutputStream
PushBackOutputStream
DataOutputStream
DataOutput
403 Java Programming (Dr Anuprita Deshmukh)
5.1 File Handling
Hierarchy of OutputStream Classes
FileOutputStream
PipedOutputStream
ByteArrayOutputStream
Object OutputStream
ObjectOutputStream
BufferedOutputStream
FilterOutputStream PushBackOutputStream
DataOutputStream DataOutput
CharArrayReader
StringReader
Object Reader
PipeReader
FilterReader PushbackReader
InputStreamReader FileReader
CharArrayWriter
StringWriter
FilterWriter
PrintWriter
OutputStreamWriter FileWriter