CH 1 Java
CH 1 Java
Java JDK (Java Development Kit) is a software development kit for developing Java
programming applications
JDK is a superset of JRE, it contains everything that JRE has along with
development tools such as compiler, debugger etc.
1. JDK stands for Java Development Kit. To install Java on your system,
you need to first install JDK on your system.
The JDK contains the JRE and the JRE contains the JVM virtually
The JDK consists of the JRE as well as command-line development tools like
compilers and debuggers that are required or useful when creating java
applications
JRE has an instance of JVM with it, library classes and development tools. To
understand the working of JRE let us see an example of a simple "Hello World"
program.
1. import java.util.*
3. System.out.println(?Hello world?);
4. }
Once you write this program, you have to save it with .java extension. Compile
your program. The output of the Java compiler is a byte-code which is platform
independent. After compiling, the compiler generates a .class file which has
the bytecode. The bytecode is platform independent and runs on any device
having the JRE. From here, the work of JRE begins. To run any Java program,
you need JRE.
Features of JDK
1. It has all the java development tools such as compiler, JVM, JRE,
debugger etc.
2. You need jdk in order to write and run java program.
3. JDK supports multiple platforms and can be installed on Windows,
Mac and other operating systems.
JRE (Java Runtime Environment)
JRE is the environment within which the java virtual machine runs. JRE contains
Java virtual Machine(JVM), class libraries, and other files excluding development
tools such as compiler and debugger.
Which means you can run the code in JRE but you can’t develop and compile the
code in JRE.
Features of JRE
1. JRE contains set of libraries and other tools that JVM needs at
runtime.
2. You can easily run any java program on JRE but you need jdk to write
and compile java programs.
3. JRE contains libraries that are required for integrations such as Java
Database Connectivity (JDBC), Java Naming and Directory Interface
(JNDI), Remote Method Invocation (RMI) etc.
JVM (Java Virtual Machine)
Java is a high level programming language. A program written in high level
language cannot be run on any machine directly. First, it needs to be translated
into that particular machine language. The javac compiler does this thing, it
takes java program (.java file containing source code) and translates it into
machine code (referred as byte code or .class file).
The JVM is an abstract computing machine that enables a computer to run a Java
program. When you run a Java application, the JVM reads the compiled bytecode
(generated by the JDK) and interprets it into machine code for execution.
Java Virtual Machine (JVM) is a virtual machine that resides in the real machine
(your computer) and the machine language for JVM is byte code. This makes it
easier for compiler as it has to generate byte code for JVM rather than different
machine code for each type of machine. JVM executes the byte code generated
by compiler and produce output. JVM is the one that makes java platform
independent.
Features of JVM
1. JVM makes it possible to run java code on any machine, it is the JVM
that makes java truly platform independent.
2. It also allows to run java applications on cloud platforms.
3. JDK and JRE both of these contain JVM.
4. JVM is an interpreter as it executes the java code line by line.
5. JVM converts the bytecode into machine code. JVM is platform
independent as JVM doesn’t depend on the hardware and operating
system of the machine.
How JVM works?
Variable
variable is a container which holds the value while the Java program is
executed. A variable is assigned with a data type.
1. Local Variable
2. Instance Variable
3. static Variable
2.1 Local Variable:
When we declare any variable inside any method then it is called a local
variable. Therefore, they can be used only within the method, block, not
outside the method.
Example:
System.out.println(a + b);
obj.add();
System.out.println(a + b);
}
public static void main(String[] args) {
obj.add();
• The static variables are those that are common to all the class instances.
• Only a single copy of the static variable is created and shared among all the class
instances.
• Because it is a class-level variable, memory allocation of such variables only happens once
when the class is loaded in the memory.
• If an object modifies the value of a static variable, the change is reflected across all objects.
Example:
static int firstNumber; // static variable created at class level with static keyword
static int secondNumber; // we can call a,b directly with class name.
Calculator.firstNumber = 5;
Calculator.secondNumber = 6;
System.out.println(firstNumber + secondNumber);