Java Interview Question Series Part 2

Shamsher Ahmed
2 min readOct 9, 2022

6. Why do people say that Java is a ‘write once and run anywhere’ language?

You can write Java code on Windows and compile it on the Windows platform. The class and jar files that you get from the Windows platform can run as it is on a Unix environment. So it is a truly platform-independent language.

Behind all this portability is Java byte code. Byte code generated by the 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.

  1. Bootstrap ClassLoader: This is the first ClassLoader. It loads classes from the rt.jar file.
  2. Extension ClassLoader: It loads class files from jre/lib/ext location.
  3. 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 the main method is a keyword in Java?

No, the main is just the name of the method. There can be multiple methods with the same name main in a class file. It is not a keyword in Java.

9. Can we write the main method as public void static instead of the 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 a static public void main() instead of a public static void main().

10. 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.

The 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.

--

--