1. JAVA
1. What is JAVA?
Java is a high-level programming language and platform.
Platform: Any hardware or software environment in which a program
runs, known as a platform. Since Java has its own Runtime Environment
(JRE) and Application Programming Interface (API), it is called platform.
Developer: James Gosling from Sun Microsystems in 1991.
2. Why JAVA? or Features of JAVA
Java works on different hardware (Device Example: Laptop, System,
Mobile) and software (OS Example: Windows, Mac, Linux, Android).
It is easy to learn and simple to use.
It is open source and free.
It is OOPL.
3. Why JAVA is not fully OOPL?
In Java, OOPL because without class and object it is impossible to
write any Java program.
In Java, everything is an object, except primitive data types
because java doesn’t support primitive data type.
You can access the members of a static class without creating an
object of it.
4. What is JRE components?
JVM, Interpreter, JIT Compiler, Garbage Collector
2. 5. How Java is platform independent?
JDK it converts .java file into .class file.
This is known as compilation.
.class file is an unreadable file it is named as Bytecode.
This bytecode is given to JRE then JRE inside JVM takes care of
.class file.
JVM is instance of JRE.
JVM has JIT compiler with the help of Interpreter .class file is
converted into binary code.
6. Is JRE is platform independent or dependent?
It is a platform dependent and JVM also platform dependent.
7. What are all the backbone of Java language?
JDK, JVM and JRE.
8. Other Programming Vs Java Programming
In other programming languages: The compiler produces machine
code for a particular system.
In Java programming language; Java compiler produces code for a
Virtual Machine known as Java Virtual Machine.
First, Java code is compiled into bytecode.
This bytecode gets interpreted on different machines
Between host system and Java source, Bytecode is an intermediary
language.
JVM is responsible for allocating memory space.
3. 9. What is JVM?
The compiler compiles the Java file into a Java .class file, then that .class
file is input into the JVM, which loads and executes the class file. It
converts Java bytecode into machines language. JVM is instance of JRE.
JVM Architecture:
4. 10.How does JVM Works?
JVM is divided into three main subsystems:
1. ClassLoader Subsystem
1. Loading
1. BootStrap ClassLoader
2. Extension ClassLoader
3. Application ClassLoader
2. Linking
1. Verify
2. Prepare
3. Resolve
3. Initialization
2. Runtime Data Area
1. Method Area
2. Heap Area
3. Stack Area
1. Local Variable Array
2. Operand Stack
3. Frame Data
4. PC Register
5. Native Method Stacks
3. Execution Engine
1. Interpreter
2. JIT Compiler
1. Intermediate Code Generator
2. Code Optimizer
3. Target Code Generator
4. Profiler
3. Garbage Collector
4. Java Native Interface (JNI)/ Native Method Interface
5. Native Method Libraries
5. ClassLoader Subsystem
Details Description Types
Loading Classes will be loaded by this
component. It will follow
Delegation Hierarchy
Algorithm while loading the
class files.
BootStrap ClassLoader:
Responsible for loading classes
from the bootstrap classpath,
nothing but rt.jar. Highest priority
will be given to this loader.
Extension ClassLoader:
Responsible for loading classes
which are inside the ext
folder (JRElib).
Application ClassLoader:
Responsible for loading
Application Level classpath, path
mentioned Environment Variable,
etc.
Linking After class is loaded by the
classloader, linking is
performed. A bytecode
verifier will verify whether the
generated bytecode is proper or
not if verification fails we will
get a verification error. It also
performs the memory
allocation to static variables
and methods found in the class.
Verify:
Bytecode verifier will verify
whether the generated bytecode is
proper or not if verification fails
we will get the verification error.
Prepare: For all static variables
memory will be allocated and
assigned with default values.
Resolve:
All symbolic memory references
are replaced with the original
references from Method Area.
Initialization This is the final phase of
ClassLoading; here, all static
variables will be assigned with
the original values, and
the static block will be
executed.
No
6. Runtime Data Area/JVM Memory Area
Types Description
Method Area All the class-level data will be stored here, including static
variables. There is only one method area per JVM, and it is a
shared resource.
Heap Area All the Objects and their corresponding instance variables
and arrays will be stored here. There is also one Heap Area
per JVM. Since the Method and Heap areas share memory for
multiple threads, the data stored is not thread-safe.
Stack Area For every thread, a separate runtime stack will be created. For
every method call, one entry will be made in the stack
memory which is called Stack Frame. All local variables will
be created in the stack memory. The stack area is thread-safe
since it is not a shared resource.
1. Local Variable Array – Related to the method how
many local variables are involved and the
corresponding values will be stored here.
2. Operand stack – If any intermediate operation is
required to perform, operand stack acts as runtime
workspace to perform the operation.
3. Frame data – All symbols corresponding to the
method is stored here. In the case of any exception, the
catch block information will be maintained in the
frame data.
PC Registers Each thread will have separate PC Registers, to hold the
address of current executing instruction once the instruction
is executed the PC register will be updated with the next
instruction.
Native
Method
Stacks
Native Method Stack holds native method information. For
every thread, a separate native method stack will be created.
7. Execution Engine
Types Description
Interpreter The interpreter interprets the bytecode faster but executes
slowly. The disadvantage of the interpreter is that when one
method is called multiple times, every time a new
interpretation is required.
JIT
Compiler
The JIT Compiler neutralizes the disadvantage of the
interpreter. The Execution Engine will be using the help of the
interpreter in converting byte code, but when it finds repeated
code it uses the JIT compiler, which compiles the entire
bytecode and changes it to native code. This native code will
be used directly for repeated method calls, which improve the
performance of the system.
1. Intermediate Code Generator – Produces intermediate
code
2. Code Optimizer – Responsible for optimizing the
intermediate code generated above
3. Target Code Generator – Responsible for Generating
Machine Code or Native Code
4. Profiler – A special component, responsible for finding
hotspots, i.e. whether the method is called multiple
times or not.
Garbage
Collector
Collects and removes unreferenced objects. Garbage
Collection can be triggered by calling System.gc(), but the
execution is not guaranteed. Garbage collection of the JVM
collects the objects that are created.
Java Native Interface (JNI):
JNI will be interacting with the Native Method Libraries and provides the
Native Libraries required for the Execution Engine.
Native Method Libraries:
This is a collection of the Native Libraries, which is required for the
Execution Engine.
8. 11. What is JRE?
It is a software package which bundles the libraries (jars) and the Java
Virtual Machine, and other components to run applications written in the Java.
JVM is just a part of JRE distributions. To execute any Java application, you
need JRE installed in the machine. It’s minimum requirement to execute Java
applications on any machine.
JRE bundles the following components –
1. DLL files used by the Java HotSpot Client Virtual Machine.
2. DLL files used by the Java HotSpot Server Virtual Machine.
3. Code libraries, property settings, and resource files used by the Java
runtime environment. e.g. rt.jar and charsets.jar.
4. Java extension files such as localedata.jar.
5. Contains files used for security management. These include the security
policy (java.policy) and security properties (java.security) files.
6. Jar files containing support classes for applets.
7. Contains TrueType font files for use by the platform.
JREs can be downloaded as part of JDKs or you can download them separately.
JREs are platform dependent. It means that based on the type of machine (OS
and architecture), you will have to select the JRE bundle to import and install.
For example, you cannot install a 64-bit JRE distribution on 32-bit machine.
Similarly, JRE distribution for Windows will not work in Linux; and vice-versa.
9. 12.What is JDK?
JDK is a superset of JRE. JDK contains everything that JRE has along with
development tools for developing, debugging, and monitoring Java applications.
You need JDK when you need to develop Java applications.
Few important components shipped with JDKs are as follows:
appletviewer – this tool can be used to run and debug Java applets without
a web browser
apt – the annotation-processing tool
extcheck – a utility that detects JAR file conflicts
javadoc – the documentation generator, which automatically generates
documentation from source code comments
jar – the archiver, which packages related class libraries into a single JAR
file. This tool also helps manage JAR files
jarsigner – the jar signing and verification tool
javap – the class file disassembler
javaws – the Java Web Start launcher for JNLP applications
JConsole – Java Monitoring and Management Console
jhat – Java Heap Analysis Tool
jrunscript – Java command-line script shell
jstack – utility that prints Java stack traces of Java threads
keytool – tool for manipulating the keystore
policytool – the policy creation and management tool
xjc – Part of the Java API for XML Binding (JAXB) API. It accepts an
XML schema and generates Java classes
Same as JREs, JDKs are also platform dependent. So take care when you
download the JDK package for your machine.
10. 13. Why is Java both Interpreted and Compiled Language?
Programming languages are classified as
Higher Level Language Ex. C++, Java
Middle-Level Languages Ex. C
Low-Level Language Ex Assembly
Finally, the lowest level as the Machine Language.
A compiler is a program which converts a program from one level of language
to another. Example conversion of C++ program into machine code.
The java compiler converts high-level java code into bytecode (which is also a
type of machine code).
An interpreter is a program which converts a program at one level to another
programming language at the same level. Example conversion of Java program
into C++
In Java, the Just in Time Code generator converts the bytecode into the native
machine code which are at the same programming levels.
Hence, Java is both compiled as well as interpreted language.
11. 14. Difference between JDK Vs JRE VS JVM
Functions JDK JRE/Java RTE JVM
Abbreviation Java Development
Kit
Java Runtime
Environment
Java Virtual
Machine
Platform
Independent
No No No
Description It contains JRE +
development tools
like Java libraries,
Java source
compilers, java
debuggers, bundling
and deployment
tools.
It is used to provide
runtime
environment and is
an implementation
of JVM.
JRE = JVM +
Library Files
It is an abstract
machine. It is a
specification that
provides runtime
environment in
which java bytecode
can be executed.
Physical Exists Yes, It is physically
exists and contains
Java tools, JVM,
JRE.
Yes, It contains set
of libraries and
tools.
No, It provides run
time environment to
executes bytecode.
Examples Different Platform
needs different
JDKs.
JREs are also
developed and
released for various
platforms by
different companies
other than Sun
Micro Systems.
There are separate
JVMs for different
OS platforms like
Mac OS, Windows,
Linux etc.
12. 15. Difference between interpreter and JIT compiler?
Interpreter JIT Compiler
It interprets the bytecode line by line and
executes it sequentially.
It helps to execute bytecode faster by
compiling the bytecode to machine code
at runtime.
Execution speed is slower Execution speed is faster
Some Interpreter based languages:
PHP, Perl, Python, Ruby etc.
Used by Java
16. What are the features of Java?
Simple
Object Oriented
Portable
Platform Independent
Secured
Robust
Architectural Neutral
Interpreted
High Performance
Multithreading
Distributed
Dynamic
Simple:
Java is very easy to learn and its syntax is simple and also easy to
understand.
Java is simple programming language because it’s syntax is based on
C++, so it is easier for programmers to learn it after C++.
Java has removed many complicated and rarely used features. Example:
pointers, operator overloading etc.
There is no need to remove unreferenced objects because there is an
automatic garbage collection in java.
Object Oriented: (What is Object Oriented Programming?)
It is an OOPL and everything is an object.
It means we organize our software as a combination of different types of
objects that incorporates both data and behaviour.
13. OOPs is a methodology that simplifies software development and
maintenance by providing some rules.
Basic concept of OOPs is Object, Class, Polymorphism, Abstraction,
Inheritance, Encapsulation.
Platform Independent:
Java is a platform independent because it source code can run on all
operating systems.
Slogan for Java is Write Once Run Anywhere (WORA).
Java code is compiled by the compiler and converted into bytecode.
This bytecode is a platform independent code because it can run on
multiple platforms.
What is platform?
Any hardware or software environment in which a program runs, known
as a platform.
Since Java has its own Runtime Environment (JRE) and Application
Programming Interface (API), it is called platform.
Secured
Java is best known for its security and we can develop virus free systems.
It is secured because no explicit pointer and java programs run inside a
virtual machine.
In java there is no concept called pointers also hacking java application is
difficult.
OS
Users Runtime
Environment of its own
JVM
Java Application
14. Physical Address Logical Address
Address name is not changed. Address name is changed.
Route way Not route way
What is class loader?
It is a part of JRE which is used to load java classes into the JVM
dynamically.
It adds security by separating the packages for the classes of the local file
systems from those that are imported from network sources.
What is bytecode verifier?
It checks the code fragments for illegal code that can violate access right
to objects.
What is security manager?
It determines what resources a class can access such as reading and
writing to the local disk.
Robust:
It simply means strong.
Java is robust because it uses strong memory management.
There is a lack of pointers that avoids security problems.
There is an automatic garbage collection in java which runs on the JVM
to get rid of objects which are not being used by java application
anymore.
There are exception handling and type checking mechanism in java.
All these make java is robust.
Architectural Neutral:
Java is an architectural neutral because there are no implementation
dependent features. Example: size of primitive data type is fixed.
In C, integer data type occupies 2 bytes for 32-bit architecture and 4 bytes
for 64-bit architecture.
In java, integer data type occupies 4 bytes for both 32 and 64-bit
architecture.
Java doesn’t bother about processor architecture.
After compilation converting byte code into binary is done inside a JRE.
So, java is architecture neutral.
15. Portable:
Java is portable because it facilitates you to carry the java bytecode to any
platform.
It doesn’t require any implementation.
High Performance:
Java is faster than other traditional interpreted programming languages.
Java bytecode is “close” to native code.
It is little bit slower than the compiled language (C++).
Java is an interpreted language that is why it is slower that C++.
Distributed:
Java is distributed because it facilitates users to create distributed
applications in java.
Remote Method Invocation (RMI) and Enterprise Java Beans (EJB) are
used for creating distributed applications.
This feature in java makes us able to access files by calling the methods
from any machine on the internet.
Multi-threading:
A thread is like a separate programs executing concurrently.
We can write java programs that deal with many tasks at once by defining
multiple threads.
Main advantages of multithreading are that it doesn’t occupy memory for
each thread and it shares a common memory area.
Dynamic:
Java is a dynamic language.
It supports dynamic loading of classes are loaded on demand and
functions from C and C++.
It supports dynamic compilation and automatic memory management
(Garbage Collection).
17. What is IDE?
Integrated Development Kit (IDE) is a text editor. In java editors like Eclipse,
NetBeans, Notepad etc.
16. 18. Why Java is platform independent?
The most unique feature of java is platform independent.
In any programming language soruce code is compiled in to executable
code. This cannot be run across all platforms.
When javac compiles a java program it generates an executable file called
.class file. class file contains byte codes.
Byte codes are interpreted only by JVM’s. Since these JVM’s are made
available across all platforms by Sun Microsystems, we can execute this
byte code in any platform.
Byte code generated in windows environment can also be executed in
linux environment. This makes java platform independent.
19.What is difference between C++ and Java?
Java C++
It is platform independent Old days it is a platform dependent
but now a days it is platform
independent.
There are no pointers concepts There are pointers concepts
There is no operator overloading in
java
It has operator overloading
There is garbage collection in java There is no garbage collection in C++
Supports multithreading Doesn’t support multithreading
There are no templates in java There are templates in C++
There are no global variables in java There are global variables in C++
20.What is JIT compiler?
JIT compiler stands for Just in time compiler.
JIT compiler compiles byte code in to executable code. JIT a part of
JVM.
JIT cannot convert complete java program in to executable code it
converts as and when it is needed during execution.
21. What is bytecode in java?
When a javac compiler compiler compiles a class it generates .class file.
This .class file contains set of instructions called byte code.
Byte code is a machine independent language and contains set of
instructions which are to be executed only by JVM. JVM can understand
this byte codes.
17. 22. What are the difference between for loop and while loop?
Do While loop While loop For Loop
It is used to
iterate(repetition) a part of
the program several times.
(Exit controlled / Post
Test )
It is used to
iterate(repetition) a part
of the program several
times. (Entry controlled /
Pre Test)
It is used to number of
iterations.
If the number of iteration
is not fixed and you must
have to execute the loop at
least once, it is
recommended to use do
while loop.
If the number of iteration
is not fixed, it is
recommended to use
while loop.
If the number of
iterations is infinite times.
Syntax:
do
{
//code to be executed
}
while(condition)
while(condition)
{
//code to be executed
}
for(initialization;
condition;
increment/decrement)
{
//code to be executed
}
Flow Chart:
Statement
conditionStatement
condition
do
False
True
True False condition
Statement
Increment/Decrement
Initialization
True
False