Parameters Used in First Java Program
Parameters Used in First Java Program
Let's see what is the meaning of class, public, static, void, main,
String[], System.out.println().
1. static public void main(String args[])
1. public static void main(String[] args)
2. public static void main(String []args)
3. public static void main(String args[])
1. public static void main(String... args)
1. class A{
2. static public void main(String... args){
3. System.out.println("hello java4");
4. }
5. };
JVM
JVM (Java Virtual Machine) is an abstract
machine. It is a specification that provides
runtime environment in which java bytecode
can be executed.
JVMs are available for many hardware and
software platforms. JVM, JRE and JDK are
platform dependent because configuration of
each OS differs. But, Java is platform
independent. There are three notions of the
JVM: specification, implementation, and
instance.
The JVM performs following main tasks:
Loads code
Verifies code
Executes code
Provides runtime environment
JRE
JRE is an acronym for Java Runtime
Environment. It is used to provide runtime
environment. It is the implementation of
JVM. It physically exists. It contains set of
libraries + other files that JVM uses at
runtime.
Implementation of JVMs are also actively
released by other companies besides Sun
Micro Systems.
JDK
JDK is an acronym for Java Development
Kit. It physically exists. It contains JRE +
development tools.
nternal Architecture of JVM
Let's understand the internal architecture of
JVM. It contains classloader, memory area,
execution engine etc.
1) Classloader
Classloader is a subsystem of JVM that is
used to load class files.
2) Class(Method) Area
Class(Method) Area stores per-class
structures such as the runtime constant pool,
field and method data, the code for methods.
3) Heap
It is the runtime data area in which objects
are allocated.
4) Stack
Java Stack stores frames. It holds local
variables and partial results, and plays a part
in method invocation and return.
Each thread has a private JVM stack, created
at the same time as thread.
A new frame is created each time a method
is invoked. A frame is destroyed when its
method invocation completes.
5) Program Counter Register
PC (program counter) register contains the
address of the Java virtual machine
instruction currently being executed.
6) Native Method Stack
It contains all the native methods used in the
application.
7) Execution Engine
It contains:
1) A virtual processor
2) Interpreter: Read bytecode stream then
execute the instructions.
3) Just-In-Time(JIT) compiler: It is used
to improve the performance. JIT compiles
parts of the byte code that have similar
functionality at the same time, and hence
reduces the amount of time needed for
compilation.Here the term ?compiler? refers
to a translator from the instruction set of a
Java virtual machine (JVM) to the
instruction set of a specific CPU.
Type Casting
Assigning a value of one type to a variable of another type is known as Type Casting.
Example :
int x = 10;
byte y = (byte)x;
In Java, type casting is classified into two types,
Widening Casting(Implicit)
Example :
Example:
class Test
{
int i = 100;
long l = i;
float f = l;
}
}
Int value 100
char and number are not compatible with each other. Let’s see when we try to convert
one into other.
//Java program to illustrate incompatible data
// type for explicit type conversion
public class Test
{
public static void main(String[] argv)
{
char ch = 'c';
int num = 88;
ch = num;
}
}
Run on IDE
Error:
7: error: incompatible types: possible lossy conversion from int to char
ch = num;
1 error
While assigning value to byte type the fractional part is lost and is reduced to modulo
256(range of byte).
Example:
//Java program to illustrate Conversion of int and
double to byte
class Test
{
public static void main(String args[])
{
byte b;
int i = 257;
double d = 323.142;
System.out.println("Conversion of int to
byte.");
//i%256
b = (byte) i;
System.out.println("i = " + i + " b = " + b);
System.out.println("\nConversion of double to
byte.");
//d%256
b = (byte) d;
System.out.println("d = " + d + " b= " + b);
}
}
Run on IDE
Output:
Conversion of int to byte.
i = 257 b = 1
d = 323.142 b = 67
NOTE- In case of single operands the result gets converted to int and then it is type
casted accordingly.
Example:
//Java program to illustrate type casting int to byte
class Test
{
public static void main(String args[])
{
byte b = 50;
//type casting int to byte
b = (byte)(b * 2);
System.out.println(b);
}
}
Run on IDE
Output
100