English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

why do v decl an object in java as
class_name obj1= new class_name();
unlike
class_name obj1;
in c++?

2007-08-08 05:51:21 · 2 answers · asked by msggeca 1 in Computers & Internet Programming & Design

2 answers

It's sort of an artifact of Java hiding pointers and doing memory management for you.

In C++ you can write:
class_name obj1;
or
class_name *pobj1 = new class_name();

In a method, as a temporary variable, the first one declares a temporary variable that's an instance of the class (temp variables are stored on the stack, so the instance of the class itself is on the stack). The other one declares a temp variable that is a pointer, to an allocated instance (from the heap). In the latter case, the only thing on the stack is the pointer to the object.

In the former case, the instance is thrown away when the method returns because the method's temporary stack space is given up. In the latter case the pointer is given up but the instance it points to must be explicitly deleted by the coder.

----------------------------
Java, on the other hand, deals with all non-primitive types by reference BUT hides the pointers. A declaration of a temporary variable:

class_name foo;

Effectively creates a pointer on the stack, but leaves it pointing at nothing. There's not a separate syntax for declaring a pointer, because pointers are hidden from the coder. If Java were to create an instance automatically for this syntax, it would lead to a lot of waste (since one doesn't always want to create the instance locally).

The C++ syntax for object instantiation on the heap ("new class_name") is pretty much identical to Java's.

In Java, all objects are on the heap, and are cleaned up by the Java VM's garbage collection -- transparently to the coder, when they are no longer referenced -- so the coder does not need to make the distinction between temporary and permanent objects.

This makes Java easier to code in, and less prone to issues like memory leaks, and entirely free of segmentation faults and the like... but there is a cost for letting the system do memory management for you, so Java is much less efficient when executing.

2007-08-08 06:39:42 · answer #1 · answered by McFate 7 · 0 0

Java is not Purely Object Oriented Programming Language. Bcoz of the Primitive types. In Java all the process will be Object or Data oriented, except primitive data types. That is the reason Java is not purely Object Oriented Programming language. Next comes to the C++ it is partially Object Oriented and Structual Oriented Programming Language. 100% pure Object Oriented Programming Language is Small Talk. Final question answer is, C++ Java Platform Dependednt Platform Independent. Pointer consists It does not have Pointers 40% Oop's Language 98% Oop's language Don't support for Web ^ 100 % support for Web Applications applications Everything will access as Everything will access as Objects different ways

2016-05-17 06:11:46 · answer #2 · answered by ? 3 · 0 0

fedest.com, questions and answers