Nav Tutorial

Best Java Data Types and Variables Interview Questions

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

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.

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):

This process occurs automatically when transforming a smaller data type into a larger one, such as converting an int to a long format.
This process is both 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.

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

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.

Scroll to Top
Verified by MonsterInsights