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

Merged Ppt of Java Programming Language

The document provides an introduction to Java programming, covering its features, the Java Virtual Machine, and the structure of a Java program. It includes details on data types, operators, control statements, and methods/functions in Java, along with examples and learning objectives. Additionally, it highlights the importance of methods for code reusability and modularity.

Uploaded by

chibunnajoe31
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Merged Ppt of Java Programming Language

The document provides an introduction to Java programming, covering its features, the Java Virtual Machine, and the structure of a Java program. It includes details on data types, operators, control statements, and methods/functions in Java, along with examples and learning objectives. Additionally, it highlights the importance of methods for code reusability and modularity.

Uploaded by

chibunnajoe31
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 184

Subject Name

Java Introduction

Dr. Anuprita Deshmukh

103 Java Programming (Dr Anuprita Deshmukh)


1.0 Java introduction
Subject Name

Learning Objectives

To know about

▪ features / characteristics of Java


▪ Java Virtual Machine
▪ Structure of Java Program
▪ Execute one simple program in Java

103 Java Programming(Dr Anuprita Deshmukh)


1.0 Java introduction
Subject Name

Java
Java programming was developed by
James Gosling at Sun Microsystems in
1995.

James Gosling is well known as the


father of Java.

103 Java Programming(Dr Anuprita Deshmukh)


1.0 Java introduction
Subject Name

Features of Java
• Simple
• Object Oriented Programming
• Secure
• Portable
• Robust
• Multithreading
• Platform independent
• Distributed

103 Java Programming (Dr Anuprita Deshmukh)


1.0 Java Introduction
Subject Name

Java virtual Machine JVM

▪ Compiler

▪ Interpreter

103 Java Programming


1.0 Java introduction
Subject Name

Java Development Kit


▪ The Java Development Kit (JDK) is a distribution of Java
technology by Oracle Corporation.
▪ It implements the Java Language Specification (JLS) and the Java
Virtual Machine Specification (JVMS) and provides the Standard
Edition (SE) of the Java Application Programming Interface (API).
▪ It is derivative of the community driven Open JDK which Oracle
stewards. It provides software for working with Java applications.
▪ Examples of included software are the Java virtual machine, a
compiler, performance monitoring tools, a debugger, and other
utilities that Oracle considers useful for Java programmers

103 Java Programming


1.0 Java introduction
Subject Name

103 Java Programming


1.0 Java introduction
Subject Name

Structure of Java Program

103 Java Programming


1.0 Java introduction
Subject Name

Structure of Java Program


▪ Documentation Section – (optional) It includes basic
information about a Java program.

▪ Package statement - (optional) In this section, declare the package


name in which the class is placed.

▪ Import statements - (optional) The import statement represents the


class stored in the other package.

▪ Interface Section - (optional) It is an optional section. It contains


only constants and method declarations
103 Java Programming
1.0 Java introduction
Subject Name

Structure of Java Program


▪ Class Definition - It is vital part of a Java program. A Java
program may conation more than one class definition. The class is
a blueprint of a Java program.

▪ Main Method Class - Every Java stand-alone program requires


the main method as the starting point of the program. There may
be many classes in a Java program, and only one class defines the
main method.

103 Java Programming


1.0 Java introduction
Subject Name

Building Java applications


class helloworld
{
public static void main(String args[])
{
System.out.println("Hello, World!");
System.out.println("Hi...."+" Anuprita");
}
}

103 Java Programming


1.0 Java introduction
Subject Name

to run a java program:

▪ javac helloworld.java

▪ java helloworld

103 Java Programming


1.0 Java introduction
Subject Name

Path

set path = C:\Program Files\Java\jdk-


17.0.2\bin;.;

103 Java Programming


1.0 Java introduction
Subject Name

Thank you

103 Java Programming


1.0 Java introduction
Subject Name

Thank you

103 Java Programming


1.0 Java introduction
Subject Name

Thank you

103 Java Programming


1.0 Java introduction
Subject Name

The java interpreter is used for execution

A. True

B. False

103 Java Programming


1.0 Java introduction
Subject Name

Which statement is true about Java?

A. Java sequence-dependent programming language

B. Java is a code dependent programming language

C. Java is a platform-dependent programming language

D. Java is a platform-independent programming

language
103 Java Programming
1.0 Java introduction
Subject Name

Which component is used to compile, debug and


execute the java programs?

A.JRE

B.JVM

C.JDK

