Java Basics: Understanding Primitive Data Types from Scratch
This article explains Java's primitive data types—including integer, floating‑point, character, and boolean types—covers their memory sizes, value ranges, literal syntax, automatic and explicit type conversions, and provides concrete code examples and tables for reference.
Primitive Types
Java data types are divided into primitive (basic) types and reference (constructed) types. Primitive types include integer, floating‑point, character, and boolean types.
Integer Types
Four integer types are defined:
byte – 1 byte, range –128 to 127
short – 2 bytes, range –32768 to 32767
int – 4 bytes, range –2³¹ to 2³¹‑1
long – 8 bytes, range –2⁶³ to 2⁶³‑1
Example of a byte overflow:
public class ChangLiang {
public static void main(String[] args) {
// Declare a byte variable and assign 37
byte teacherAge = 37;
// Error: value exceeds byte range
teacherAge = 129;
System.outprintln("teacherAge=" + teacherAge);
}
}Assigning a value that exceeds the int range to a long variable requires an L suffix:
// Value exceeds int range, needs L
long num = 3456789101L;
// Value within int range, L optional
long num = 34567891L;
// Within int range, no suffix needed
long num = 34567891;Omitting the L suffix for a value larger than int causes a compile‑time error:
long num = 3456789101; // compile‑time errorFloating‑Point Types
Two floating‑point types exist: float (single precision) and double (double precision). The default literal type is double; float literals require an F suffix.
float f = 345.67F; // float literal requires F suffix
double d = 3456.7; // double is default
double d2 = 3456.7D; // D suffix optionalBoth types can also hold integer literals:
float f = 345; // valid, integer assigned to float
double d = 3456; // valid, integer assigned to doubleCharacter Type
The char type stores a single Unicode character in 2 bytes. Declaration uses single quotes:
char dog = '狗';
char symbol = '$';Boolean Type
Boolean values are true or false and are declared with boolean:
boolean yesOrNo = false;
yesOrNo = true;Type Conversion
Automatic (implicit) conversion
Smaller‑range types ( byte, short, char) are automatically promoted to larger types when assigned or used in arithmetic.
float price = 36; // int 36 is promoted to float 36.0
System.out.println("price=" + price); // prints price=36.0If only byte, short, or char participate, the result is an int after promotion:
byte b = -12;
short s = -345;
char c = 'a';
int result = b + s + c; // result is int
System.out.println("result = " + result); // prints result = 454If an int, long, float or double participates, the expression is promoted to the highest‑ranked type:
byte b = 12;
int i = 345;
float f = 67.8F;
double d = b + i + f; // b and i promoted to double
System.out.println("d = " + d); // prints d = 424.8Explicit (casting) conversion
Assigning a larger‑range value to a smaller‑range variable requires a cast:
int num = (int) 34.5; // truncates to 34
System.out.println("num=" + num); // prints num=34Explicit casts can be used in mixed‑type arithmetic to force a higher‑ranked operand to a lower type before the operation:
byte b = 12;
int i = 345;
float f = 678.9F;
int result = b + i + (int) f; // f cast to int (678)
System.out.println("result = " + result); // prints result = 1035Escape Characters
Escape sequences represent non‑printable characters in char literals or strings. \n – new line \t – horizontal tab \v – vertical tab \b – backspace \r – carriage return \f – form feed \\ – backslash \' – single quote \" – double quote \d – octal character \xd – hexadecimal character
Example using a newline escape character:
public class CharConstantTest {
public static void main(String[] args) {
// Define a char constant with a newline escape
char newline = '
';
System.out.println("Line1" + newline + "Line2");
}
} Line1
Line2Lisa Notes
Lisa's notes: musings on daily life, work, study, personal growth, and casual reflections.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
