Re: Differences between C++ and Java



"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

:)



.



Relevant Pages

  • Re: No call for Ada (was Re: Announcing new scripting/prototyping language)
    ... >> Add to that the presence of available class libraries and Ada comes up ... >> someone looking at what Java provides just by virtue of being Java. ... Java standard libraries do not exist due to the ...
    (comp.lang.ada)
  • Re: The future of C++
    ... Is the problem that the committee doesn't think it's important, ... Java SDK, and compare that to what you can get with a 'standard' C++ setup. ... There are many open source libraries available, and for that matter, you ...
    (comp.lang.cpp)
  • Re: The future of C++
    ... > gather it comes with a similar arsenal of libraries. ... into the language standard; there are even several very good reasons not ... > things I get as part of Java either with the JSDK or as a freely ...
    (comp.lang.cpp)
  • Re: Help with Enter and Leave Instructions
    ... >>> for Memory Accesses, ... > The only standard way to do it is via malloc. ... Uh, SBRK/BRK is a standard, documented system call. ... > I really don't understand the objection to using C libraries. ...
    (alt.lang.asm)
  • Re: Differences between C++ and Java
    ... > A link to an essay on the differences between C++ and Java in the Java ... C++, you manually manage memory. ... "Java has a vast standard library set, ... provide third party libraries that work just as well, ...
    (comp.lang.java.programmer)