Theory
એક book, Java ના શબ્દોમાં
BookBridge એક book વિશે facts store કરીને start કરે છે: title, section letter, કેટલી copies exist છે, price, અને શું એ હાલમાં shelf પર છે.
દરેક fact ને variable જોઈએ છે, દરેક variable ને type જોઈએ છે, દરેક type ની પોતાની literal form છે, અને તમે જે પણ name invent કરો એ identifier rules ને follow કરવું જોઈએ. આ એક lesson એ vocabulary છે જેમાં આખો subject લખાયેલો છે; આ words ને right મેળવો અને દરેક later listing easily reads થાય છે.
Theory
8 primitive types
Java exactly 8 primitive data types આપે છે, અને C થી અલગ, એના sizes દરેક platform પર fixed છે (એ "portable" property action માં છે).
Whole numbers 4 sizes માં આવે છે: byte, short, int, long. Decimals 2 માં આવે છે: float અને double. પછી single character માટે char અને true/false માટે boolean. Choosing એટલે એક 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, rupees માં fines (default choice) |
| long | 8 bytes | ISBN numbers (int માટે too big) |
| 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 એ કોઈપણ name છે જે તમે invent કરો: class, variable અથવા method names. Rules:
- letters, digits, underscore
_અને dollar$contain કરી શકે છે - digit થી start થવું જોઈએ નહીં
- case-sensitive:
copiesઅનેCopiesdifferent variables છે - keyword ન હોવું જોઈએ
Keywords એ Java ના roughly 50 reserved words છે (class, int, if, static...). બે oddities જે mark worth છે: const અને goto reserved છે પણ ક્યારેય usable નથી.
Practical
Book facts as variables અને literals
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 default double છે
float rating = 4.5f; // f suffix float બનાવે છે
long isbn = 9788183331630L; // L suffix: int માટે too big
boolean available = true; // boolean literal
System.out.println(title + " | copies: " + copies);
}
}
Theory
Literals: values કેવી રીતે spelt થાય છે
Literal એ value છે જે code માં directly લખવામાં આવે છે, અને તેની spelling એનો type announce કરે છે:
5એ int છે;9788183331630Lને L જોઈએ કારણ કે એ int ની range exceed કરે છે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 ને break કરે છે. book-price માં hyphen છે, જેને Java minus operator તરીકે read કરે છે (book minus price). class એ keyword છે, permanently reserved. Exams normal-looking names ની વચ્ચે keyword option ને hide કરવાનું like કરે છે, એટલે પહેલા 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 જે grew થયું
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 માટે enough પણ world ની scripts માટે નહીં; 2 bytes char ને Gujarati, Hindi, Tamil અને beyond ના characters hold કરવા દે છે. આ classic 1-mark difference question છે, અને એ પણ explain કરે છે કે શા માટે Java international software માં comfortable છે.
Watch out
Java C કરતાં stricter છે, on purpose
C માં, int x = 3.5; compile થાય છે અને silently 3 store કરે છે. Java માં same line compile error છે: possible lossy conversion from double to int. Java તમારા .5 ને explicit cast (next lessons) વગર throw away કરવા refuse કરે છે.
બીજો trap: = assign કરે છે, == compare કરે છે. Java માં if (copies = 0) લખવું પણ compile error છે (condition boolean નથી), જે તમને C ના most famous silent bug થી save કરે છે.
Summary
Key takeaways
- 8 primitives everywhere 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 twice (byte-short-int-long, પછી float-double), char world માટે 2 મેળવે છે.