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
byte: The byte data type is an 8-bit signed two’s complement integer. Byte helps save memory.
short: The short data type is a 16-bit signed two’s complement integer.
int: The int data type is a 32-bit signed two’s complement integer.
long: The long data type is a 64-bit two’s complement integer. The signed long has a minimum value of -263 and a maximum value of 263-1.
float: The float data type is a single-precision 32-bit floating point.
double: The double data type is a double-precision 64-bit floating point.
boolean: The boolean data type has only two possible values: true and false. We can use this data type for simple flags that track true/false conditions.
char: The char data type is a single 16-bit Unicode character.
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.
2. What is type casting in Java? Explain implicit and explicit casting.
Type Casting:
Type casting is converting one data type into another.
Implicit Casting (Widening Conversion):
- This process occurs automatically when transforming a smaller data type into a larger one, such as converting an int to a long format.
- Safe and lossless.
Example:
int num = 20;
long bigNum = num; // Implicit casting
Explicit Casting (Narrowing Conversion):
- Converting a larger type to a smaller one requires manual casting.
- This process can be risky due to the potential loss of data.
Example:
double price = 29.99;
int intPrice = (int) price; // Explicit casting, value becomes 29.
3. 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
byte (8-bit) has a range from -128 to 127
short (16-bit) has a range from -32,768 to 32,767
int (32-bit) has ranged from -2,147,483,648 to 2,147,483,647
long (64-bit) has ranged from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
4. Difference Between final, static, and const in Java
Finals are used to declare constants, prevent method overriding, and restrict inheritance. static belongs to the class rather than instances (objects). 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 final, 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.
5. 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:
Automatic conversion of a wrapper class to a primitive type.
Example:
Integer obj = 15;
int num = obj; // Unboxing
6. 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.
7. 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.
8. 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.