D.JIT

103 Java Programming


1.0 Java introduction
Subject Name

The Java source code can be created in a


Notepad editor.

A.True

B.False

103 Java Programming


1.0 Java introduction
Subject Name

What is the extension of Java code


files?
A. .js

B. .txt

C. .class

D. .java

103 Java Programming


1.0 Java introduction
Subject Name

On successful compilation a file with the class


extension is created.
A.True

B.False

103 Java Programming


1.0 Java introduction
Subject Name

Data Types Operators & Control Statements

Dr. Anuprita Deshmukh

103 Java Programming (Dr Anuprita Deshmukh)


1.0 Java introduction
Subject Name

Learning Objectives

To know about

• To know the data types of the Java


• To know the use of various operators in java programs.
• To learn how to build expressions using operators in java
program.
• To know the functions of operators.
• To learn Control structure in java.

103 Java Programming(Dr Anuprita Deshmukh)


1.0 Java introduction
Subject Name

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

103 Java Programming(Dr Anuprita Deshmukh)


1.0 Java introduction
Subject Name

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.

103 Java Programming(Dr Anuprita Deshmukh)


1.0 Java introduction
Subject Name

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

float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7


decimal digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15
decimal digits
boolean 1 bit Stores true or false values
char 2 bytes Stores a single character/letter or ASCII values

103 Java Programming(Dr Anuprita Deshmukh)


1.0 Java introduction
Subject Name

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

The switch Statement


The Java switch statement executes one statement from multiple conditions. It is
like if-else-if ladder statement. The switch statement works with byte, short, int,
long, enum types, String and some wrapper types like Byte, Short, Int, and Long.
The syntax for switch is:
switch(expression)
{
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}

103 Java Programming(Dr Anuprita Deshmukh)


1.0 Java introduction
Subject Name

Thank You

103 Java Programming(Dr Anuprita Deshmukh)


1.0 Java introduction
Subject Name

Functions / methods in
Java

Dr. Anuprita Deshmukh

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Functions\methods in Java
Subject Name

Learning Objectives
▪ Need of functions/methods

▪ Writing and using static method

▪ How to pass values and return a value

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Functions\methods in Java
Subject Name

What is function\method

Methods / functions of Java is a collection of statements that perform


some specific task and return the result to the caller.

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 }
}

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Functions\methods in Java
Subject Name

Ways to create method in Java

▪ Instance method

▪ Static method

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Functions\methods in Java
Subject Name

Types of method

▪ Predefined method

▪ User method

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Functions\methods in Java
Subject Name

Static method

public class Main


{
static void MyFirstMethod(ParameterList)
{
// code to be executed
}
}

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Functions\methods in Java
Subject Name

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

Calling function with parameter


class Math1 {
public static void main(String args[]) {
int a = 25;
int b = 24;
EvenOdd(a);
EvenOdd(b);
}
public static void EvenOdd(int x) {
int r = x % 2;
if (r == 0)
System.out.println("The Number is Even");
else
System.out.println("The Number is Odd");
}
}
103 Java Programming (Dr Anuprita Deshmukh)
1.3 Functions\methods in Java
Subject Name

Returning value from function


class Math1
{
public static void main(String args[])
{
int a = 5;
int S1;
S1 = Square(a);
System.out.println("The Square of " + a + " is " + S1);
}
public static int Square(int n)
{
return n * n;
}
}

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Functions\methods in Java
Subject Name

Method overloading

In java multiple methods can have the same name with


different parameters

int Add(int x, int y)


int Add(int x, int y, int z)

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Functions\methods in Java
Subject Name

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

103 Java programming (Dr Anuprita Deshmukh)


1.3 functions\methods in Java
Subject Name

Quiz

103 Java programming (Dr Anuprita Deshmukh)


1.3 functions\methods in Java
Subject Name

What are the parts of Java?

103 Java programming (Dr Anuprita Deshmukh)


1.3 functions\methods in Java
Subject Name

What are the parts of method in Java?


1. Modifier
2. The return type
3. Method Name
4. Parameter list
5. Exception list
6. Method body

103 Java programming (Dr Anuprita Deshmukh)


1.3 functions\methods in Java
Subject Name

What is the return type of a method that does


not return any value?

A. Int

B. Float

C. Void

D. double
103 Java programming (Dr Anuprita Deshmukh)
1.3 functions\methods in Java
Subject Name

What is the return type of a method that does not


