Comparison of Java with C++

Java C++ નું OOP heart રાખે છે પણ pointers, destructors, multiple inheritance અને operator overloading ને remove કરે છે, raw control ને safety માટે trade કરે છે: exam ની favourite comparison table.

9 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


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 છે)

FeatureC++Java
CompilationNative machine code માંbytecode માં, JVM દ્વારા run થાય છે
Pointersહા, full pointer arithmeticકોઈ pointers નહીં; ફક્ત references
Memory freeingManual, delete સાથે (destructors)Automatic garbage collection
Multiple inheritanceclasses માટે allowedclasses માટે નહીં; interfaces દ્વારા
Operator overloadingહાનહીં (ફક્ત Strings માટે built-in +)
Preprocessor#include, #defineકોઈ નહીં; import વાપરે છે
Global variables/functionsAllowedનહીં; everything class ની અંદર
gotoAvailableReserved 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 માં ઉપલબ્ધ નથી?

  1. Multiple inheritance of classes
  2. Garbage collection
  3. Interfaces
  4. 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 સાથે.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Introduction to Java

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Comparison of Java with C++ · Java Programming Language · Gri-Learn