Java Interview Questions

Previous Java Interview Questions Next

Variables and Data Types

1. What are the different types of variables in Java?

Java has three main types of variables: Instance Variables (non-static fields that belong to objects), Static Variables (class-level variables shared by all instances), and Local Variables (declared within methods, constructors, or blocks). Instance variables have default values, while local variables must be initialized before use.

2. Explain Java's primitive data types and their sizes.

Java has eight primitive data types: byte (8-bit), short (16-bit), int (32-bit), long (64-bit) for integers; float (32-bit) and double (64-bit) for floating-point numbers; char (16-bit Unicode) for characters; and boolean (true/false values). Each has a specific range and default value.

3. What is the difference between primitive and reference data types?

Primitive types store actual values directly in memory and include byte, short, int, long, float, double, char, and boolean. Reference types store memory addresses (references) to objects and include classes, interfaces, arrays, and enums. Primitives are stored on the stack, while objects are stored on the heap.

4. What is type casting in Java? Explain implicit and explicit casting.

Type casting converts one data type to another. Implicit casting (widening) happens automatically when converting from smaller to larger types (e.g., int to long). Explicit casting (narrowing) requires manual intervention when converting from larger to smaller types (e.g., double to int) and may result in data loss.

5. What are the default values for different data types in Java?

Instance and static variables have default values: byte, short, int, long default to 0; float, double default to 0.0; char defaults to '\u0000'; boolean defaults to false; and reference types default to null. Local variables have no default values and must be initialized.

6. What is the difference between instance variables and static variables?

Instance variables are created when an object is instantiated and each object has its own copy. Static variables belong to the class rather than any object and are shared among all instances. Instance variables are accessed through object references, while static variables are accessed using the class name.

7. What is the 'final' keyword when used with variables?

The final keyword makes variables constant and unchangeable. A final variable must be initialized either at declaration or in the constructor and cannot be modified afterward. Final instance variables must be set before the constructor completes. Final static variables are constants that belong to the class.

8. What is autoboxing and unboxing in Java?

Autoboxing is the automatic conversion of primitive types to their corresponding wrapper classes (e.g., int to Integer). Unboxing is the automatic conversion of wrapper classes back to primitives (e.g., Integer to int). This feature was introduced in Java 5 to simplify code working with both primitives and objects.

9. What are wrapper classes and why are they used?

Wrapper classes (Integer, Double, Boolean, etc.) wrap primitive values in objects. They are used when objects are required instead of primitives, such as in Collections API, and provide utility methods for conversion and comparison. Each primitive type has a corresponding wrapper class in the java.lang package.

10. What is variable scope in Java?

Variable scope defines where a variable can be accessed. Instance variables have class-level scope, static variables have class-level scope shared across instances, and local variables have method/block-level scope. Local variables are only accessible within the block they are declared in and are destroyed when the block exits.

11. What is the difference between 'var' and explicit data type declaration?

Java 10 introduced var for local variable type inference, allowing the compiler to infer the type from the initializer. However, var can only be used for local variables with explicit initialization and cannot be used for method parameters, return types, or instance variables. Explicit declaration provides clarity but var reduces boilerplate code.

12. What are the memory allocations for different variable types?

Instance variables are stored in the heap as part of the object, static variables are stored in the method area (part of heap), and local variables are stored in the stack. Primitive local variables store values directly on the stack, while reference local variables store heap addresses on the stack.

Previous Java Interview Questions Next