return any value?
A. Int

B. Float

C. Void

D. double

103 Java programming (Dr Anuprita Deshmukh)


1.3 functions\methods in Java
Subject Name

Which method can be defined only once in a


program

A. main method

B. finalize method

C. static method

D. private method

103 Java programming (Dr Anuprita Deshmukh)


1.3 functions\methods in Java
Subject Name

What is the process of defining more than one method in


a class differentiated by method signature?

A. Function overriding

B. Function overloading

C. Function doubling

D. None of the mentioned


103 Java programming (Dr Anuprita Deshmukh)
1.3 functions\methods in Java
Subject Name

What is the process of defining more than one


method in a class differentiated by method
signature? A. Function overriding

B. Function overloading

C. Function doubling

D. None of the mentioned

103 Java programming (Dr Anuprita Deshmukh)


1.3 functions\methods in Java
Subject Name

Summary

▪ We discussed the Need of methods

▪ Learned how to Write static method

▪ Learned how to pass values and return

a value from the function

103 Java programming (Dr Anuprita Deshmukh)


1.3 functions\methods in Java
Subject Name

Thank you

103 Java programming (Dr Anuprita Deshmukh)


1.3 functions\methods in Java
Subject Name

Loops in Java

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

Learning Objective

To learn iterative statements in java.

To learn while loop

To learn the for loop.

To learn for-each loop.

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

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

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

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

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

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.

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

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
}

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

Break Statement
It terminate from the loop immediately.

Can be used inside a loop,

The control returns from the loop immediately to the first

statement after the loop.

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

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.

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

Quiz

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

Which Loop construct in Java is best


suited when number of iterations is known
A. for loop

B. while loop

C. do-while loop

D. break statement

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

Which Loop construct is used when


number of iterations is not known
A. for loop

B. while loop

C. do-while loop

D. break statement

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

What is the purpose of continue


statement in loop
A. To exit the loop immediately

B. To skip the current iteration and move to the

next iteration

C. To terminate the program

D. To execute a specific block of code

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

What is the purpose of break


statement in a loop

A. To exit the loop immediately

B. To skip the current iteration and move to the next iteration

C. To terminate the program

D. To execute a specific block of code

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

What is the key difference between


do loop and do while loop ?

A. The syntax used to define the loop

B. The number of iterations performed

C. The condition check timing

D. The ability to use the break statement


103 Java Programming (Dr Anuprita Deshmukh)
1.3 Loops in Java
Subject Name

What is common aspect between do loop


and while do loop?
A. The syntax used to define the loop

B. The number of iterations performed

C. The condition check timing

D. The ability to use the break statement

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

Which statement is used to exit loop


prematurely?

A. return statement

B. continue statement

C. break statement

D. exit statement

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

Which statement is used to return to


calling program?
A. return statement

B. continue statement

C. break statement

D. exit statement

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

Which of the following can loop through an


array without referring to the elements by
index?
A. do-while loop

B. for (traditional)

C. for-each

D. while

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

Which of the following is best suited


for limited operations?
A. do-while loop

B. for (traditional)

C. for-each

D. while

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

Summary
Practical implementation of for loop.

Practical implementation of while loop.

Practical implementation of for-each loop.

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

Thank You

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

Classes and objects concepts

Dr. Anuprita Deshmukh

103 Java Programming (Dr Anuprita Deshmukh)


2.1 classes and object concept
Subject Name

Learning Objective

To learn introduction to object oriented concepts

103 Java Programming (Dr Anuprita Deshmukh)


2.1 classes and object concept
Subject Name

Object Oriented Programming

103 Java Programming (Dr Anuprita Deshmukh)


2.1 classes and object concept
Subject Name

Object Oriented Programming

▪ Traditional Programming

▪ Sequential Programming

▪ Structural Programming

103 Java Programming (Dr Anuprita Deshmukh)


2.1 classes and object concept
Subject Name

Classes and Objects

• Person

• Car

103 Java Programming (Dr Anuprita Deshmukh)


2.1 classes and object concept
Subject Name

Objects

103 Java Programming (Dr Anuprita Deshmukh)


2.1 classes and object concept
Subject Name

Encapsulation
wrapping code and data together into a single unit,

Class

Variables / Data

Methods / Functions

103 Java Programming (Dr Anuprita Deshmukh)


2.1 classes and object concept
Subject Name

Abstraction

103 Java Programming (Dr Anuprita Deshmukh)


