1. What is the difference between JDK and JRE?
JDK stands for Java Development Kit. It contains the tools and libraries for development of Java programs. It also contains compilers and debuggers needed to compile Java program, JRE stands for Java Runtime Environment. This is included in JDK. JRE provides libraries and JVM that is required to run a Java program.
2. What is Java Virtual Machine (JVM)?
Java Virtual Machine (JVM) is an abstract machine that executes Java Bytecode. There are different JVM for different hardware and software platforms. So JVM is platform dependent. JVM is responsible for loading, verifying and executing the Bytecode on a platform.
3. What are the different types of memory areas allocated by JVM?
In java, JVM allocates memory to different processes, methods and objects. Some of the memory areas allocated by JVM are:
- ClassLoader: It is a component of JVM used to load class files.
- Class (Method) Area: It stores per-class structures such as the runtime constant pool, field and method data, and the code for methods.
- Heap: Heap is created a runtime and it contains the runtime data area in which objects are allocated.
- Stack: Stack stores local variables and partial results at runtime. It also helps in method invocation and return value. Each thread creates a private JVM stack at the time of thread creation.
- Program Counter Register: This memory area contains the address of the Java virtual machine instruction that is currently being executed.
- Native Method Stack: This area is reserved for all the native methods used in the application.
4. What is JIT compiler?
Just In Time compiler also known as JIT compiler is used for performance improvement in Java. It is enabled by default. It is compilation done at execution time rather earlier. Java has popularized the use of JIT compiler by including it in JVM.
5. How Java platform is different from other platforms?
Java is a platform independent language. Java compiler converts Java code in to byte code that can be interpreted by JVM. There are JVM written for almost all the popular platforms in the world. Java byte code can run on any supported platform in same way. Where as other languages require libraries compiled for a specific platform to run.
6. Why people say that Java is ‘write once and run anywhere’ language?
You can write Java code on Windows and compile it in Windows platform. The class and jar files that you get from Windows platform can run as it is on Unix environment. So it is a truly platform independent language. Behind all this portability is Java byte code. Byte code generated by Java compiler can be interpreted by any JVM. So it becomes much easier to write programs in Java and expect those to run on any platform. Java compiler javac compiles java code and JVM java runs that code.
7. How does ClassLoader work in Java?
In Java, ClassLoader is a class that is used to load files in JVM. ClassLoader loads files from their physical file locations e.g. Filesystem, Network location etc. There are three main types of ClassLoaders in Java.
- Bootstrap ClassLoader: This is the first ClassLoader. It loads classes from rt.jar file.
- Extension ClassLoader: It loads class files from jre/lib/ext location.
- Application ClassLoader: This ClassLoader depends on CLASSPATH to find the location of class files. If you specify your jars in CLASSPATH, then this ClassLoader will load them.
8. Do you think ‘main’ used for main method is a keyword in Java?
No, main is just a name of method. There can be multiple methods with same name main in a class file. It is not a keyword in Java.
9. Can we write main method as public void static instead of public static void?
No, you cannot write it like this. Any method has to first specify the modifiers and then the return value. The order of modifiers can change.
We can write static public void main() instead of public static void main().
10.In Java, if we do not specify any value for local variables, then what will be the default value of the local variables?
Java does not initialize local variables with any default value. So these variables will be just null by default.
11. Let say, we run a java class without passing any arguments. What will be the value of String array of arguments in Main method?
By default, the value of String array of arguments is empty in Java. It is not null.
12.What is the difference between byte and char data types in Java?
Both byte and char are numeric data types in Java. They are used to represent numbers in a specific range.
Major difference between them is that a byte can store raw binary data where as a char stores characters or text data. Usage of char is E.g. char ch = ‘x’;
Byte values range from -128 to 127. A byte is made of 8 bits. But a char is made of 16 bits. So it is equivalent to 2 bytes.
That’s all about Java Basic Interview Questions and Answers.