Theory
एक Book, Java के Words में
BookBridge एक book के facts store करके शुरू होता है: एक title, एक section letter, कितनी copies exist करती हैं, price, और यह अभी shelf पर है या नहीं।
हर fact को एक variable चाहिए, हर variable को एक type चाहिए, हर type का अपना literal form है, और आपके invent किए हर name को identifier rules follow करने चाहिए। यह एक lesson वह vocabulary है जिसमें पूरा subject लिखा गया है; इन words को सही करिए और हर बाद की listing आसानी से पढ़ी जाती है।
Theory
8 Primitive Types
Java exactly 8 primitive data types देता है, और C के विपरीत, इनकी sizes हर platform पर fixed हैं (यही action में "portable" property है)।
Whole numbers 4 sizes में आते हैं: byte, short, int, long। Decimals 2 में आते हैं: float और double। फिर एक single character के लिए char और true/false के लिए boolean। Choose करने का मतलब है एक question पूछना: यह value कितनी बड़ी हो सकती है, और क्या इसमें decimal point है?
At a glance
Primitive Types (Sizes Exam Favourites हैं)
| Type | Size | BookBridge में Example Use |
|---|---|---|
| byte | 1 byte | एक shelf पर copies (-128 से 127) |
| short | 2 bytes | एक section में books |
| int | 4 bytes | total books, fines rupees में (default choice) |
| long | 8 bytes | ISBN numbers (int के लिए बहुत बड़े) |
| float | 4 bytes | average rating (f suffix चाहिए) |
| double | 8 bytes | price (decimals के लिए default) |
| char | 2 bytes | section letter 'C' (Unicode) |
| boolean | JVM-dependent | available: true या false |
Theory
Identifiers और Keywords
एक identifier आपके invent किए किसी भी name है: class, variable या method names। Rules:
- letters, digits, underscore
_और dollar$contain कर सकता है - digit से start NAHI होना चाहिए
- case-sensitive:
copiesऔरCopiesअलग variables हैं - एक keyword नहीं होना चाहिए
Keywords Java के लगभग 50 reserved words हैं (class, int, if, static...)। दो oddities एक mark के लायक हैं: const और goto reserved हैं पर कभी usable नहीं।
Practical
Variables और Literals की तरह Book Facts
public class Book {
public static void main(String[] args) {
String title = "Let Us C"; // String literal
char section = 'C'; // char literal: single quotes
int copies = 5; // int literal
double price = 350.50; // decimals are double by default
float rating = 4.5f; // f suffix makes it float
long isbn = 9788183331630L; // L suffix: too big for int
boolean available = true; // boolean literal
System.out.println(title + " | copies: " + copies);
}
}
Theory
Literals: Values कैसे Spell होती हैं
एक literal एक value है जो code में directly लिखी जाती है, और इसकी spelling इसका type announce करती है:
5एक int है;9788183331630Lको L चाहिए क्योंकि यह int की range से ज़्यादा है350.50default से double है;4.5fको float होने के लिए f चाहिए'C'(single quotes) एक char है;"C"(double quotes) एक String हैtrue,false,nullभी literals हैं
Week 1 में ज़्यादातर compile errors literal-spelling errors हैं: float x = 4.5; fail होता है क्योंकि 4.5 एक double है।
Quiz
इनमें से कौन सा एक valid Java identifier है?
- 2ndBook
- book-price
- _bookCount
- class
Show the answer
_bookCount
Underscore एक legal starting character है, तो _bookCount fine है। 2ndBook no-starting-digit rule तोड़ता है। book-price में एक hyphen है, जिसे Java minus operator की तरह पढ़ता है (book minus price)। class एक keyword है, permanently reserved। Exams normal-दिखने वाले names के बीच keyword option hide करना पसंद करते हैं, तो पहले reserved words के लिए scan कीजिए।
At a glance
Operator Families
| Family | Operators | BookBridge Use |
|---|---|---|
| Arithmetic | + - * / % | fine = days * 2 |
| Relational | == != > < >= <= | copies > 0 |
| Logical | && || ! | available && copies > 0 |
| Assignment | = += -= *= /= | issue पर copies -= 1 |
| Increment/decrement | ++ -- | issuedCount++ |
| Ternary | ?: | msg = ok ? "Issued" : "Denied" |
Think first
वह Char जो बड़ा हो गया
BCA104 में, एक C char 1 byte occupy करता था। Tap करने से पहले: एक Java char कितना बड़ा है, और change क्यों?
Show the answer
एक Java char 2 bytes है, क्योंकि यह Unicode store करता है, सिर्फ़ ASCII नहीं। एक byte सिर्फ़ 256 symbols cover करता है, English के लिए काफ़ी पर world की scripts के लिए नहीं; 2 bytes एक char को Gujarati, Hindi, Tamil और आगे के characters hold करने देते हैं। यह एक classic 1-mark difference question है, और यह यह भी explain करता है Java international software में क्यों comfortable है।
Watch out
Java Purposely C से Stricter है
C में, int x = 3.5; compile होता है और silently 3 store करता है। Java में same line एक compile error है: possible lossy conversion from double to int। Java बिना एक explicit cast (अगले lessons) के आपकी .5 फेंकने से मना करता है।
दूसरा trap: = assign करता है, == compare करता है। Java में if (copies = 0) लिखना भी एक compile error है (condition एक boolean नहीं है), जो आपको C के सबसे famous silent bug से बचाता है।
Summary
Key takeaways
- 8 primitives हर जगह fixed sizes के साथ: byte 1, short 2, int 4, long 8, float 4, double 8, char 2, boolean JVM-dependent।
- char 2 bytes का है क्योंकि यह Unicode store करता है; C का char 1 byte ASCII था।
- Identifiers: letters, digits, _ और $; कभी digit से start नहीं; case-sensitive; कभी एक keyword नहीं।
- Literal spellings types declare करती हैं: long के लिए L, float के लिए f, char के लिए single quotes, String के लिए double quotes।
- Decimals default से double होते हैं; whole-number literals default से int होते हैं।
- Operators families में आते हैं: arithmetic, relational, logical, assignment, increment, ternary।
- Memory hook: 1-2-4-8 दो बार (byte-short-int-long, फिर float-double), char को world के लिए 2 मिलते हैं।