Fundamentals
Java Fundamentals
Variables & Data Types
1. Variables
A variable is a named memory location used to store data.
java
1int age = 25;2String name = "Akash";| Concept | Example | Meaning |
|---|---|---|
| Declaration | int age; | Creates a variable |
| Initialization | age = 25; | Assigns a value |
| Declaration + Initialization | int age = 25; | Both together |
| Reassignment | age = 26; | Changes the value |
2. Primitive Data Types
Java has 8 primitive data types.
| Type | Size | Example | Default Value* |
|---|---|---|---|
byte | 8-bit | byte x = 10; | 0 |
short | 16-bit | short x = 100; | 0 |
int | 32-bit | int x = 1000; | 0 |
long | 64-bit | long x = 100000L; | 0L |
float | 32-bit | float x = 10.5f; | 0.0f |
double | 64-bit | double x = 10.5; | 0.0d |
char | 16-bit | char c = 'A'; | '\u0000' |
boolean | JVM-dependent | boolean flag = true; | false |
Note: Default values apply to instance/static variables, not local variables.
3. Reference Data Types
Reference variables store a reference to an object.
| Type | Example |
|---|---|
String | String name = "Akash"; |
| Array | int[] nums = {1, 2, 3}; |
| Class | User user = new User(); |
| Interface | List<Integer> list = new ArrayList<>(); |
| Enum | Day day = Day.MONDAY; |

4. Primitive vs Reference
| Primitive | Reference |
|---|---|
| Stores actual value | Stores reference to an object |
| 8 built-in types | Classes, arrays, interfaces, enums, etc. |
Cannot be null | Can be null |
| Generally more lightweight | Objects may require heap allocation |
int x = 10 | String s = "Hello" |
Operators
Operators are symbols used to perform operations on variables and values.
1. Types of Operators
| Category | Operators | Example |
|---|---|---|
| Arithmetic | +, -, *, /, % | a + b |
| Relational | ==, !=, >, <, >=, <= | a > b |
| Logical | &&, ||, ! | a > 0 && b > 0 |
| Assignment | =, +=, -=, *=, /=, %= | a += 5 |
| Unary | ++, --, +, -, ! | ++a |
| Bitwise | &, |, ^, ~ | a & b |
| Shift | <<, >>, >>> | a << 2 |
| Ternary | ?, : | a > b ? a : b |
instanceof | instanceof | obj instanceof String |
2. Arithmetic Operators
| Operator | Purpose | Example |
|---|---|---|
+ | Addition | 5 + 2 → 7 |
- | Subtraction | 5 - 2 → 3 |
* | Multiplication | 5 * 2 → 10 |
/ | Division | 5 / 2 → 2 |
% | Remainder | 5 % 2 → 1 |
Important: Integer division removes the decimal part.
java
1int x = 5 / 2; // 22double y = 5 / 2; // 2.03double z = 5 / 2.0; // 2.53. Logical Operators
| Operator | Meaning | Example |
|---|---|---|
&& | AND | a > 0 && b > 0 |
|| | OR | a > 0 || b > 0 |
! | NOT | !(a > 0) |
Key interview point: && and || use short-circuit evaluation.
4. Unary Operators
| Operator | Meaning | Example |
|---|---|---|
++a | Increment, then use | Pre-increment |
a++ | Use, then increment | Post-increment |
--a | Decrement, then use | Pre-decrement |
a-- | Use, then decrement | Post-decrement |
!a | Reverses boolean | Logical NOT |
java
1int a = 5;2int b = a++; // b = 5, a = 65. Ternary Operator
Short form of if-else.
java
1int max = a > b ? a : b;Next TopicArrays & Strings