2.1 classes and object concept
Subject Name

Polymorphism

103 Java Programming (Dr Anuprita Deshmukh)


2.1 classes and object concept
Subject Name

Inheritance

103 Java Programming (Dr Anuprita Deshmukh)


2.1 classes and object concept
Subject Name

Quiz

103 Java Programming (Dr Anuprita Deshmukh)


2.1 classes and object concept
Subject Name

Which of the following is not the OOPs concept


in Java?
A. Inheritance

B. Encapsulation

C. Polymorphism

D. Compilation
103 Java Programming (Dr Anuprita Deshmukh)
2.1 classes and object concept
Subject Name

Which of the following is the OOPs


concept in Java?
A. Inheritance

B. Encapsulation

C. Polymorphism

D. All the above


103 Java Programming (Dr Anuprita Deshmukh)
2.1 classes and object concept
Subject Name

Which of the concept is achieved in Java by combining


methods and attributes in a class?
A. Encapsulation

B. Inheritance

C. Polymorphism

D. Abstraction

103 Java Programming (Dr Anuprita Deshmukh)


2.1 classes and object concept
Subject Name

Which of the following concept allows


call by methods

A. Encapsulation

B. Inheritance

C. Polymorphism

D. Abstraction
103 Java Programming (Dr Anuprita Deshmukh)
2.1 classes and object concept
Subject Name

Summary

Understand the concept of Object Oriented Programming.

103 Java Programming (Dr Anuprita Deshmukh)


2.1 classes and object concept
Subject Name

Thank You

103 Java Programming (Dr Anuprita Deshmukh)


2.1 classes and object concept
Subject Name

Creating class and object

Dr. Anuprita Deshmukh

103 Java Programming (Dr Anuprita Deshmukh)


2.1 creating class and object in Java
Subject Name

Learning Objective

▪ To learn how to define a class

▪ How to create a objects from class

