Core Concepts: Data Types, Variables, Control Statements & Arrays
1. Data Types in Java
Java is a statically-typed language, meaning all variables must be declared before use. A variable is a
reserved memory location to store a value. The data type of a variable determines the values it may
contain and the operations that may be performed on it.
Java data types are divided into two categories:
• Primitive Types – predefined by the language and named by reserved keywords.
• Non-Primitive (Reference) Types – such as arrays, classes, and interfaces.
1.1 Primitive Data Types
Java defines eight primitive types grouped into four categories:
• Integers: byte, short, int, long – for whole-valued signed numbers.
• Floating-point: float, double – for numbers with fractional precision.
• Character: char – represents symbols using Unicode (16-bit).
• Boolean: boolean – holds only true or false.
Default
Data Type Size Description
Value
boolean 1 bit false Logical true/false
char 2 bytes '\u0000' Unicode character
byte 1 byte 0 Signed 8-bit integer
short 2 bytes 0 Signed 16-bit integer
int 4 bytes 0 Signed 32-bit integer (most common)
long 8 bytes 0L Signed 64-bit integer
float 4 bytes 0.0f Single-precision 32-bit floating point
double 8 bytes 0.0d Double-precision 64-bit floating point
Integer Types – Details
• byte – Signed 8-bit type. Useful when working with streams of data from a network or file, or with raw
binary data. Range: –128 to 127. Example: byte b = 10;
• short – Signed 16-bit type. Useful to save memory in large arrays. Example: short s = 1000;
• int – Signed 32-bit type. The most commonly used integer type, used to control loops and index
arrays. Example: int a = 50000;
• long – Signed 64-bit type. Used when int is not large enough. Example: long a = 123456789L;
Floating-Point Types – Details
• float – Single-precision 32-bit. Useful when a fractional component is needed but high precision is not
critical. Example: float highTemp = 36.5f;
, • double – Double-precision 64-bit. Best choice when maintaining accuracy over many iterative
calculations or with large-valued numbers. Example: double pi = 3.14159;
Character and Boolean Types
• char – Java uses Unicode (16-bit) to represent characters. Example: char letterA = 'A';
• boolean – Can only hold true or false. This is the type returned by all relational operators. Example:
boolean b = true;
1.2 Wrapper Classes
Each primitive data type has a corresponding wrapper class in the java.lang package. These wrap
primitive types into objects, enabling them to be used in collections, generics, or anywhere objects are
required.
Primitive Wrapper Class
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
Java 5 introduced auto-boxing (automatic conversion of a primitive to its wrapper) and auto-unboxing
(automatic conversion of a wrapper back to its primitive).
2. Variables
A variable is the basic unit of storage in a Java program. It is defined by a combination of an identifier, a
type, and an optional initializer. All variables have a scope (visibility) and a lifetime.
Example declarations:
int a, b, c;
int d = 3, e, f = 5;
byte z = 22;
double pi = 3.14159;
char x = 'x';
2.1 Naming Rules
• Variable names are case-sensitive.
• Allowed characters: letters [A–Z, a–z], digits [0–9], $ (dollar sign), and _ (underscore).
• Identifiers cannot start with a digit. Example: 123geeks is invalid.
• Reserved words (e.g., while, int) cannot be used as identifiers. Java has 53 reserved words.
• Special characters like @ are not allowed. Example: geek@ is invalid.