Theory
program જે travel નહોતું કરતું
BCA104 માં તમે college lab માં C program ને compile કર્યું હતું. એ ત્યાં perfectly ચાલ્યું. તમે એને તમારા friend ના laptop પર copy કર્યું અને એ start થવા નકાર્યું: compiled file એ lab machine ની language બોલતી હતી, laptop ની નહીં.
આ semester તમે BookBridge build કરો છો, નાનું library app. librarian પાસે જૂનું Windows PC છે, તમારા external examiner Linux ચલાવી શકે છે, અને તમે તમારા own laptop પર test કરો છો. દરેક machine માટે rewriting અથવા recompiling એ exactly એ pain છે જે Java remove કરવા invent થઈ હતી.
Theory
અડધું-પકેલું meal kit
C compiler એ fully cooked dish serve કરે છે: એક specific table માટે perfect, બીજે ક્યાંય useless.
Java બદલે meal kit pack કરે છે: recipe ને standard, half-finished form (bytecode) માં pre-process કરવામાં આવે છે, અને દરેક kitchen local chef (JVM) રાખે છે જે તે kitchen માટે એને finish કરે છે. Same kit, કોઈપણ kitchen. એ Java નું famous promise છે: write once, run anywhere.
Theory
promise કેવી રીતે work કરે છે
Java platform-independent છે કારણ કે compilation 2 stages માં થાય છે:
javacતમારા.javasource ને bytecode (.classfile) માં compile કરે છે, standard instruction set જે કોઈ real machine નું નથી.- દરેક computer પર JVM (Java Virtual Machine) એ bytecode ને read કરે છે અને execute કરે છે, local machine ની instructions માં translate કરીને.
એટલે એક BookBridge.class file Windows, Linux અને macOS પર ચાલે છે, જ્યાં સુધી દરેક પાસે તેનો own JVM installed હોય.
At a glance
Java ના Properties (exam list)
| Property | શું અર્થ |
|---|---|
| Simple | C-family syntax minus pointers, operator overloading અને other sharp edges |
| Object-oriented | All code classes ની અંદર lives કરે છે; objects real world ને model કરે છે |
| Platform-independent | Bytecode + JVM write once, run anywhere આપે છે |
| Portable | No machine-dependent sizes: int દરેક જગ્યાએ 4 bytes છે |
| Robust | Garbage collection, exception handling, strict compile-time checks |
| Secure | No direct memory access; JVM bytecode ને run કરતા પહેલા verify કરે છે |
| Multithreaded | એક program એક સાથે several tasks કરી શકે છે (Unit 4) |
| High-performance | JIT compiler hot bytecode ને native code માં run કરતા સમયે turn કરે છે |
Quiz
Java ને platform-independent શા માટે exactly કહેવામાં આવે છે?
- javac source ને bytecode માં compile કરે છે, અને કોઈપણ platform નું JVM એ same bytecode ને execute કરી શકે છે
- Compiler દરેક operating system માટે separate native .exe produce કરે છે
- Java programs source code માંથી directly run થાય છે કોઈ compilation વગર
- Java માં pointers નથી, એટલે programs machine પર depend નથી કરી શકતા
Show the answer
javac source ને bytecode માં compile કરે છે, અને કોઈપણ platform નું JVM એ same bytecode ને execute કરી શકે છે
આખો trick 2-stage design છે: એક universal bytecode file, per-platform JVM દ્વારા executed. Option B languages like C માં cross-compiling ને describe કરે છે, opposite approach. Option C purely interpreted language ને describe કરે છે, પણ Java IS compiled (bytecode માં). Option D Java વિશે true fact છે પણ platform independence સાથે કંઈ લેવાદેવા નથી; exams આ true-fact-wrong-reason trap ને love કરે છે.
Theory
Robust અને secure, concretely
બે properties answer માં one line કરતાં વધુ deserve કરે છે.
Robust: C માં, stray pointer silently memory ને corrupt કરી શકે છે. Java pointers ને remove કરે છે, memory ને automatically free કરે છે (garbage collection), અને તમને errors ને exceptions (Unit 3) દ્વારા face કરવા force કરે છે. Programs mysteriously ને બદલે loudly અને early fail થાય છે.
Secure: bytecode JVM એને run કરે તે પહેલાં verifier માંથી passes થાય છે, અને code raw memory addresses ને touch નથી કરી શકતું. એટલે banks અને Android apps એ દાયકાઓ સુધી Java ને trusted કર્યું.
Think first
તેને C સાથે compare કરો
BCA104 માં, marks.c ને compile કરવાથી file produce થઈ જે ફક્ત તે machine type ચલાવી શકતી હતી. tap કરતા પહેલા: BookBridge.java ને compile કરવાથી શું produce થાય છે, અને એને શું run કરે છે?
Show the answer
એ BookBridge.class produce કરે છે, bytecode file જે કોઈ real machine સાથે tied નથી. JVM એને run કરે છે: librarian ના PC પર Windows JVM, examiner ના laptop પર Linux JVM, દરેક same bytecode ને તેના own hardware માટે translate કરે છે. C નું output finished native program હતું; Java નું output universal middle step છે.
Watch out
mark-losing sentence
Students લખે છે "the JVM is platform-independent". Wrong, અને એ mark cost કરે છે.
bytecode platform-independent છે; JVM platform-specific છે (Windows, Linux, macOS માટે separate JVM build છે). એમ લખો: Java platform independence achieve કરે છે કારણ કે platform-independent bytecode platform-specific JVM પર ચાલે છે. એ one sentence examiner ને show કરે છે કે તમે machinery ને understand કરો છો.
Theory
તમે Java ને already meet કર્યું છે
BCA305-02 માં તમે Android screens build કરવા Java લખ્યું હતું, એટલે syntax new નથી. આ subject underneath જાય છે: WHY language એમ behave કરે છે, proper OOP (Unit 2), Strings અને exceptions (Unit 3), threads (Unit 4) અને hand-built linked lists (Unit 5), બધું BookBridge ની અંદર. અને BCA304 ના C++ ને mind માં રાખો: half exam comparisons Java vs C++ છે.
Summary
Key takeaways
- Java 2 stages માં compile થાય છે: javac source ને bytecode (.class) માં turn કરે છે, અને JVM bytecode ને દરેક machine પર execute કરે છે.
- Write once, run anywhere: એક bytecode file જ્યાં JVM exist કરે ત્યાં ચાલે છે.
- Bytecode platform-independent છે; JVM platform-specific છે. ક્યારેય swap ન કરો.
- Exam list of properties: simple, object-oriented, platform-independent, portable, robust, secure, multithreaded, high-performance (JIT).
- Robust = garbage collection + exceptions + strict checks; secure = bytecode verifier + no raw memory access.
- Memory hook: meal kit travels કરે છે, chef home રહે છે.