103 Java Programming (Dr Anuprita Deshmukh


2.2 creating class and object in Java
Subject Name

Creating a Java class


Syntax to create a Java class
access_modifier class class_name
{
data members;
constructors;
methods;
...;
}

103 Java Programming (Dr Anuprita Deshmukh


2.2 creating class and object in Java
Subject Name

Creating a Java Class


class classname {
type instance-variable1;
type instance-variable2;
// ...
type instance-variableN;
type methodname1(parameter-list) {
// body of method
}
type methodname2(parameter-list) {
// body of method
}
// ...
type methodnameN(parameter-list) {
// body of method
}
}

103 Java Programming (Dr Anuprita Deshmukh


2.2 creating class and object in Java
Subject Name

Create a Java class


Example to create a Java class
class Box
{
double width;
double height;
double depth;
}

103 Java Programming (Dr Anuprita Deshmukh


2.2 creating class and object in Java
Subject Name

Creating a Java class and object


class BoxDemo
{
public static void main(String args[])
class Box {
Box mybox = new Box();
{ int vol;

int width; Mybox.width = 10;


mybox.height = 20;
int height; mybox.depth = 15;
int depth; vol = mybox.width * mybox.height *
} mybox.depth;
System.out.println("Volume is " +
vol);
}
}

103 Java Programming (Dr Anuprita Deshmukh


2.2 creating class and object in Java
Subject Name
▪ Object in Memory

103 Java Programming (Dr Anuprita Deshmukh


2.2 creating class and object in Java
Subject Name

Quiz

103 Java Programming (Dr Anuprita Deshmukh


2.2 creating class and object in Java
Subject Name

Which of the following is NOT a valid way to


create an object in Java?

A. MyClass obj = new MyClass();

B. obj = new MyClass();

C. new obj = MyClass();

D. MyClass obj = new();


103 Java Programming (Dr Anuprita Deshmukh
2.2 creating class and object in Java
Subject Name

Which of the following is a valid way to create an


object in Java?
A. MyClass obj = new MyClass();

B. obj = new MyClass();

C. new obj = MyClass();

D. MyClass obj = new();

103 Java Programming (Dr Anuprita Deshmukh


2.2 creating class and object in Java
Subject Name

What is the relationship between a class and a object?

A. An object is an instance of a class.

B. A class is an instance of an object.

C. A class and an object are the same thing.

D. An object inherits from a class.

103 Java Programming (Dr Anuprita Deshmukh


2.2 creating class and object in Java
Subject Name

What is the purpose of “static” keyword in a class


method in Java?

A. To indicate that the method can only be called from a static

context

B. To create a new instance of the class

C. To prevent the method from being overridden

D. To indicate that the method belongs to the class, not an instance

103 Java Programming (Dr Anuprita Deshmukh


2.2 creating class and object in Java
Subject Name

What is the purpose of not using “static ” keyword in a class


method?

A. To indicate that the method can only be called from a static context

B. To create a new instance of the class

C. To prevent the method from being overridden

D. To indicate that the method belongs to the class, not an instance

103 Java Programming (Dr Anuprita Deshmukh


2.2 creating class and object in Java
Subject Name

How an object is linked with a class?

A. An object is an instance of a class.

B. A class is an instance of an object.

C. A class and an object are the same thing.

D. An object inherits from a class


103 Java Programming (Dr Anuprita Deshmukh
2.2 creating class and object in Java
Subject Name

Summary

We learn how to create class and objecsts

103 Java Programming (Dr Anuprita Deshmukh


2.2 creating class and object in Java
Subject Name

Thank You

103 Java Programming (Dr Anuprita Deshmukh


2.2 creating class and object in Java
Subject Name

Instance Methods in Java

103 Java Programming (Dr Anuprita Deshmukh)


2.3 Instance methods
Subject Name

Learning Objectives

To learn how to create different ways of creating the methods /


functions in java class.

103 Java Programming (Dr Anuprita Deshmukh)


2.3 Instance methods
Subject Name

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

103 Java Programming (Dr Anuprita Deshmukh)


2.3 Instance methods
Subject Name

Method declaration
class Box
{
int width;
int height;
int depth;
void volume()
{
System.out.print("Volume is ");
System.out.println(width * height * depth);
}
}

103 Java Programming (Dr Anuprita Deshmukh)


2.3 Instance methods
Subject Name

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

103 Java Programming (Dr Anuprita Deshmukh)


2.3 Instance methods
Subject Name

Method Declaration
class Box
{
int width;
int height;
int depth;
int volume()
{
return width * height * depth;
}
}

103 Java Programming (Dr Anuprita Deshmukh)


2.3 Instance methods
Subject Name

Method Declaration class BoxDemo3


{
public static void main(String args[])
{
Box mybox1 = new Box();

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

103 Java Programming (Dr Anuprita Deshmukh)


2.3 Instance methods
Subject Name

Method Declaration

class BoxDemo3
{
public static void main(String args[])
{
Box mybox1 = new Box();

myBox.setDim(100, 200, 30)


}
}

103 Java Programming (Dr Anuprita Deshmukh)


2.3 Instance methods
Subject Name

this keyword
this is a keyword in Java.

which is used as a reference to the


object of the current class, with in
an instance method or a constructor

103 Java Programming (Dr Anuprita Deshmukh)


2.3 Instance methods
Subject Name

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

103 Java Programming (Dr Anuprita Deshmukh)


2.3 Instance methods
Subject Name

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

103 Java Programming (Dr Anuprita Deshmukh)


2.3 Instance methods
Subject Name

Quiz

103 Java Programming (Dr Anuprita Deshmukh)


2.3 Instance methods
Subject Name

What is the default value of an instance variable (non-static field) of an


object in Java if not explicitly initialized?
A. 0

B. null

C. false

D. Depends on the data type

103 Java Programming (Dr Anuprita Deshmukh)


2.3 Instance methods
Subject Name

What is the default value of an instance variable of an object in Java if not


explicitly initialized?

A. 0

B. null

C. false

D. Depends on the data type


103 Java Programming (Dr Anuprita Deshmukh)
2.3 Instance methods
Subject Name

this keyword in Java is -

A. Used to hold the reference of the current object

B. Holds object value

C. Used to create a new instance

D. All of these

103 Java Programming (Dr Anuprita Deshmukh)


2.3 Instance methods
Subject Name

New keyword in Java is -

A. Used to hold the reference of the current object

B. Holds object value

C. Used to create a new instance

D. All of these

103 Java Programming (Dr Anuprita Deshmukh)


2.3 Instance methods
Subject Name

summary

To learn how to create different ways of creating the Instance

methods in java class

103 Java Programming (Dr Anuprita Deshmukh)


2.3 Instance methods
Subject Name

Thank you

103 Java Programming (Dr Anuprita Deshmukh)


2.3 Instance methods
Multithreading

Dr Anuprita Deshmukh

403 Java Programming (Dr Anuprita Deshmukh)


4.1 Concurrent Programming
Learning objectives

To learn Multithreading / Concurrency in java.

403 Java Programming (Dr Anuprita Deshmukh)


4.1 Concurrent Programming
Multithreading / Concurrency
• A single application is often expected to do more than one thing at
a time.
• In concurrent programming, there are two basic units of
execution: processes and threads.
• In the Java programming language, concurrent programming is
mostly concerned with threads.
• A computer system has many active processes and threads.
• Processing time for a single core is shared among processes and
threads through an OS feature called time slicing.

403 Java Programming (Dr Anuprita Deshmukh)


4.1 Concurrent Programming
Process
• A process has a self-contained execution environment. A process
generally has a complete, private set of basic run-time resources;
in particular, each process has its own memory space.

• Processes are often seen as synonymous with programs or


applications.

• The Java Virtual Machine run as a single process.

403 Java Programming (Dr Anuprita Deshmukh)


4.1 Concurrent Programming
Process-based Multitasking (Multiprocessing)
• Each process has an address in memory. In other words, each
process allocates a separate memory area.

• A process is heavyweight.

• Cost of communication between the process is high.

• Switching from one process to another requires some time for


saving and loading registers, memory maps, updating lists, etc.

403 Java Programming (Dr Anuprita Deshmukh)


4.1 Concurrent Programming
403 Java Programming (Dr Anuprita Deshmukh)
4.1 Concurrent Programming
Threads
• Threads are sometimes called lightweight processes. Both
processes and threads provide an execution environment, but
creating a new thread requires fewer resources than creating a
new process.

• Threads exist within a process — every process has at least one.

• Threads share the process's resources, including memory and


open files. This makes for efficient, but potentially problematic,
communication.

403 Java Programming (Dr Anuprita Deshmukh)


4.1 Concurrent Programming
Thread-based Multitasking (Multithreading)
• Threads share the same address space.

• A thread is lightweight.

• Cost of communication between the thread is low.

• At least one process is required for each thread.

403 Java Programming (Dr Anuprita Deshmukh)


4.1 Concurrent Programming
Thread Objects
Each thread is associated with an instance of the class Thread. There
are two basic strategies for using Thread objects to create a concurrent
application.

1. To directly control thread creation and management, simply


instantiate Thread each time the application needs to initiate an
asynchronous task.

2. To abstract thread management from the rest of your


application, pass the application's tasks to an executor.

403 Java Programming (Dr Anuprita Deshmukh)


4.1 Concurrent Programming
Defining and Starting a Thread

An application that creates an instance of Thread must provide the


code that will run in that thread. There are two ways to do this:

1. Provide a Runnable object. The Runnable interface defines a


single method, run, meant to contain the code executed in the
thread. The Runnable object is passed to the Thread constructor.
(more flexible)

2. Subclass Thread. The Thread class itself implements Runnable,


though its run method does nothing.

403 Java Programming (Dr Anuprita Deshmukh)


4.1 Concurrent Programming
A Runnable object
public class HelloRunnable implements Runnable {

public void run() {


System.out.println("Hello from a thread!");
}

public static void main(String args[]) {


(new Thread(new HelloRunnable())).start();
}
invoke Thread.start() Method in order to start the new thread.
}
403 Java Programming (Dr Anuprita Deshmukh)
4.1 Concurrent Programming
Thread Object
public class HelloThread extends Thread {

public void run() {


System.out.println("Hello from a thread!");
}

public static void main(String args[]) {


(new HelloThread()).start();
}
invoke Thread.start() Method in order to start the new thread.
}
403 Java Programming (Dr Anuprita Deshmukh)
4.1 Concurrent Programming
• A Runnable object, is more general, because the Runnable object
can subclass a class other than Thread.

• The second idiom is easier to use in simple applications, but is


limited by the fact that your task class must be a descendant of
Thread.

403 Java Programming (Dr Anuprita Deshmukh)


4.1 Concurrent Programming
Pausing Execution with Sleep
• Thread.sleep() causes the current thread to suspend execution for a
specified period.

• This is an efficient means of making processor time available to the


other threads of an application or other applications that might be
running on a computer system

403 Java Programming (Dr Anuprita Deshmukh)


4.1 Concurrent Programming
Interrupts

• An interrupt is an indication to a thread that it should stop what it


is doing and do something else. It's up to the programmer to
decide exactly how a thread responds to an interrupt

403 Java Programming (Dr Anuprita Deshmukh)


4.1 Concurrent Programming
Joins
• The join method allows one thread to wait for the completion of
another. If t is a Thread object whose thread is currently executing,

t.join();

403 Java Programming (Dr Anuprita Deshmukh)


4.1 Concurrent Programming
Life cycle of a Thread
A Thread States.

• New
• Active
• Blocked / Waiting
• Timed Waiting
• Terminated

403 Java Programming (Dr Anuprita Deshmukh)


4.1 Concurrent Programming
403 Java Programming (Dr Anuprita Deshmukh)
https://www.scientecheasy.com/2020/08/life-cycle-of-thread-in-java.html/
4.1 Concurrent Programming
403 Java Programming (Dr Anuprita Deshmukh)
https://www.scientecheasy.com/2020/08/life-cycle-of-thread-in-java.html/
4.1 Concurrent Programming
Inter Thread Communication
inter thread communication
1. wait()
2. notify()
3. notifyAll()

403 Java Programming (Dr Anuprita Deshmukh)


4.1 Concurrent Programming
Thank You

403 Java Programming (Dr Anuprita Deshmukh)


4.1 Concurrent Programming
Quiz

403 Java Programming (Dr Anuprita Deshmukh)


4.1 Concurrent Programming
What is multithreaded programming?
A. It’s a process in which two different processes run simultaneously
B. It’s a process in which two or more parts of same process run
simultaneously
C. It’s a process in which many different process are able to access
same information
D. It’s a process in which a single process can access information
from many sources

403 Java Programming (Dr Anuprita Deshmukh)


4.1 Concurrent Programming
What is multithreaded programming?
A. It’s a process in which two different processes run simultaneously
B. It’s a process in which two or more parts of same process run
simultaneously
C. It’s a process in which many different process are able to access
same information
D. It’s a process in which a single process can access information
from many sources

403 Java Programming (Dr Anuprita Deshmukh)


4.1 Concurrent Programming
Which of these are types of multitasking?
A. Process based
B. Thread based
C. Process and Thread based
D. None of the mentioned

403 Java Programming (Dr Anuprita Deshmukh)


4.1 Concurrent Programming
Which of these are types of multitasking?
A. Process based
B. Thread based
C. Process and Thread based
D. None of the mentioned

403 Java Programming (Dr Anuprita Deshmukh)


4.1 Concurrent Programming
What will happen if two thread of the same priority
are called to be processed simultaneously?
A. Anyone will be executed first lexographically
B. Both of them will be executed simultaneously
C. None of them will be executed
D. It is dependent on the operating system

403 Java Programming (Dr Anuprita Deshmukh)


4.1 Concurrent Programming
What will happen if two thread of the same priority
are called to be processed simultaneously?

A. Anyone will be executed first lexographically


B. Both of them will be executed simultaneously
C. None of them will be executed
D. It is dependent on the operating system

403 Java Programming (Dr Anuprita Deshmukh)


4.1 Concurrent Programming
Summary

We understood Multithreading in java.

403 Java Programming (Dr Anuprita Deshmukh)


4.1 Concurrent Programming
File handling

Dr Anuprita Deshmukh

403 Java Programming (Dr Anuprita Deshmukh)


5.1 File Handling
Learning objectives

To learn File Handling in java.

403 Java Programming (Dr Anuprita Deshmukh)


5.1 File Handling
Create a File
Get File Information
Write to a File
Read From a File
Delete a File

403 Java Programming (Dr Anuprita Deshmukh)


5.1 File Handling
I/O Streams
• An I/O Stream represents an input source or an output destination.
A stream can represent many different kinds of sources and
destinations, including disk files, devices, other programs, and
memory arrays.

• Streams support many different kinds of data, including simple


bytes, primitive data types, localized characters, and objects. Some
streams simply pass on data; others manipulate and transform the
data in useful ways.

403 Java Programming (Dr Anuprita Deshmukh)


5.1 File Handling
403 Java Programming (Dr Anuprita Deshmukh)
5.1 File Handling
I/O Stream

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;

403 Java Programming (Dr Anuprita Deshmukh)


5.1 File Handling
public static void main(String[] args) throws 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

403 Java Programming (Dr Anuprita Deshmukh)


5.1 File Handling
Character Streams

• import java.io.FileReader;
• import java.io.FileWriter;
• import java.io.IOException;

403 Java Programming (Dr Anuprita Deshmukh)


5.1 File Handling
public static void main(String[] args) throws
IOException {

FileReader inputStream = null; finally {


FileWriter outputStream = null;
if (inputStream != null) {
try { inputStream.close();
inputStream = new }
FileReader("xanadu.txt"); if (outputStream != null) {
outputStream = new outputStream.close();
FileWriter("characteroutput.txt"); }
int c;
}
while ((c = inputStream.read()) != -1) { }
outputStream.write(c);
}
403 Java}Programming (Dr Anuprita Deshmukh)
5.1 File Handling
Buffered Streams

• If we read or write the request can handled directly by the


underlying OS. This can make a program much less efficient, since
each such request often triggers disk access, network activity, or
some other operation that is relatively expensive.

• To reduce this kind of overhead, the Java platform implements


buffered I/O streams. Buffered input streams read data from a
memory area known as a buffer; the native input API is called only
when the buffer is empty. Similarly, buffered output streams write
data to a buffer, and the native output API is called only when the
buffer is full.
403 Java Programming (Dr Anuprita Deshmukh)
5.1 File Handling
Hierarchy of InputStream Classes
Object
InputStream
FileInputStream
PipeInputStream
PipeArrayInputStream
SequenceInputStream
ObjectInputStream
StringBufferInputStream
FilterInputStream
BufferedInputStream
PushBackInputStream
DataInputStream
DataInput
403 Java Programming (Dr Anuprita Deshmukh)
5.1 File Handling
Hierarchy of InputStream Classes
FileInputStream

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

403 Java Programming (Dr Anuprita Deshmukh)


5.1 File Handling
Hierarchy of ReaderStream Classes
Object
Reader
BufferedReader
CharArrayReader
StringReader
PipeReader
FilterReader
PushbackReader
InputStreamReader
FileReader

403 Java Programming (Dr Anuprita Deshmukh)


5.1 File Handling
Hierarchy of ReaderStream Classes
BufferedReader

CharArrayReader

StringReader
Object Reader
PipeReader

FilterReader PushbackReader

InputStreamReader FileReader

403 Java Programming (Dr Anuprita Deshmukh)


5.1 File Handling
Hierarchy of WriterStream Classes
Object
Writer
BufferedWriter
CharArrayWriter
StringWriter
PipeWriter
FilterWriter
PrintWriter
OutputStreamWriter
FileWriter

403 Java Programming (Dr Anuprita Deshmukh)


5.1 File Handling
Hierarchy of WriterStream Classes
BufferedWriter

CharArrayWriter

StringWriter

Object Writer PipeWriter

FilterWriter

PrintWriter

OutputStreamWriter FileWriter

403 Java Programming (Dr Anuprita Deshmukh)


5.1 File Handling
Thank You

403 Java Programming (Dr Anuprita Deshmukh)


5.1 File Handling
Quiz

403 Java Programming (Dr Anuprita Deshmukh)


5.1 File Handling
Which of these class contains the methods
used to write in a file?
A. FileStream
B. FileInputStream
C. BUfferedOutputStream
D. FileBufferStream

403 Java Programming (Dr Anuprita Deshmukh)


5.1 File Handling
Which of these class contains the methods
used to write in a file?
A. FileStream
B. FileInputStream
C. BUfferedOutputStream
D. FileBufferStream

403 Java Programming (Dr Anuprita Deshmukh)


5.1 File Handling
Which of these values is returned by read()
method is end of file (EOF) is encountered?
A. 0
B. 1
C. -1
D. Null

403 Java Programming (Dr Anuprita Deshmukh)


5.1 File Handling
Which of these values is returned by read()
method is end of file (EOF) is encountered?
A. 0
B. 1
C. -1
D. Null

403 Java Programming (Dr Anuprita Deshmukh)


5.1 File Handling
Which of these exception is thrown by close()
and read() methods?
A. IOException
B. FileException
C. FileNotFoundException
D. FileInputOutputException

403 Java Programming (Dr Anuprita Deshmukh)


5.1 File Handling
Which of these exception is thrown by close()
and read() methods?
A. IOException
B. FileException
C. FileNotFoundException
D. FileInputOutputException

403 Java Programming (Dr Anuprita Deshmukh)


5.1 File Handling
Summary

We understood File handling in java.

403 Java Programming (Dr Anuprita Deshmukh)


5.1 File Handling

You might also like