Introduction
Java ek statically-typed aur strongly-typed programming language hai, jisme har variable ke liye uska data type fix hota hai. Programming karte waqt sahi data types ka chunav aur unka sahi istemal bahut zaruri hai.
Primitive Data Types
Java me total 8 primitive data types hote hain. Yeh simple values store karte hain.
Data Type | Description | Size (in bytes) | Default Value | Example |
---|---|---|---|---|
byte | Integer value -128 to 127 | 1 | 0 | byte b = 100; |
short | Integer value -32,768 to 32,767 | 2 | 0 | short s = 10000; |
int | Integer value -2,147,483,648 to 2,147,483,647 | 4 | 0 | int i = 50000; |
long | Integer value -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | 8 | 0L | long l = 15000000000L; |
float | Decimal value upto 6-7 decimal digits | 4 | 0.0f | float f = 5.75f; |
double | Decimal value upto 15 decimal digits | 8 | 0.0d | double d = 19.99d; |
char | Single 16-bit Unicode character | 2 | '\u0000' | char c = 'A'; |
boolean | True or False | 1 (size not precisely defined) | false | boolean b = true; |
Variables in Java
Variables memory locations hoti hain jisme program ke dauran data store kiya jata hai. Java me variables declare karne ke liye data type ke sath naam specify karte hain.
Variable Types:
- Local Variables: Methods ke andar declare hote hain, sirf method ke andar accessible.
- Instance Variables: Class ke object-specific variables hote hain.
- Static Variables: Class ka shared variable jo sabhi objects ke liye common hota hai.
Example of variable declaration:
int age = 25;
float salary = 55000.50f;
char grade = 'A';
boolean isActive = true;
String name = "RKTechGame";
Constants in Java
Constant variables wo hote hain jinki value ek baar assign hone ke baad change nahi ho sakti. Java me inhe declare karne ke liye final
keyword ka use hota hai.
Example of constant declaration:
final double PI = 3.14159;
final int MAX_USERS = 100;
Important Notes:
- Constants ka naam usually uppercase letters me likhte hain taaki easily identify kiya ja sake.
- Constants ko initialize karna zaruri hota hai, varna compile-time error aayega.