tags : Java

What are data types in Java?

Data types specify the type of data a variable can hold.
Java is a strongly typed language, so each variable must have a declared type.

What are the main categories of data types in Java?

  1. Primitive data types (8 types)
  2. Non-primitive (Reference) data types like arrays, classes, strings, etc.

What are the 8 primitive data types in Java?

TypeSizeExampleDefault Value
byte1 byte-128 to 1270
short2 bytes-32,768 to 32,7670
int4 bytesCommonly used for integers0
long8 bytesLarger integer values0L
float4 bytesDecimal values with less precision0.0f
double8 bytesDecimal values with more precision0.0d
char2 bytesSingle character ('A')\u0000
boolean1 bittrue or falsefalse

Why is String not a primitive data type in Java?

Because it is a class in Java. It holds a sequence of characters, and is part of the reference data types.

What is type casting in Java?

Type casting is converting one data type into another. There are two types:

  • Implicit (smaller to larger): int x = 10; double y = x;
  • Explicit (larger to smaller): double d = 10.5; int x = (int) d;

What happens during overflow and underflow in primitive types?

  • Underflow: when it goes below the min limit.
  • Overflow: when a value exceeds the max limit, it wraps around.
byte b = 127;
b++;  // becomes -128

Is char a numeric type in Java?

Yes. char holds a Unicode number, so it can be treated like an integer.

char c = 'A';       // 65
int code = c + 1;   // 66

What is the range of an int in Java?

  • -2,147,483,648 to 2,147,483,647
  • 32-bit signed integer (uses 2’s complement)

What is the size of a boolean in Java?

Although it logically holds only true or false, its exact memory size is not precisely defined by the Java specification. JVM handles it internally, typically as 1 byte. 8 bit = 1 byte.