Java Basics



JAVA BASICS

1) What is JAVA?

Java is a high-level programming language and is platform independent.

Java is a collection of objects. It was developed by Sun Micro-systems. There are a lot of applications, websites and Games that are developed using Java.

2) Explain JDK, JRE and JVM?


JDK


  • JDK is a software development kit to develop applications in Java.
  • JDK is platform dependent.
  • It contains tools for developing, debugging, and monitoring java code.
  • It is the superset of JRE
  • The JDK enables developers to create Java programs that can be executed and run by the JRE and JVM
  • JDK comes with the installer.


JRE


  • It is a software bundle which provides Java class libraries with necessary components to run Java code.
  • JRE is also platform dependent.
  • It contains class libraries and other supporting files that JVM requires to execute the program.
  • It is the subset of JDK.
  • The JRE is the part of Java that creates the JVM.
  • JRE only contain environment to execute source code.



JVM


  • JVM executes Java byte code and provides an environment for executing it.
  • JVM is platform-independent.
  • Software development tools are not included in JVM.
  • JVM is a subset of JRE.
  • It is the Java platform component that executes source code.
  • JVM bundled in both software JDK and JRE.


3) What is JVM (Java Virtual Machine) Architecture?

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

(i.e. JVM is platform dependent).



As shown in the above architecture diagram JVM is divided into three main subsystems


Class Loader Subsystem


Runtime Data Area


Execution Engine


1. Class Loader Subsystem


Java’s dynamic class loading functionality is handled by the class loader subsystem. It loads, links and initializes the class when it refers to a class for the first time at runtime, not at compile-time. It performs three major functionality such as Loading, Linking, and Initialization.


1.1 Loading 


Classes will be loaded by this component. BootStrap ClassLoader, Extension ClassLoader, Application ClassLoader are the three class loader which will help in achieving it.


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 ext folder (jre\lib)


Application ClassLoader –Responsible for loading Application Level Classpath, path mentioned Environment Variable etc.


The above Class Loaders will follow Delegation Hierarchy Algorithm while loading the class files.


1.2 Linking


Verify – Bytecode verifier will verify whether the generated bytecode is proper or not if verification fails we will get 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.


1.3 Initialization


This is the final phase of Class Loading, here all static variable will be assigned with the original values and static block will be executed.


2. Runtime Data Area


Runtime Data Area is divided into 5 major components


Method Area – All the Class level data will be stored here including static variables. Method Area is one per JVM and it is a shared resource.


Heap Area – All the Objects and its corresponding instance variables and arrays will be stored here.


Heap Area is also one per JVM since Method area and Heap area shares 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 as Stack Frame. All local variables will be created in the stack memory. Stack area is thread safe since it is not a shared resource. Stack Frame is divided into three sub-entities such as


Local Variable Array – Related to the method how many local variables are involved and the corresponding values will be stored here.


Operand stack – If any intermediate operation is required to perform, operand stack act as runtime workspace to perform the operation.


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 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, separate native method stack will be created.


3. Execution Engine


The bytecode which is assigned to the Runtime Data Area will be executed by the Execution Engine. The Execution Engine reads the byte code and executes one by one.


Interpreter – Reads the bytecode, interprets it and executes it one by one. The interpreter interprets the bytecode faster but executes slowly. The disadvantage of the interpreter is that when one method called multiple times, every time interpretation is required.


JIT Compiler – JIT Compiler neutralizes the disadvantage of the Interpreter ( a single method called multiple times, each time interpretation is required ), The Execution Engine will be using the help of Interpreter in converting but when it found repeated code it uses 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.


Intermediate Code generator – produces intermediate code


Code Optimizer – Code Optimizer is responsible for optimizing the intermediate code generated above


Target Code Generator – Target Code Generator is responsible for Generating Machine Code/ Native Code


Profiler – Profiler is a special component, it is responsible for finding the hotspots (i.e) Used to identify whether the method is called multiple time or not.


Garbage Collector :


Garbage Collector is a part of Execution Engine, it collects/removes the unreferenced objects. Garbage Collection can be triggered by calling “System.gc()”, but the execution is not guaranteed. Garbage collector of JVM collects only those objects that are created by new keyword. So if you have created any object without new, you can use finalize method to perform cleanup.


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 : It is a Collection of the Native Libraries which is required for the Execution Engine.



4) How does Java enable high performance?

Java uses Just In Time compiler to enable high performance. JIT is used to convert the instructions into bytecodes.


5) List the features of Java Programming language.


There are the following features in Java Programming Language.


Simple: Java is easy to learn. The syntax of Java is based on C++ which makes easier to write the program in it.


Object-Oriented: Java follows the object-oriented paradigm which allows us to maintain our code as the combination of different type of objects that incorporates both data and behavior.


Portable: Java supports read-once-write-anywhere approach. We can execute the Java program on every machine. Java program (.java) is converted to bytecode (.class) which can be easily run on every machine.


Platform Independent: Java is a platform independent programming language. It is different from other programming languages like C and C++ which needs a platform to be executed. Java comes with its platform on which its code is executed. Java doesn't depend upon the operating system to be executed.


Secured: Java is secured because it doesn't use explicit pointers. Java also provides the concept of ByteCode and Exception handling which makes it more secured.


Robust: Java is a strong programming language as it uses strong memory management. The concepts like Automatic garbage collection, Exception handling, etc. make it more robust.


Architecture Neutral: Java is architectural neutral as it is not dependent on the architecture. In C, the size of data types may vary according to the architecture (32 bit or 64 bit) which doesn't exist in Java.


Interpreted: Java uses the Just-in-time (JIT) interpreter along with the compiler for the program execution.


High Performance: Java is faster than other traditional interpreted programming languages because Java bytecode is "close" to native code. It is still a little bit slower than a compiled language (e.g., C++).


Multithreaded: We can write Java programs that deal with many tasks at once by defining multiple threads. The main advantage of multi-threading is that it doesn't occupy memory for each thread. It shares a common memory area. Threads are important for multi-media, Web applications, etc.


Distributed: Java is distributed because it facilitates users to create distributed applications in Java. RMI and EJB are used for creating distributed applications. This feature of Java makes us able to access files by calling the methods from any machine on the internet.


Dynamic: Java is a dynamic language. It supports dynamic loading of classes. It means classes are loaded on demand. It also supports functions from its native languages, i.e., C and C++.



Java Variables

* A variable is a container which holds the value while the java    n     program is executed. 


* A variable is assigned with a datatype.


* Variable is a name of memory location. There are three types of        variables in java: local, instance and static.


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 instance variable. It is not declared as static.


It is called instance variable because its value is instance specific and is not shared among instances.


3) Static variable



A variable which is declared as static is called static variable. It cannot be local. You can create a single copy of static variable and share among all the instances of the class. Memory allocation for static variable happens only once when the class is loaded in the memory.

Example:


class Test

{  
int data=50;//instance variable  
static int m=100;//static variable  
void method(){  
int n=90;//local variable  
}  

}


Explain about System.out.println(); ? 

Ex: 1
class System{
static PrintStream out;} System.out.Println(“Hello”);


*System is class present in java.lang.package
*’out’ is a static variable present in system class of type PrintStream
*Println() is a method present in PrintStream class


Ex:2
class Test{
static String s=”java”;} Test.s.length();
*Test is a class name  * ‘s’ is a static variable present in Test class of type String.  * length()is a method present in String class.



Explain about public static void main (String [] args) ?

public : To call by JVM from anywhere
static : Without existing object also JVM has to call this metod and main method no way related to any object
void : main() method won’t return anything to JVM
main : This is name which is configured inside JVM
String[] args : command line arguments