Loading content...
Loading content...
A data type defines the type of data that a variable can store.
Predefined by Java
Store a single value
Efficient in memory and performance
Created by programmer or provided by Java
Store references (addresses of objects)
Can hold multiple values
String
Array
Wrapper Classes
Collections
Class
Interface
Enum
Annotation
A variable is a container used to store data in memory.
<data_type> variable_name = value;Declared inside class but outside methods
Stored in heap memory
Default values assigned by JVM
Types:
Instance Variable
Static Variable
Declared inside methods, blocks, or constructors
Stored in stack memory
Must be initialized before use
Accessible only within their scope
Used to declare constants. Value cannot be changed.
final int a = 10;Stores objects and class-level variables
Stores method calls and local variables
Each method has its own stack frame
A literal is a fixed value assigned to a variable.
Values without decimal points.
int a = 10;Types:
Decimal → 10 (0-9)
Octal → 0123 (0-7)
Hexadecimal → 0x1A (0-9 a-f)
Binary → 0b1010
Octal:
Digits must be between 0–7
Must start with 0
Hexadecimal:
Digits: 0–9, A–F
Must start with 0x or 0X
Binary:
Only 0 and 1
Must start with 0b or 0B
Decimal values.
double d = 12.34;
float f = 12.34f;Scientific notation:
double d = 1.2e3; // 1200boolean b = true;Valid values:
true
false
Invalid:
boolean b = 1; // not allowedSingle character enclosed in single quotes.
char ch = 'A';Valid:
'A', '1', '\n', '\t'
Invalid:
''
'AB'
Enclosed in double quotes.
String s = "Hello";Note:
String is not a primitive data type
It is a class from java.lang package
Represents absence of value.
String s = null;Default value for reference types
Not equal to 0