SlideShare a Scribd company logo
1 of 55
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.
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.
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.
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
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
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;
}
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.
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

Java Programming and J2ME: The Basics
Java Programming and J2ME: The BasicsJava Programming and J2ME: The Basics
Java Programming and J2ME: The Basics
tosine
ย 
ITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUE
ITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUEITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUE
ITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUE
VinishA23
ย 
Java tutorials
Java tutorialsJava tutorials
Java tutorials
saryu2011
ย 
Unit 1 Core Java for Compter Science 3rd
Unit 1 Core Java for Compter Science 3rdUnit 1 Core Java for Compter Science 3rd
Unit 1 Core Java for Compter Science 3rd
prat0ham
ย 

Similar to Module 1.pptx (20)

Cse java
Cse javaCse java
Cse java
ย 
Java Programming and J2ME: The Basics
Java Programming and J2ME: The BasicsJava Programming and J2ME: The Basics
Java Programming and J2ME: The Basics
ย 
intro_java (1).pptx
intro_java (1).pptxintro_java (1).pptx
intro_java (1).pptx
ย 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptx
ย 
Java For beginners and CSIT and IT students
Java  For beginners and CSIT and IT studentsJava  For beginners and CSIT and IT students
Java For beginners and CSIT and IT students
ย 
Scala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud FoundryScala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud Foundry
ย 
ITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUE
ITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUEITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUE
ITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUE
ย 
Java tutorials
Java tutorialsJava tutorials
Java tutorials
ย 
Java essentials for hadoop
Java essentials for hadoopJava essentials for hadoop
Java essentials for hadoop
ย 
Java essentials for hadoop
Java essentials for hadoopJava essentials for hadoop
Java essentials for hadoop
ย 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
ย 
java training faridabad
java training faridabadjava training faridabad
java training faridabad
ย 
Android Training (Java Review)
Android Training (Java Review)Android Training (Java Review)
Android Training (Java Review)
ย 
Java Wrapper Classes and I/O Mechanisms
Java Wrapper Classes and I/O MechanismsJava Wrapper Classes and I/O Mechanisms
Java Wrapper Classes and I/O Mechanisms
ย 
1 java programming- introduction
1  java programming- introduction1  java programming- introduction
1 java programming- introduction
ย 
JavaTutorials.ppt
JavaTutorials.pptJavaTutorials.ppt
JavaTutorials.ppt
ย 
Introduction java programming
Introduction java programmingIntroduction java programming
Introduction java programming
ย 
Unit 1 Core Java for Compter Science 3rd
Unit 1 Core Java for Compter Science 3rdUnit 1 Core Java for Compter Science 3rd
Unit 1 Core Java for Compter Science 3rd
ย 
Java Tutorials
Java Tutorials Java Tutorials
Java Tutorials
ย 
Learning core java
Learning core javaLearning core java
Learning core java
ย 

Recently uploaded

Call Girls In Bangalore โ˜Ž 7737669865 ๐Ÿฅต Book Your One night Stand
Call Girls In Bangalore โ˜Ž 7737669865 ๐Ÿฅต Book Your One night StandCall Girls In Bangalore โ˜Ž 7737669865 ๐Ÿฅต Book Your One night Stand
Call Girls In Bangalore โ˜Ž 7737669865 ๐Ÿฅต Book Your One night Stand
amitlee9823
ย 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
SUHANI PANDEY
ย 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
ย 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
ย 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
sivaprakash250
ย 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
ย 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
ย 

Recently uploaded (20)

Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
ย 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
ย 
Call Girls In Bangalore โ˜Ž 7737669865 ๐Ÿฅต Book Your One night Stand
Call Girls In Bangalore โ˜Ž 7737669865 ๐Ÿฅต Book Your One night StandCall Girls In Bangalore โ˜Ž 7737669865 ๐Ÿฅต Book Your One night Stand
Call Girls In Bangalore โ˜Ž 7737669865 ๐Ÿฅต Book Your One night Stand
ย 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
ย 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
ย 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
ย 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
ย 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
ย 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
ย 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
ย 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
ย 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ย 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
ย 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
ย 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ย 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
ย 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
ย 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
ย 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
ย 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
ย 

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.
  • 8.
  • 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.
  • 11.
  • 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.
  • 14.
  • 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
  • 17.
  • 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
  • 20.
  • 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; }
  • 47.
  • 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.
  • 51.
  • 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); } }