📦
Basics

Variables और Data Types

Storing Data
💡 Variable एक labeled डिब्बा (box) है जिसमें तुम एक value रखते हो — डिब्बे का नाम और उसमें क्या रख सकते हो (type), पहले से तय होता है।

Java में हर variable का एक type होता है — जैसे int (whole number), double (decimal number), char (single letter), boolean (true/false), और String (text)।

Java "statically typed" language है — मतलब डिब्बे का type शुरू में ही बताना पड़ता है, और उस डिब्बे में सिर्फ़ वही type की चीज़ fit होगी।

int age = 10;
double price = 99.5;
char grade = 'A';
boolean isFun = true;
String name = "Duke";
📦
Variable एक labeled डिब्बा (box) है जिसमें तुम एक value रखते हो — डिब्बे का नाम और उसमें क्या रख सकते हो (type), पहले से तय होता है।
1 / 7
⚡ झट से Recap
  • Variable = data के लिए एक labeled box
  • Types: int, double, char, boolean, String
  • Java statically typed है
इस page में (3 subtopics)

Java में 8 primitive types हैं, और इन्हें दो groups में बाँट सकते हैं: numbers (byte, short, int, long, float, double), text (char), और logic (boolean)। हर type की अपनी fixed size (bits/bytes) होती है, जो decide करती है कितना बड़ा number store हो सकता है।

Whole numbers के लिए 4 types हैं — छोटे से बड़े तक byte, short, int, long। ज़्यादातर programs में int ही default choice होती है (बहुत बड़े numbers के लिए long)। Decimal numbers के लिए float और double हैं — double ज़्यादा precise है और Java में decimal literals (जैसे 3.14) by default double ही माने जाते हैं, इसीलिए float use करने के लिए 'f' suffix ज़रूरी है।

TypeSizeRangeDefaultExample
byte1 byte-128 से 1270byte b = 100;
short2 bytes-32,768 से 32,7670short s = 2000;
int4 bytes~-2.1B से 2.1B0int i = 50000;
long8 bytesबहुत बड़ा0Llong l = 100L;
float4 bytes~7 decimal digits precision0.0ffloat f = 3.14f;
double8 bytes~15 decimal digits precision0.0double d = 3.14;
char2 bytes0 से 65,535 (Unicode)'\u0000'char c = 'A';
boolean1 bit (JVM-dependent)true / falsefalseboolean b = true;
💡Tip: जब तक कोई specific reason ना हो, whole numbers के लिए int और decimals के लिए double use करो — ये Java के "default" choices हैं और ज़्यादातर situations में best fit होते हैं।

Local variable एक method/block के अंदर define होता है — सिर्फ़ वहीं तक "ज़िंदा" रहता है (जब method ख़त्म, variable भी गया)। इसे use करने से पहले initialize करना ज़रूरी है, वरना compile error आएगा — Java local variables को कोई default value नहीं देता (जान-बूझकर, ताकि ग़लती से uninitialized data use ना हो)।

Instance variable class के अंदर, method के बाहर define होता है — हर object की अपनी अलग copy होती है। ये "heap" memory में object के साथ store होती है, और automatically default value मिलती है (0, null, false) अगर explicitly initialize ना करो।

Static variable class के साथ जुड़ा होता है, किसी individual object से नहीं — सब objects इसमें एक ही copy share करते हैं। ये program शुरू होते ही (class load होते ही) बनती है और पूरे program के चलते रहती है।

class Counter {
  int id;              // instance variable — har object ki apni
  static int total;    // static variable — sabme shared
  void increment() {
    int temp = 1;       // local variable — sirf is method mein
    total += temp;
  }
}
⚠️Common Mistake: Local variable को बिना initialize किए use करने की कोशिश मत करो — "variable might not have been initialized" compile error मिलेगा। ये Java की safety feature है, bug नहीं!

Java में camelCase use होता है variables/methods के लिए (studentName, calculateTotal), और PascalCase class names के लिए (StudentRecord, BankAccount)। Constants (static final variables) SCREAMING_SNAKE_CASE में लिखे जाते हैं (MAX_SPEED, PI_VALUE) — ये conventions Java Language Specification में official नहीं हैं, लेकिन पूरी industry follow करती है, इसलिए इन्हें तोड़ना code को "unprofessional" बना देता है।

Numeric literals में Java 7+ से underscore use कर सकते हो readability के लिए — 1_000_000 पढ़ना 1000000 से कहीं आसान है। Hexadecimal 0x से शुरू होता है (0x1A = 26), octal सिर्फ़ 0 से (010 = 8 decimal में), और binary 0b से (0b1010 = 10)।

Variable names letters, digits, underscore (_), और dollar sign ($) से बन सकते हैं, लेकिन digit से शुरू नहीं हो सकते, और Java के reserved keywords (class, int, if, etc.) use नहीं कर सकते।

int million = 1_000_000;
int hex = 0x1A;      // 26
int binary = 0b1010; // 10
final double MAX_SPEED = 120.0;