SlideShare a Scribd company logo
JAVA PROGRAMMING
Module 1
BASICS OF JAVA PROGRAMMING
SIMPLE JAVA PROGRAM
class Simple{
public static void main(String args[]){
System.out.println("Hello Java");
}
}
OUTPUT:
Hello Java
JVM
• JVM (Java Virtual Machine) is an abstract
machine.
• It is called a virtual machine because it doesn't
physically exist.
• It is a specification that provides a runtime
environment in which Java bytecode can be
executed.
• It can also run those programs which are written
in other languages and compiled to Java
bytecode.
• JVMs are available for many hardware and
software platforms.
• JVM, JRE, and JDK are platform dependent
because the configuration of each OS is
different from each other.
• However, Java is platform independent.
• There are three notions of the
JVM: specification, implementation,
and instance.
The JVM performs the following main tasks:
• Loads code
• Verifies code
• Executes code
• Provides runtime environment
JRE
• JRE is an acronym for Java Runtime Environment.
It is also written as Java RTE.
• The JRE is a set of software tools which are used
for developing Java applications.
• It is used to provide the runtime environment. It
is the implementation of JVM.
• It physically exists. It contains a set of libraries +
other files that JVM uses at runtime.
• The implementation of JVM is also actively
released by other companies besides Sun Micro
Systems.
Module 1.pptx
JDK
• JDK is an acronym for Java Development Kit.
• The Java Development Kit (JDK) is a software
development environment which is used to
develop Java applications and applets. It
physically exists.
• It contains JRE + development tools.
• JDK is an implementation of any one of the below
given Java Platforms released by Oracle
Corporation:
– Standard Edition Java Platform
– Enterprise Edition Java Platform
– Micro Edition Java Platform
• The JDK contains a private Java Virtual
Machine (JVM) and a few other resources
such as an interpreter/loader (java), a
compiler (javac), an archiver (jar), a
documentation generator (Javadoc), etc. to
complete the development of a Java
Application.
Module 1.pptx
Data Types
• Data types specify the different sizes and
values that can be stored in the variable.
There are two types of data types in Java:
• Primitive data types: The primitive data types
include boolean, char, byte, short, int, long,
float and double.
• Non-primitive data types: The non-primitive
data types include Classes, Interfaces,
and Arrays.
Primitive Data Types
• Primitive data types are the building blocks of
data manipulation.
Module 1.pptx
Boolean Data Type
The Boolean data type is used to store only
two possible values: true and false.
• Example:
Boolean one = false
Variables
• A variable is a container which holds the value
while the java program is executed.
• A variable is assigned with a data type.
• It is a combination of "vary + able" which
means its value can be changed.
• Example:
int data=50; //Here data is variable
Module 1.pptx
1) Local Variable
A variable declared inside the body of the method is called
local variable. You can use this variable only within that
method and the other methods in the class aren't even
aware that the variable exists.
A local variable cannot be defined with "static" keyword.
2) Instance Variable
A variable declared inside the class but outside the body of
the method, is called an instance variable. It is not declared
as static.
It is called an instance variable because its value is instance-
specific and is not shared among instances.
3) Static variable
A variable that is declared as static is called a static variable.
It cannot be local. You can create a single copy of the static
variable and share it among all the instances of the class.
public class A
{
static int m=100; //static variable
void method()
{
int n=90; //local variable
}
public static void main(String args[])
{
int data=50; //instance variable
}
} //end of class
Module 1.pptx
public class OperatorExample{
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
}}
public class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
System.out.println(a+b); //15
System.out.println(a-b); //5
System.out.println(a*b); //50
System.out.println(a/b); //2
System.out.println(a%b); //0
}}
public class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
int c=20;
System.out.println(a<b&&a<c);
//false && true = false
System.out.println(a<b&a<c);
//false & true = false
}}
public class OperatorExample{
public static void main(String[] args){
int a=10;
a+=3; //10+3 =13
System.out.println(a);
a-=4; //13-4 =9
System.out.println(a);
a*=2; //9*2 =18
System.out.println(a);
a/=2; //18/2 =9
System.out.println(a);
}}
Conditional Operator ( ? : )
Conditional operator is also known as
the ternary operator. This operator consists of
three operands and is used to evaluate
Boolean expressions.
Example:
int min=(a<b)?a:b;
public class OperatorExample{
public static void main(String args[]){
int a=2;
int b=5;
int min=(a<b)?a:b; //2
System.out.println(min);
}}
Naming Convention
• Java naming convention is a rule to follow as you
decide what to name your identifiers such as class,
package, variable, constant, method, etc.
Command Line Arguments
• The java command-line argument is an
argument i.e. passed at the time of running
the java program.
• The arguments passed from the console can
be received in the java program and it can be
used as an input.
class CommandLineExample{
public static void main(String args[]){
System.out.println("Your first argument is: “
+args[0]);
}
}
OUTPUT:
compile by > javac CommandLineExample.java
run by > java CommandLineExample sonoo
Your first argument is: sonoo
What is an object in Java?
• An entity that has state and behavior is known
as an object e.g., chair, bike, marker, pen,
table, car, etc.
• It can be physical or logical.
• An object is an instance of a class.
What is a class in Java?
• A class is a group of objects which have
common properties. It is a template or
blueprint from which objects are created.
• A class in Java can contain:
o Fields
o Methods
o Constructors
o Blocks
o Nested class and interface
Syntax:
class <class_name>{
field;
method;
}
Method:
A method is like a function which is used to
expose the behavior of an object.
Wrapper class
• A Wrapper class is a class which contains
the primitive data types (int, char, short,
byte, etc).
• In other words, wrapper classes provide a way
to use primitive data types (int, char, short,
byte, etc) as objects.
• These wrapper classes come under java.util
package
Why we need Wrapper Class
• Wrapper Class will convert primitive data types
into objects. The objects are necessary if we wish
to modify the arguments passed into the method
(because primitive types are passed by value).
• The classes in java.util package handles only
objects and hence wrapper classes help in this
case also.
• Data structures in the Collection framework such
as ArrayList and Vector store only the objects
(reference types) and not the primitive types.
• The object is needed to
support synchronization in multithreading.
Autoboxing in Wrapper Class:
Autoboxing is used to convert primitive data
types into corresponding objects.
Unboxing in Wrapper Class:
Unboxing is used to convert the Wrapper class
object into corresponding primitive data types.
public class AutoBoxingTest
{
public static void main(String args[]) {
//Converting int primitive into an Integer object
int num = 10; // int primitive
// creating a wrapper class object
Integer obj = Integer.valueOf(num);
System.out.println(num + " " + obj);
}}
Constructors
• n Java, a constructor is a block of codes similar to the
method.
• It is called when an instance of the class is created.
• At the time of calling constructor, memory for the
object is allocated in the memory.
• It is a special type of method which is used to initialize
the object.
• Every time an object is created using the new()
keyword, at least one constructor is called.
• It calls a default constructor if there is no constructor
available in the class. In such case, Java compiler
provides a default constructor by default.
Rules for creating Java constructor
• Constructor name must be the same as its
class name.
• A Constructor must have no explicit return
type.
• A Java constructor cannot be abstract, static,
final, and synchronized.
• We can have private, protected, public or
default constructor in Java.
Types of Java constructors
• Default constructor (no-arg constructor):
doesn't have any parameter
• Parameterized constructor:
which has a specific number of
parameters.
Default constructor
class Bike1{
//creating a default constructor
Bike1()
{
System.out.println("Bike is created");
}
public static void main(String args[]){
//calling a default constructor
Bike1 b=new Bike1();
}
}
Parameterized constructor
class Student4{
int id;
String name;
//creating a parameterized constructor
Student4(int i, String n){
id = i;
name = n;
}
void display()
{
System.out.println(id+" "+name);
}
public static void main(String args[]){
//creating objects and passing values
Student4 s1 = new Student4(111,“Harinya");
Student4 s2 = new Student4(222,“Makil");
//calling method to display the values of object
s1.display();
s2.display();
}
}
Constructor Overloading
class Student5{
int id;
String name;
int age;
//creating two arg constructor
Student5(int i,String n){
id = i;
name = n;
}
//creating three arg constructor
Student5(int i,String n,int a){
id = i;
name = n;
age=a;
}
void display()
{System.out.println(id+" "+name+" "+age);}
public static void main(String args[]){
Student5 s1 = new Student5(111,"Karan");
Student5 s2 = new Student5(222,"Aryan",25);
s1.display();
s2.display();
}
}
Copy Constructor
class Student6{
int id;
String name;
Student6(int i, String n){
id = i;
name = n;
}
//constructor to initialize another object
Student6 (Student6 s){
id = s.id;
name =s.name;
}
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
Student6 s1 = new Student6(111,“Maayon");
Student6 s2 = new Student6(s1); // (111 Maayon)
s1.display();
s2.display();
}
}
Copying values without constructor
class Student7{
int id;
String name;
Student7(int i,String n){
id = i;
name = n;
}
Student7(){}
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
Student7 s1 = new Student7(111,“Maayon");
Student7 s2 = new Student7();
s2.id=s1.id;
s2.name=s1.name;
s1.display();
s2.display();
}
}
Looping Statements
A loop statement allows us to execute a
statement or group of statements multiple
times.
• while loop
• for loop
• do..while loop
while loop
• while loop is used to iterate a part of
the program repeatedly until the specified
Boolean condition is true.
• As soon as the Boolean condition becomes
false, the loop automatically stops.
• Syntax:
while (Boolean_expressions){
// statements;
}
Module 1.pptx
public class WhileExample {
public static void main(String[] args) {
int i=1;
while(i<=10){
System.out.println(i);
i++;
}
}
}
for loop
• A for loop is a repetition control structure that
allows you to efficiently write a loop that needs
to be executed a specific number of times.
• A for loop is useful when you know how many
times a task is to be repeated.
• Syntax:
for(initialization; condition; increment/decrement)
{
//statement or code to be executed
}
• Initialization: It is the initial condition which is
executed once when the loop starts. Here, we can
initialize the variable, or we can use an already
initialized variable. It is an optional condition.
• Condition: It is the second condition which is
executed each time to test the condition of the
loop. It continues execution until the condition is
false. It must return boolean value either true or
false. It is an optional condition.
• Increment/Decrement: It increments or
decrements the variable value. It is an optional
condition.
• Statement: The statement of the loop is executed
each time until the second condition is false.
Module 1.pptx
public class ForExample {
public static void main(String[] args) {
for(int i=1;i<=10;i++)
{
System.out.println(i);
}
}
}
do… while loop
• Java do-while loop is called an exit control
loop.
• Therefore, unlike while loop and for loop, the
do-while check the condition at the end of
loop body.
• The Java do-while loop is executed at least
once because condition is checked after loop
body.
Syntax:
do{
//statement
}while (condition);
public class DoWhileExample {
public static void main(String[] args) {
int i=1;
do{
System.out.println(i);
i++;
}while(i<=10);
}
}

More Related Content

Similar to Module 1.pptx (20)

PPTX
Java For Automation
Abhijeet Dubey
 
PPTX
Unit-1 Data Types and Operators.pptx to computers
csangani1
 
PPT
JavaTutorials.ppt
Khizar40
 
PPTX
Introduction to oop and java fundamentals
AnsgarMary
 
PPTX
Java
Raghu nath
 
PPTX
Introduction to java Programming Language
rubyjeyamani1
 
PPTX
Lecture - 3 Variables-data type_operators_oops concept
manish kumar
 
PPTX
Java
Zeeshan Khan
 
PPTX
Java Tokens in java program . pptx
CmDept
 
PPSX
Esoft Metro Campus - Certificate in java basics
Rasan Samarasinghe
 
PDF
JAVA Class Presentation.pdf Vsjsjsnheheh
AnushreeP4
 
PPTX
UNIT – 2 Features of java- (Shilpa R).pptx
shilpar780389
 
PPTX
Java Programs
vvpadhu
 
DOCX
Computer Programming 2
VasanthiMuniasamy2
 
PPT
Core_java_ppt.ppt
SHIBDASDUTTA
 
PPT
Java tutorials
saryu2011
 
ODP
Synapseindia reviews.odp.
Tarunsingh198
 
PPTX
Core java
Sun Technlogies
 
PPTX
Core java
Shivaraj R
 
Java For Automation
Abhijeet Dubey
 
Unit-1 Data Types and Operators.pptx to computers
csangani1
 
JavaTutorials.ppt
Khizar40
 
Introduction to oop and java fundamentals
AnsgarMary
 
Introduction to java Programming Language
rubyjeyamani1
 
Lecture - 3 Variables-data type_operators_oops concept
manish kumar
 
Java Tokens in java program . pptx
CmDept
 
Esoft Metro Campus - Certificate in java basics
Rasan Samarasinghe
 
JAVA Class Presentation.pdf Vsjsjsnheheh
AnushreeP4
 
UNIT – 2 Features of java- (Shilpa R).pptx
shilpar780389
 
Java Programs
vvpadhu
 
Computer Programming 2
VasanthiMuniasamy2
 
Core_java_ppt.ppt
SHIBDASDUTTA
 
Java tutorials
saryu2011
 
Synapseindia reviews.odp.
Tarunsingh198
 
Core java
Sun Technlogies
 
Core java
Shivaraj R
 

Recently uploaded (20)

PPTX
Online Cab Booking and Management System.pptx
diptipaneri80
 
PDF
勉強会資料_An Image is Worth More Than 16x16 Patches
NABLAS株式会社
 
PDF
The Complete Guide to the Role of the Fourth Engineer On Ships
Mahmoud Moghtaderi
 
PDF
Jual GPS Geodetik CHCNAV i93 IMU-RTK Lanjutan dengan Survei Visual
Budi Minds
 
PPTX
Sensor IC System Design Using COMSOL Multiphysics 2025-July.pptx
James D.B. Wang, PhD
 
PDF
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
PDF
Introduction to Robotics Mechanics and Control 4th Edition by John J. Craig S...
solutionsmanual3
 
PPT
IISM Presentation.ppt Construction safety
lovingrkn
 
PPTX
MULTI LEVEL DATA TRACKING USING COOJA.pptx
dollysharma12ab
 
PDF
Farm Machinery and Equipments Unit 1&2.pdf
prabhum311
 
PPTX
Basics of Auto Computer Aided Drafting .pptx
Krunal Thanki
 
PDF
Natural Language processing and web deigning notes
AnithaSakthivel3
 
PPTX
Unit 2 Theodolite and Tachometric surveying p.pptx
satheeshkumarcivil
 
PPTX
Precedence and Associativity in C prog. language
Mahendra Dheer
 
PDF
CFM 56-7B - Engine General Familiarization. PDF
Gianluca Foro
 
PPTX
cybersecurityandthe importance of the that
JayachanduHNJc
 
PDF
MRI Tool Kit E2I0500BC Plus Presentation
Ing. Ph. J. Daum GmbH & Co. KG
 
PPTX
filteration _ pre.pptx 11111110001.pptx
awasthivaibhav825
 
PPTX
ETP Presentation(1000m3 Small ETP For Power Plant and industry
MD Azharul Islam
 
PDF
July 2025 - Top 10 Read Articles in Network Security & Its Applications.pdf
IJNSA Journal
 
Online Cab Booking and Management System.pptx
diptipaneri80
 
勉強会資料_An Image is Worth More Than 16x16 Patches
NABLAS株式会社
 
The Complete Guide to the Role of the Fourth Engineer On Ships
Mahmoud Moghtaderi
 
Jual GPS Geodetik CHCNAV i93 IMU-RTK Lanjutan dengan Survei Visual
Budi Minds
 
Sensor IC System Design Using COMSOL Multiphysics 2025-July.pptx
James D.B. Wang, PhD
 
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
Introduction to Robotics Mechanics and Control 4th Edition by John J. Craig S...
solutionsmanual3
 
IISM Presentation.ppt Construction safety
lovingrkn
 
MULTI LEVEL DATA TRACKING USING COOJA.pptx
dollysharma12ab
 
Farm Machinery and Equipments Unit 1&2.pdf
prabhum311
 
Basics of Auto Computer Aided Drafting .pptx
Krunal Thanki
 
Natural Language processing and web deigning notes
AnithaSakthivel3
 
Unit 2 Theodolite and Tachometric surveying p.pptx
satheeshkumarcivil
 
Precedence and Associativity in C prog. language
Mahendra Dheer
 
CFM 56-7B - Engine General Familiarization. PDF
Gianluca Foro
 
cybersecurityandthe importance of the that
JayachanduHNJc
 
MRI Tool Kit E2I0500BC Plus Presentation
Ing. Ph. J. Daum GmbH & Co. KG
 
filteration _ pre.pptx 11111110001.pptx
awasthivaibhav825
 
ETP Presentation(1000m3 Small ETP For Power Plant and industry
MD Azharul Islam
 
July 2025 - Top 10 Read Articles in Network Security & Its Applications.pdf
IJNSA Journal
 
Ad

Module 1.pptx

  • 2. BASICS OF JAVA PROGRAMMING
  • 3. SIMPLE JAVA PROGRAM class Simple{ public static void main(String args[]){ System.out.println("Hello Java"); } } OUTPUT: Hello Java
  • 4. JVM • JVM (Java Virtual Machine) is an abstract machine. • It is called a virtual machine because it doesn't physically exist. • It is a specification that provides a runtime environment in which Java bytecode can be executed. • It can also run those programs which are written in other languages and compiled to Java bytecode.
  • 5. • JVMs are available for many hardware and software platforms. • JVM, JRE, and JDK are platform dependent because the configuration of each OS is different from each other. • However, Java is platform independent. • There are three notions of the JVM: specification, implementation, and instance.
  • 6. The JVM performs the following main tasks: • Loads code • Verifies code • Executes code • Provides runtime environment
  • 7. JRE • JRE is an acronym for Java Runtime Environment. It is also written as Java RTE. • The JRE is a set of software tools which are used for developing Java applications. • It is used to provide the runtime environment. It is the implementation of JVM. • It physically exists. It contains a set of libraries + other files that JVM uses at runtime. • The implementation of JVM is also actively released by other companies besides Sun Micro Systems.
  • 9. JDK • JDK is an acronym for Java Development Kit. • The Java Development Kit (JDK) is a software development environment which is used to develop Java applications and applets. It physically exists. • It contains JRE + development tools. • JDK is an implementation of any one of the below given Java Platforms released by Oracle Corporation: – Standard Edition Java Platform – Enterprise Edition Java Platform – Micro Edition Java Platform
  • 10. • The JDK contains a private Java Virtual Machine (JVM) and a few other resources such as an interpreter/loader (java), a compiler (javac), an archiver (jar), a documentation generator (Javadoc), etc. to complete the development of a Java Application.
  • 12. Data Types • Data types specify the different sizes and values that can be stored in the variable. There are two types of data types in Java: • Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double. • Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays.
  • 13. Primitive Data Types • Primitive data types are the building blocks of data manipulation.
  • 15. Boolean Data Type The Boolean data type is used to store only two possible values: true and false. • Example: Boolean one = false
  • 16. Variables • A variable is a container which holds the value while the java program is executed. • A variable is assigned with a data type. • It is a combination of "vary + able" which means its value can be changed. • Example: int data=50; //Here data is variable
  • 18. 1) Local Variable A variable declared inside the body of the method is called local variable. You can use this variable only within that method and the other methods in the class aren't even aware that the variable exists. A local variable cannot be defined with "static" keyword. 2) Instance Variable A variable declared inside the class but outside the body of the method, is called an instance variable. It is not declared as static. It is called an instance variable because its value is instance- specific and is not shared among instances. 3) Static variable A variable that is declared as static is called a static variable. It cannot be local. You can create a single copy of the static variable and share it among all the instances of the class.
  • 19. public class A { static int m=100; //static variable void method() { int n=90; //local variable } public static void main(String args[]) { int data=50; //instance variable } } //end of class
  • 21. public class OperatorExample{ 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 }}
  • 22. public class OperatorExample{ public static void main(String args[]){ int a=10; int b=5; System.out.println(a+b); //15 System.out.println(a-b); //5 System.out.println(a*b); //50 System.out.println(a/b); //2 System.out.println(a%b); //0 }}
  • 23. public class OperatorExample{ public static void main(String args[]){ int a=10; int b=5; int c=20; System.out.println(a<b&&a<c); //false && true = false System.out.println(a<b&a<c); //false & true = false }}
  • 24. public class OperatorExample{ public static void main(String[] args){ int a=10; a+=3; //10+3 =13 System.out.println(a); a-=4; //13-4 =9 System.out.println(a); a*=2; //9*2 =18 System.out.println(a); a/=2; //18/2 =9 System.out.println(a); }}
  • 25. Conditional Operator ( ? : ) Conditional operator is also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean expressions. Example: int min=(a<b)?a:b;
  • 26. public class OperatorExample{ public static void main(String args[]){ int a=2; int b=5; int min=(a<b)?a:b; //2 System.out.println(min); }}
  • 27. Naming Convention • Java naming convention is a rule to follow as you decide what to name your identifiers such as class, package, variable, constant, method, etc.
  • 28. Command Line Arguments • The java command-line argument is an argument i.e. passed at the time of running the java program. • The arguments passed from the console can be received in the java program and it can be used as an input.
  • 29. class CommandLineExample{ public static void main(String args[]){ System.out.println("Your first argument is: “ +args[0]); } } OUTPUT: compile by > javac CommandLineExample.java run by > java CommandLineExample sonoo Your first argument is: sonoo
  • 30. What is an object in Java? • An entity that has state and behavior is known as an object e.g., chair, bike, marker, pen, table, car, etc. • It can be physical or logical. • An object is an instance of a class.
  • 31. What is a class in Java? • A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. • A class in Java can contain: o Fields o Methods o Constructors o Blocks o Nested class and interface
  • 32. Syntax: class <class_name>{ field; method; } Method: A method is like a function which is used to expose the behavior of an object.
  • 33. Wrapper class • A Wrapper class is a class which contains the primitive data types (int, char, short, byte, etc). • In other words, wrapper classes provide a way to use primitive data types (int, char, short, byte, etc) as objects. • These wrapper classes come under java.util package
  • 34. Why we need Wrapper Class • Wrapper Class will convert primitive data types into objects. The objects are necessary if we wish to modify the arguments passed into the method (because primitive types are passed by value). • The classes in java.util package handles only objects and hence wrapper classes help in this case also. • Data structures in the Collection framework such as ArrayList and Vector store only the objects (reference types) and not the primitive types. • The object is needed to support synchronization in multithreading.
  • 35. Autoboxing in Wrapper Class: Autoboxing is used to convert primitive data types into corresponding objects. Unboxing in Wrapper Class: Unboxing is used to convert the Wrapper class object into corresponding primitive data types.
  • 36. public class AutoBoxingTest { public static void main(String args[]) { //Converting int primitive into an Integer object int num = 10; // int primitive // creating a wrapper class object Integer obj = Integer.valueOf(num); System.out.println(num + " " + obj); }}
  • 37. Constructors • n Java, a constructor is a block of codes similar to the method. • It is called when an instance of the class is created. • At the time of calling constructor, memory for the object is allocated in the memory. • It is a special type of method which is used to initialize the object. • Every time an object is created using the new() keyword, at least one constructor is called. • It calls a default constructor if there is no constructor available in the class. In such case, Java compiler provides a default constructor by default.
  • 38. Rules for creating Java constructor • Constructor name must be the same as its class name. • A Constructor must have no explicit return type. • A Java constructor cannot be abstract, static, final, and synchronized. • We can have private, protected, public or default constructor in Java.
  • 39. Types of Java constructors • Default constructor (no-arg constructor): doesn't have any parameter • Parameterized constructor: which has a specific number of parameters.
  • 40. Default constructor class Bike1{ //creating a default constructor Bike1() { System.out.println("Bike is created"); } public static void main(String args[]){ //calling a default constructor Bike1 b=new Bike1(); } }
  • 41. Parameterized constructor class Student4{ int id; String name; //creating a parameterized constructor Student4(int i, String n){ id = i; name = n; } void display() { System.out.println(id+" "+name); } public static void main(String args[]){ //creating objects and passing values Student4 s1 = new Student4(111,“Harinya"); Student4 s2 = new Student4(222,“Makil"); //calling method to display the values of object s1.display(); s2.display(); } }
  • 42. Constructor Overloading class Student5{ int id; String name; int age; //creating two arg constructor Student5(int i,String n){ id = i; name = n; } //creating three arg constructor Student5(int i,String n,int a){ id = i; name = n; age=a; } void display() {System.out.println(id+" "+name+" "+age);} public static void main(String args[]){ Student5 s1 = new Student5(111,"Karan"); Student5 s2 = new Student5(222,"Aryan",25); s1.display(); s2.display(); } }
  • 43. Copy Constructor class Student6{ int id; String name; Student6(int i, String n){ id = i; name = n; } //constructor to initialize another object Student6 (Student6 s){ id = s.id; name =s.name; } void display(){System.out.println(id+" "+name);} public static void main(String args[]){ Student6 s1 = new Student6(111,“Maayon"); Student6 s2 = new Student6(s1); // (111 Maayon) s1.display(); s2.display(); } }
  • 44. Copying values without constructor class Student7{ int id; String name; Student7(int i,String n){ id = i; name = n; } Student7(){} void display(){System.out.println(id+" "+name);} public static void main(String args[]){ Student7 s1 = new Student7(111,“Maayon"); Student7 s2 = new Student7(); s2.id=s1.id; s2.name=s1.name; s1.display(); s2.display(); } }
  • 45. Looping Statements A loop statement allows us to execute a statement or group of statements multiple times. • while loop • for loop • do..while loop
  • 46. while loop • while loop is used to iterate a part of the program repeatedly until the specified Boolean condition is true. • As soon as the Boolean condition becomes false, the loop automatically stops. • Syntax: while (Boolean_expressions){ // statements; }
  • 48. public class WhileExample { public static void main(String[] args) { int i=1; while(i<=10){ System.out.println(i); i++; } } }
  • 49. for loop • A for loop is a repetition control structure that allows you to efficiently write a loop that needs to be executed a specific number of times. • A for loop is useful when you know how many times a task is to be repeated. • Syntax: for(initialization; condition; increment/decrement) { //statement or code to be executed }
  • 50. • Initialization: It is the initial condition which is executed once when the loop starts. Here, we can initialize the variable, or we can use an already initialized variable. It is an optional condition. • Condition: It is the second condition which is executed each time to test the condition of the loop. It continues execution until the condition is false. It must return boolean value either true or false. It is an optional condition. • Increment/Decrement: It increments or decrements the variable value. It is an optional condition. • Statement: The statement of the loop is executed each time until the second condition is false.
  • 52. public class ForExample { public static void main(String[] args) { for(int i=1;i<=10;i++) { System.out.println(i); } } }
  • 53. do… while loop • Java do-while loop is called an exit control loop. • Therefore, unlike while loop and for loop, the do-while check the condition at the end of loop body. • The Java do-while loop is executed at least once because condition is checked after loop body.
  • 55. public class DoWhileExample { public static void main(String[] args) { int i=1; do{ System.out.println(i); i++; }while(i<=10); } }