🔹 1. Building Blocks of Java Program
Every Java program is made up of:
- Keywords – Reserved words with special meaning (e.g.,Â
int,Âclass,Âif,Âfor). - Identifiers – Names for variables, methods, classes, etc.
✅ Must start with a letter,Â_, orÂ$; can’t use spaces or keywords. - Literals – Constant values (likeÂ
10,Â'A',Â"Hello"). - Operators – Symbols to perform operations (likeÂ
+,Â-,Â==). - Separators –Â
(),Â{},Â[],Â;,Â,,Â. etc.
🔹 2. Data Types in Java
Java is a strongly typed language — every variable must have a declared type.
A. Primitive Data Types
| Type | Size | Example | Description |
|---|---|---|---|
| byte | 1 byte | 127 | small integers |
| short | 2 bytes | 32000 | medium integers |
| int | 4 bytes | 100000 | default integer type |
| long | 8 bytes | 10000000000L | large integers |
| float | 4 bytes | 10.5f | decimal numbers (less precision) |
| double | 8 bytes | 12.345 | decimal numbers (more precision) |
| char | 2 bytes | ‘A’ | single character |
| boolean | 1 bit | true / false | logical value |
B. Non-Primitive Data Types
- String, Arrays, Classes, Interfaces, Objects etc.
🔹 3. Variables
A variable is a name that refers to a memory location storing data.
Syntax:
int age = 18;
Types of Variables:
- Local Variable – Declared inside a method.
- Instance Variable – Declared inside a class but outside any method.
- Static Variable – Declared usingÂ
static keyword; shared by all objects.
Rules for Naming:
- Must start with letter,Â
$, orÂ_. - No spaces or keywords.
- Case-sensitive (
Age ≠Âage).
🔹 4. Constants
- Declared using theÂ
final keyword.
Example:final double PI = 3.14159; - Once assigned, their value cannot be changed.
🔹 5. Type Casting
Changing one data type into another.
Types:
- Implicit (Automatic) – Smaller → larger type
Example:int a = 10; double b = a; // int to double - Explicit (Manual) – Larger → smaller type
Example:double x = 9.8; int y = (int) x; // double to int
🔹 6. Operators in Java
A. Arithmetic Operators
+, -, *, /, %
B. Relational Operators
==, !=, >, <, >=, <=
C. Logical Operators
&&, ||, !
D. Assignment Operators
=, +=, -=, *=, /=, %=
E. Unary Operators
++ (increment), -- (decrement)
F. Conditional (Ternary) Operator
condition ? value_if_true : value_if_false
Example:
int max = (a > b) ? a : b;
G. Bitwise Operators
Operate on bits: &, |, ^, ~, <<, >>, >>>
🔹 7. Expressions and Type Promotion
- If different data types are used in an expression, Java promotes smaller types to a larger type automatically.
Example:
int a = 5;
float b = 6.2f;
float c = a + b; // a converted to float
🔹 8. Comments in Java
Used to improve readability.
// Single-line comment
/* Multi-line
comment */
