Re: Differences between C++ and Java
- From: "Luc The Perverse" <sll_noSpamlicious_z_XXX_m@xxxxxxxxxx>
- Date: Mon, 21 Nov 2005 19:04:05 -0700
"AndyRB" <xandyrb@xxxxxxxxxxx> wrote in message
news:1132596122.346797.180460@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
>
> Roedy Green wrote:
>> A link to an essay on the differences between C++ and Java in the Java
>> glossary has died, so I concocted this little essay to replace it at
>> http://mindprod.com/jgloss/cpp.html
>
> My comments:
> "In Java, the sizes of int, long etc. are rigidly defined in terms of
> bits. In C++ they are platform-dependent. "
>
> Technically this is inaccurate as the sizes of built-in types in c++
> are *implementation* defined and the standard also specifies the
> miniumum sizes of types and the size relationship between then:
> char - 8 bit, -127 to +127 when signed
> short - plus or minus 32767 (2**15)
> int - plus or minus 32767
> long - plus or minus 2147483657 (2**31)
>
> char <= short <= int <= long.
>
> "In Java, garbage collection of unreferenced objects is automatic. In
> C++, you manually manage memory. In Java you don't know for sure that
> all finalizers will be run; in C++ you do, because you invoke them
> manually. You could of course write a manual close method for Java
> objects. In Java, the choice of stack of heap allocation is handled
> automatically by the compiler (Sun's javac is not very bright always
> using the heap for objects). In C++ you control it manually. "
>
> There is more than one type of memory in c++, for example a variable
> declared as having automatic storage duration will be automatically
> managed (deallocated and destructor called). So it would be more
> accurate to specify the type of memeory, i.e. memory is manually
> managed when using dynamic memory. There are no finalizers in c++ and
> although there are destructors which you can invoke manually
> "ObjectName.~classname()", doing so would often result in undefined
> behaviour (very rarely done except in low-level code - i.e.
> implementation of std::vector).
>
> "In Java, references are constrained to point only to the beginnings of
> objects. In C++, you can do arithmetic on pointers and make pointers
> point anywhere in the address space. "
>
> I understand what you are saying here, but in c++ you can legally only
> point to an object or one past the end of a object, otherwise you have
> undefined behaviour. But obviously it is up to the programmer in this
> case to ensure they do not invoke undefined behaviour, the language
> will not enforce/check this.
>
> "Java checks all subscripts that they are in bounds and all casts for
> validity. C++ does not. "
> You are right that C++ does not check *all* subscripts and *all* casts
> but it does check some. For example, when using c++ style casting,
> rather than a traditional C cast.
>
> "Java has a vast standard library set, including AWT and Swing. C++ has
> a relativey modest standard set of methods, and relies on
> platform-specific GUI libraries."
>
> A difference you could mention the fact that the c++ standard library
> is typically more template / generic based rather than object oriented.
> The library is implemented in standard c++, therefore anyone can
> provide third party libraries that work just as well, so although it
> would be good if more functionality was standardized, it is not
> necessarily required.
> The fact that the c++ standard library is part of an ISO standard and
> must be portable across many "platforms" which have far less
> requirements than the JVM (i.e. no requirement for a display or hard
> drive), certainly makes a difference. The library and the language
> satisfy only the core requirements and has a far more limited scope
> than the Java library. Third party library play an important part in
> c++ and always will.
>
> "In C++ constructors inherit. In Java, constructors are redefined for
> every subclass."
> This is not true, constructors in c++ do not inherit.
>
> "Java has the final keyword and C++ has const. C++ gives you finer
> control, but that control can be overridden with a cast. "
> Not really. In c++ you can usually always lie to the compiler, but...
>
> const int const_val = 5;
> int & val_ref = const_cast<int&>(const_val);
> val_ref = 6; //undefined behaviour.
>
> In the above the const cast is OK, but the underlying variable
> const_val remains const (it may, for example, have been place in an
> area of memory marked as read-only). The result is any attempt to
> modify it is illegal - undefined behaviour.
>
> Other main differences:
> - c++ the language and it's standard library are defined by a ISO
> standard.
> - deterministic destruction (this is very important from a c++ point of
> vioew).
> - exception handling and specifically exception specifications
> - different philosophies / goals.
While I have no doubt that you are competant and know what you are talking
about, the criticism of the document on Roedy's site seems a little nit
picky.
--
LTP
:)
.
- Follow-Ups:
- Re: Differences between C++ and Java
- From: AndyRB
- Re: Differences between C++ and Java
- From: Roedy Green
- Re: Differences between C++ and Java
- From: Bjorn Abelli
- Re: Differences between C++ and Java
- References:
- Differences between C++ and Java
- From: Roedy Green
- Re: Differences between C++ and Java
- From: AndyRB
- Differences between C++ and Java
- Prev by Date: Re: Java and mySQL
- Next by Date: Re: Differences between C++ and Java
- Previous by thread: Re: Differences between C++ and Java
- Next by thread: Re: Differences between C++ and Java
- Index(es):
Relevant Pages
|