Java , basics of java programming, JVM, JRE, JDK detailed explanation. primitive datatype. this ppt explains the basic concepts of java clearly starting from variables to complex programmes. It has 55 slides which gives you better understanding
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.
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;
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
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.