Java Data Types and Variables Interview Question
Here are the most asked Java Data Types & Variables Interview Questions.
1.What are primitive and non-primitive data types in Java?
Primitive Data Types
Primitive data types in Java deal with primitive numerical data types. These are int, long, short, byte, float, and double, allowing us to represent integer and real numbers.
Basic Types
Non-Primitive Data Types
Non-primitive data types are not defined in Java, but programmers create them. We also refer to them as reference variables or object references, denoting a memory location that stores data.
Examples:
strings, arrays, and classes.
What is type casting in Java? Could you please explain implicit and explicit casting?
Type Casting:
Type casting is converting one data type into another.
Implicit Casting (Widening Conversion):
Example:
int num = 20;
long bigNum = num; // Implicit casting
Explicit Casting (Narrowing Conversion):
Example:
double price = 29.99;
int intPrice = (int) price; // Explicit casting, value becomes 29.
How Does Java Handle Integer Overflow?
In Java, an integer overflow occurs when an arithmetic operation results in a value that exceeds the maximum or minimum limit of the data type.
Integer Overflow in Java
Difference Between final, static, and const in Java
Java uses finals to declare constants, prevent method overriding, and restrict inheritance. The static keyword indicates that a member belongs to the class itself rather than to any specific instance (object) of that class. const is not a keyword in Java.
The final keyword turns a variable into a constant, preventing any changes to its value after assignment. static Makes a variable shared across all instances of a class. Java does not use const.
final prevents method overriding by subclasses. You can use static with the class’s static methods, but const is not applicable.
In the final instance, variables are stored separately for each object. Static variables are stored in the class memory and shared among all objects.
In the end, you want to create constants, prevent overriding, or restrict inheritance. In static, you want a variable or method to belong to the class rather than individual objects.
What are autoboxing and unboxing?
Autoboxing:
Autoboxing involves the automatic conversion of a primitive type to its corresponding wrapper class.
Example:
int num = 8;
Integer obj = num; // Autoboxing
Unboxing:
The system automatically transforms a wrapper class into a primitive type.
Example:
Integer obj = 15;
int num = obj; // Unboxing
What is a final variable in Java?
In Java, a final variable is a value that cannot be changed once assigned. This means that after initialization, the value remains constant throughout the program.
In Java, final static variables behave like global constants.
How is memory allocated for primitive vs. object types?
Primitive data types are stored in stack memory, whereas object types are stored as references in the stack.
In primitive data types, memory size is fixed, whereas in object types, memory is dynamic and depends on storage.
It is faster (direct value access) in primitive data types but slower in object types.
How Does Java Handle Overflow in Integer Calculations?
Silently wraps around using two’s complement arithmetic.
Java does not generate an error on overflow.
To handle this, use Math class methods like Math.addExact(), which throws exceptions on overflow.