Theory
Semester 3 નો transfer student
છેલ્લા semester માં તમે CanteenTill ને C++ માં build કર્યું: classes, constructors, delete, એક કે બે friend functions. હવે Java file ખોલો અને 90% familiar લાગે છે: braces, semicolons, if, for, even class.
exam બાકીના 10% માં lives કરે છે. "Compare Java with C++" એ એક of the most repeated BCA403 questions છે, અને good news એ છે કે almost every difference Java ના એક design decision ને follow કરે છે.
Theory
locked drawer
C++ એ workshop છે જ્યાં દરેક tool open bench પર છે, including જે તમને cut કરી શકે છે: raw pointers, manual delete, operator overloading.
Java એ same workshop છે safety audit પછી: dangerous drawer locked છે. તમે કેટલુંક low-level control ગુમાવો છો; બદલામાં, crashes ના whole categories (dangling pointers, memory leaks, double frees) simply happen નથી કરી શકતા. comparison table ની દરેક row એ એક જ trade છે: control for safety.
At a glance
Java vs C++ (આ table learn કરો, એ જ answer છે)
| Feature | C++ | Java |
|---|---|---|
| Compilation | Native machine code માં | bytecode માં, JVM દ્વારા run થાય છે |
| Pointers | હા, full pointer arithmetic | કોઈ pointers નહીં; ફક્ત references |
| Memory freeing | Manual, delete સાથે (destructors) | Automatic garbage collection |
| Multiple inheritance | classes માટે allowed | classes માટે નહીં; interfaces દ્વારા |
| Operator overloading | હા | નહીં (ફક્ત Strings માટે built-in +) |
| Preprocessor | #include, #define | કોઈ નહીં; import વાપરે છે |
| Global variables/functions | Allowed | નહીં; everything class ની અંદર |
| goto | Available | Reserved word, ક્યારેય usable નહીં |
Theory
શા માટે Java એ sharp tools ને drop કર્યા
Pointers C++ code ને કોઈપણ memory address ને read અથવા write કરવા દે છે; એક wrong address data ને corrupt કરે છે અથવા program ને crash કરે છે. Java ના references objects તરફ point કરે છે પણ એને add, subtract અથવા raw memory તરફ aim નથી કરી શકાતા.
Destructors અને delete એ programmer ને memory free કરવાની જવાબદારી આપી; once forget કરો અને leak થાય. Java નો garbage collector એવા objects ને watch કરે છે જેના તરફ કોઈ refer નથી કરતું અને એને automatically reclaim કરે છે. Safer, exactly ક્યારે memory returns થાય છે એના પર some control ની cost પર.
Quiz
આમાંથી કયું feature Java માં ઉપલબ્ધ નથી?
- Multiple inheritance of classes
- Garbage collection
- Interfaces
- Single inheritance between classes
Show the answer
Multiple inheritance of classes
Java class ને 2 parent classes extend કરવાની મનાઈ કરે છે (C++ diamond problem એ એક motivation હતું). Garbage collection એ Java ના headline features માંનું એક છે, interfaces એ Java નો multiple inheritance માટે replacement route છે (Unit 2), અને extends સાથે single inheritance everyday Java છે. જો such question માં option interfaces અથવા garbage collection ને missing તરીકે mention કરે, તો એ trap છે.
Think first
delete ક્યાં ગયું?
CanteenTill માં તમે Bill object ને use પછી delete સાથે free કર્યું. tap કરતા પહેલા: Java માં equivalent step શું છે?
Show the answer
કોઈ equivalent step નથી, અને એ જ answer છે જે examiner wants છે. જ્યારે કોઈ reference object તરફ point નથી કરતું, ત્યારે garbage collector એને automatically reclaim કરે છે; તમે ક્યારેય delete call નથી કરતા અને Java માં કોઈ destructors નથી. at most તમે reference ને drop કરી શકો છો (bill = null) જેથી object sooner eligible થાય. Manual memory freeing એ exactly C++ responsibility છે જેને Java remove કર્યું.
Watch out
આ comparison માં બે traps
Trap 1: લખવું "Java multiple inheritance કરી શકતું નથી". Incomplete. કહો: classes સાથે નહીં, પણ class multiple interfaces implement કરી શકે છે, જે Java નો same idea નો route છે.
Trap 2: લખવું "Java માં pointers નથી, એટલે objects everywhere copy થાય છે". Java variables of class types objects ના references hold કરે છે. Java શું remove કરે છે એ pointer arithmetic અને raw memory access છે, object ને refer કરવાનો concept નહીં.
Theory
આ thread ને subject માં carry કરો
આખા semester દરમિયાન એક હાથમાં CanteenTill અને બીજામાં BookBridge રાખો. Constructors (Unit 2) બંને languages માં alike behave કરે છે; this C++ ના this pointer syntax ને replace કરે છે; extends colon ને replace કરે છે; interfaces (Unit 2) multiple inheritance નો answer આપે છે; garbage collection delete ને replace કરે છે. દરેક વખતે જ્યારે Java feature odd લાગે, પૂછો: આ C++ ના કયા problem ને fix કરવા design થયું હતું? answer usually તમારા exam paragraph ને તમારા માટે write કરે છે.
Summary
Key takeaways
- Java અને C++ C-family syntax અને OOP share કરે છે; differences deliberate safety choices છે.
- Java માં કોઈ pointers નથી, કોઈ destructors/delete નથી, કોઈ operator overloading નથી, કોઈ preprocessor નથી, કોઈ globals નથી.
- Memory ને programmer દ્વારા નહીં પણ garbage collector દ્વારા reclaim કરવામાં આવે છે.
- classes ની multiple inheritance forbidden છે; interfaces Java route પૂરો પાડે છે.
- C++ native machine code માં compile થાય છે; Java JVM માટે bytecode માં compile થાય છે.
- goto અને const Java માં reserved words તરીકે exist કરે છે પણ ક્યારેય use નથી થઈ શકતા.
- Memory hook: Java એ C++ છે dangerous drawer locked સાથે.