java

Java Data Types


1. Primitive Data Types

Data TypeSizeDefault ValueDescription
byte1 byte0Stores whole numbers from -128 to 127.
short2 bytes0Stores whole numbers from -32,768 to 32,767.
int4 bytes0Stores whole numbers from -2,147,483,648 to 2,147,483,647.
long8 bytes0LStores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Add L or l suffix for long literals (e.g., 1234L).
float4 bytes0.0fStores fractional numbers. Precision: Up to 7 decimal digits. Add f or F suffix for float literals (e.g., 3.14f).
double8 bytes0.0dStores fractional numbers. Precision: Up to 15 decimal digits. Add d or D suffix for double literals (e.g., 3.14159d).
char2 bytes'\u0000'Stores a single 16-bit Unicode character. Example: 'A' or '\u0041'.
boolean1 bit (not precisely defined)falseStores true or false. Size depends on JVM implementation (typically 1 bit or 1 byte).

2. Non-Primitive Data Types

TypeSizeDefault ValueDescription
StringDepends on contentnullStores a sequence of characters. Immutable (cannot be changed after creation).
ArraysDepends on length & typenullStores a collection of elements of the same type.
ClassDepends on object fieldsnullRepresents user-defined types.
InterfaceN/AnullRepresents a contract that classes can implement.

Key Points

  • Wrapper Classes: Each primitive type has a corresponding wrapper class (Byte, Short, Integer, Long, Float, Double, Character, Boolean) to use primitives as objects.
  • Default Values: Non-primitive data types default to null.
  • Ranges of Integer Types: Derived from -2^(n-1) to 2^(n-1)-1, where n is the number of bits.
  • Floating-Point Representation: Based on IEEE 754 standard.
  • Type Conversion: Implicit conversions (widening) occur in compatible types (e.g., int to long), but explicit casting is required for narrowing (e.g., double to float).

Leave a Reply

Your email address will not be published. Required fields are marked *