Fundamentals of Core Java FAQ's

Java is a simple programming language. Writing, compilation, and debugging a program is very easy in Java. It helps to create reusable code.

  • Platform Independent – The source code is converted into bytecode using the Java compiler. This bytecode can be used to run the application on any platform such as Windows, Mac, Linux, etc.
  • Open Source – The source code is available to the general public for use and/or modification from its original design at no cost.
  • Multithreading – Java supports multithreading, which enables a program to perform several tasks simultaneously.
  • More Secure – It provides a virtual firewall between the application and the computer, preventing unauthorized access.
  • Portable – Java follows the "Write Once, Run Anywhere" principle, meaning code written on one machine can run on another machine without modification.
  • JDK (Java Development Kit) – Contains JRE + development tools. Required to run and develop Java applications.
  • JRE (Java Runtime Environment) – Contains JVM + library files. Provides the environment to run Java applications.
  • JVM (Java Virtual Machine) – Allocates memory and compiles code. It is the heart of Java's platform independence.

Objects are stored in heap memory. Heap memory exists inside the JVM, and the JVM runs inside the RAM.

Garbage Collection makes Java memory efficient because the garbage collector removes unreferenced objects from heap memory.

Access Specifiers are:

  • Public – Global level access; accessible from the same package and different packages.
  • Private – Class level access; accessible only within the class.
  • Default – Package level access; accessible within the package.
  • Protected – Accessible inside the package using object creation & inheritance, and outside the package using only inheritance.

In Java, data types define the type of data a variable can hold. Java has two main categories of data types:

  • Primitive Data Types – Built-in types and Java keywords. There are 8 primitive data types: byte, short, int, long, double, float, char, boolean.
  • Non-Primitive Data Types – Reference types created by the programmer that refer to objects. Examples: String, Arrays, Classes, etc.

The Scanner class is used to get input from the user. We create a Scanner object, pass System.in as a parameter, and call methods like nextInt() to get integers or next()/nextLine() to get strings.

Wrapper classes are classes for primitive data types. They are used to convert primitive types into objects.

The final keyword is used to restrict modification:

  • final variable → Value cannot be changed (constant).
  • final method → Cannot be overridden in a subclass.
  • final class → Cannot be extended (no child class).
  • Belongs to the class, not the object.
  • static variable → Shared across all objects.
  • static method → Can be called without creating an object.
  1. Local Variable – Declared inside a method, constructor, or block. Must be initialized before use.
  2. Instance Variable – Declared inside a class but outside any method. Belongs to the object (each object gets its own copy) and is initialized to default values if not set.
  3. Static Variable (Class Variable) – Declared using the static keyword. Shared across all objects of the class. Memory is allocated once during class loading.
  • public – Access modifier; JVM needs to access the method from anywhere, so it must be public.
  • static – No need to create an object to call main(); JVM calls main() without creating an object of the class.
  • void – Return type; main() does not return any value to the JVM.
  • main – Starting point of execution; this method name is fixed for JVM to start the program.
  • String[] args – Array of String type; used to accept command-line arguments from the user.
  • Pascal Case (Pascal Notation) – First letter of every word is capitalized, with no underscores or spaces. Used for: Class names, Interface names.
  • Camel Case (Camel Notation) – First letter of the first word is lowercase, others are capitalized, with no underscores or spaces. Used for: Variable names, Method names, Object names.

finalize() is a method in Java used for garbage collection. It is called by the Garbage Collector before destroying an object.

Yes, the program will successfully execute. In Java, there is no specific rule for the order of specifiers.

The code will compile successfully, but the JVM will throw a runtime error: NoSuchMethodError: main. Reason: JVM requires public static void main(String[] args) as the entry point. Since the method isn’t static, it cannot be called without creating an object — but no object is created by the JVM.

Yes, the main method can be overloaded. You can define multiple main() methods with different parameter types.

No, private methods cannot be overridden because they are not inherited by subclasses and are accessible only within the class where they are defined.