Differences between C++ and Java

C

cyberdude

Hi,

Both C++ and Java are object oriented. What are the major differences
between them then? Thank you in advance.

Mic
 
R

Roedy Green

Both C++ and Java are object oriented. What are the major differences
between them then? Thank you in advance.

Java does automatic garbage collection. In C++ you do memory
allocation and deallocation manually.

C++ allows low-level addressing of data. You can manipulate machine
addresses to look at anything you want. Java access is controlled.

C++ has no safety net. Java automatically checks subscripts on every
use, and checks every cast to be sure it is legit.

C++ allows operator overloading. e.g. >> can mean shift or i/o. Java
has only one overloaded operator + to mean addition or concatenation.
In Java there is no way to add your own overloaded operators.

C++ has several addressing operators . * & -> where Java has only one:
the .

C++ types are defined in a platform-dependent way. Java types are
defined in a platform-independent way; they have the exact same
meaning on all platforms.

C++ produces little-endian i/o on a little-endian machine and
big-endian i/o on a big-endian machine. Java always produces
big-endian.

If you are planning to learn both languages, Java is the simpler to
learn. It is also more forgiving and gives you better error messages.
So it is probably the one you should learn first. You need C++ to
write platform-dependent C++ code.
 
J

Jonathan Mcdougall

Both C++ and Java are object oriented. What are the major differences
between them then? Thank you in advance.

The same differences than between a hammer and a screwdriver; they are
used for different jobs.

Google is your friend.

And since this kind of question has the bad habit of being a troll,
please, people, ignore it.


Jonathan
 
J

Jonathan Mcdougall

Both C++ and Java are object oriented. What are the major differences
Java does automatic garbage collection. In C++ you do memory
allocation and deallocation manually.

No, in C++ memory management is done automatically when using the
stack, which is the usual way.

int main()
{
MyClass o;

o.f();

// o is destroyed automatically
}

If you want to use dynamic memory allocation (typically called "on the
heap"), then yes, you must manage it yourself.
C++ types are defined in a platform-dependent way.

Please try to be more precise in your statements.

The relation between C++ types are defined by the standard, but the
exact size of the types is not.
Java types are
defined in a platform-independent way; they have the exact same
meaning on all platforms.

As long as you consider the jvm not being a platform.
If you are planning to learn both languages, Java is the simpler to
learn.

This is a completly personal opinion.
It is also more forgiving and gives you better error messages.
So it is probably the one you should learn first.

They are two different languages and learning one makes it more
difficult to grasp things in the other. There is no preferred order
of learning a language.
You need C++ to
write platform-dependent C++ code.

What do you mean?


Jonathan
 
R

Roedy Green

No, in C++ memory management is done automatically when using the
stack, which is the usual way.

There is nothing to "manage" when you allocate on the stack.

Memory management refers to how you deal with the heap containing
objects.

Pascal, C++ and Java all handle local variable the same way. C++ is
unusual in that you can allocate objects on the stack, not just
references to them.
 
R

Roedy Green

Please try to be more precise in your statements.

If you want to know what int means you have to consult the docs for
the particular platform implementation.
 
R

Roedy Green

As long as you consider the jvm not being a platform.

What a smartass!

Java fixes the sizes. C++ does not. This is a pain in the butt for C++
programmers but cheerleaders like you won't admit the ruddy language
has any warts. You do your mad typedef preprocessor logic behind
closed doors at midnight and excuse yourself because it isn't really
part of C++.
 
R

Roedy Green

This is a completly personal opinion.

Don't be silly. By any objective measure Java is a simpler language.
automatic vs manual allocation.
number of addressing operators
number of language features
amount of built in code you don't have to write yourself.
 
R

Roedy Green

They are two different languages and learning one makes it more
difficult to grasp things in the other. There is no preferred order
of learning a language.

Schools teach the simplest things first. I see no reason why computer
languages should be an exception.

Java enforces more structure that C++ does. After all, every C
spaghetti program is also a legal C++ program. It is good for someone
starting out to get nudged into OO solutions. Then when they flip to
C++ they won't be tempted to revert to C-style coding. They will use
the low level features of C++ only when they are truly needed.

Read Stroustrup's book The Design and Evolution of C++
http://www.amazon.com/exec/obidos/ASIN/0201543303/canadianmindprod

We have different design constraints now. Safety is becoming more
important. Speed and RAM consumption are less important. Clarity is
becoming more important. The ability to co-ordinate teams on large
projects in becoming more important. These are trends. Presumably
new languages will emerge to better fit the evolutionary pressures.

The design constraints of Java are a better fit to day that the design
constraints for C++ which was developed two decades ago. A lot has
changed.

C++ had the albatross of C compatibility which was key to its success,
but which also held it back from being a cleaner language.

Java voluntarily copied some of C's mistakes, perhaps so they would be
familiar teddy bears to new programmers.
 
W

Wayne Berke

Roedy said:
Don't be silly. By any objective measure Java is a simpler language.
automatic vs manual allocation.
number of addressing operators
number of language features
amount of built in code you don't have to write yourself.

Don't forget:

single inheritance vs. multiple inheritance (of implementation)

Wayne
 
J

Josh Joyce

Java enforces more structure that C++ does. After all, every C
spaghetti program is also a legal C++ program. It is good for someone

Just to be clear, C and C++ are two different languages. The set of
all valid C programs is not a subset of the set of all valid C++
programs. Here is an example of a C program that is not a C++ program:

int main(void)
{
int class = 0;
return class;
}
 
R

Roedy Green

ust to be clear, C and C++ are two different languages. The set of
all valid C programs is not a subset of the set of all valid C++
programs. Here is an example of a C program that is not a C++ program:

int main(void)
{
int class = 0;
return class;
}

C++ has a few new reserved words. Is there anything other than that
disqualifying a C program as a valid C++ program?
 
S

Sudsy

Roedy Green wrote:
C++ has a few new reserved words. Is there anything other than that
disqualifying a C program as a valid C++ program?

One of the first things that bites you is that every function in
C++ HAS to have a prototype. No more assuming that an undeclared
function returns an int...
 
J

Jonathan Mcdougall

Is that a new netiquette to answer a message in *6* posts? Please try
to do that in one post, easier for everybody.
There is nothing to "manage" when you allocate on the stack.

Of course there is, but the C++ runtime system takes care of it.
Memory management refers to how you deal with the heap containing
objects.

That is the meaning you want to give to it. I take `memory
management` to mean.. well.. memory management.
Pascal, C++ and Java all handle local variable the same way.

For Pascal, I don't know. For Java and C++, I agree, as long as you
consider references to be 'variables'.
C++ is
unusual in that you can allocate objects on the stack, not just
references to them.
Unusual??


If you want to know what int means you have to consult the docs for
the particular platform implementation.

Yes, but you said

C++ types are defined in a platform-dependent way.

C++ types are *not* defined at all, except for the relations between
them and that a char is 1 byte, but I agree we are playing with words.
I think we understand each other here.
What a smartass!

Please, try to stay as polite as possible.
Java fixes the sizes. C++ does not. This is a pain in the butt for C++
programmers

I would not say a 'pain in the butt', but I admit there are
circumstances where it can be challenging. OTOH, it makes possible
for implementations to be more efficient than others.
but cheerleaders like you won't admit the ruddy language
has any warts.

Again, please try to stay as polite as possible. C++ has its "dusty
corners" like every language, but we do not go there.
You do your mad typedef preprocessor logic behind
closed doors at midnight and excuse yourself because it isn't really
part of C++.

Please, explain what you mean, maybe it is my english.
Don't be silly.

It never was my intention.
By any objective measure Java is a simpler language.
automatic vs manual allocation.

Dynamic allocation in C++ is used only in given cases, which are not
to be encoutered by beginners.
number of addressing operators

The language is different, the features are differents.

Since out-of-class functions are legal, there must be a way to
differentiate between pointers to member function and pointers to
'ordinary' functions.

Since pointer arithmetic is legal, there must be a way to dereference
pointers to access the actual value.

Since objects, references and pointers are all represented in the
language, there must be a way to declare and use them.

Java has none of these features and, of course, it is normal that it
lacks the operators to use them.
number of language features

Are we talking about *mastering* or *learning* a language? I agree
C++ has a lot more features and that its syntax is less easy to grasp
on the first look, which makes it a harder programming language to
master, as assembly is harder to master than C++.

But to learn C++ and Java for a beginner is the very same thing.
amount of built in code you don't have to write yourself.

I think you should try to get a bit more informed on C++ libraries
available today. We'll talk about that later if you want.
Schools teach the simplest things first.

Depends on the schools.. :)
I see no reason why computer
languages should be an exception.
Java enforces more structure that C++ does.

Can you explain your point?
After all, every C
spaghetti program is also a legal C++ program.

Whoaa, who's talking about C here? C and C++ are really two different
beasts. It is possible to compile a program written in C using a C++
compiler, but there are many well-known exceptions. Please read more
on that before making statements like that.

And who said C was a spaghetti language? A well-written C program is
as good as a C++ program.
It is good for someone
starting out to get nudged into OO solutions. Then when they flip to
C++ they won't be tempted to revert to C-style coding. They will use
the low level features of C++ only when they are truly needed.

So you are saying it is hard for a beginner to program in an
object-oriented way using C++? I am sorry, but I do not agree at all.
Both languages have their quirks or associated styles and both are
good.

A beginner will not be "tempted to rever to C-style coding" since he
never did some and even if he did, unlearning it is not difficult as
long as you understand the benefits of oop.
Read Stroustrup's book The Design and Evolution of C++
http://www.amazon.com/exec/obidos/ASIN/0201543303/canadianmindprod

Thanks for the recommendation.
We have different design constraints now. Safety is becoming more
important. Speed and RAM consumption are less important.

This is blatant nonsense. Speed is the ultimate goal in programming,
wether it is for games, banking, internet or whatever. RAM
consumption is still a constraint, associated with i/o.

I agree safety has become more important since computers are being
used in every domain. Bugs can make a *lot* of problems, we have all
seen examples of that.

But I think a good C++ programmer and a good Java programmer making
the same program will result in two as safe programs.
Clarity is
becoming more important. The ability to co-ordinate teams on large
projects in becoming more important.

These are empty statements. Explain them.
These are trends. Presumably
new languages will emerge to better fit the evolutionary pressures.

I agree.
The design constraints of Java are a better fit to day that the design
constraints for C++ which was developed two decades ago. A lot has
changed.

C++ was not developed two decades ago, it has been developping for 15
years, until the 98 standard. Since then, a lot of people have been
working on a better C++.
C++ had the albatross of C compatibility which was key to its success,
but which also held it back from being a cleaner language.

What is a clean language is quite a matter of opinion and programming
style.
Java voluntarily copied some of C's mistakes, perhaps so they would be
familiar teddy bears to new programmers.

:) I take that as a joke


Jonathan
 
J

Joona I Palaste

C++ has a few new reserved words. Is there anything other than that
disqualifying a C program as a valid C++ program?

Yes.

int main(void) {
void *v = (void *)0;
char *c = v;
return 0;
}

int main(void) {
static int i=0;
if (i) {
return 1;
}
else {
i=1;
return main();
}
}

struct foo {
int bar;
}
typedef int foo;
int main(void) {
return 0;
}

The above are the three most common examples.
Also, because character constants are ints in C, but chars in C++,
code including constructs like "sizeof 'a'" *may* work differently in
C and C++.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Holy Banana of this, Sacred Coconut of that, Magic Axolotl of the other."
- Guardian in "Jinxter"
 
W

Wojtek

No, in C++ memory management is done automatically when using the
stack, which is the usual way.

int main()
{
MyClass o;
o.f();
// o is destroyed automatically
}
If you want to use dynamic memory allocation (typically called "on the
heap"), then yes, you must manage it yourself.

But what use is this? Is "f()" a static method? If so, then why have
the line "MyClass o" at all?

To actually get an object MyClass, do you not need to "o = new
MyClass()"? And then, if you do not manage the "delete o", does not
the object become unreferenced and therefore a memory leak? And now
you must manage it yourself (as per your last sentence).

In Java, simply because there are no references to an object, that
object is a candidate for memory recovery (also known as "garbage
collection").

-------------------------
public void someMethod()
{
MyClass o = new MyClass();
o.f();
}
-------------------------

This snippet will NOT cause a memory leak. When the reference "o" goes
away, then the Java memory manager sees that there are no references
to the object. That object is then garbage collected.

In C++ you will have an unreferenced part of memory remaining on the
heap. Which makes memory management in Java more "automatic" than C++.

I worked beside people who were working on a rather large C++ project.
This project had memory leaks. It was actually easier to occasionally
bounce the application to recover lost memory than to locate and find
all the memory leaks.

And if you are thinking that the original authors of the project were
lazy and stupid, then maybe you are right. But EVERYONE makes mistakes
in programming. And memory leaks usually only show up in production.
And in million line applications are really hard to track down.
 
G

Gerbrand van Dieijen

Is that a new netiquette to answer a message in *6* posts? Please try
to do that in one post, easier for everybody.


Of course there is, but the C++ runtime system takes care of it.

Do you know how a stack works!??
The whole point of a stack is, there's nothing to manage (with regard to
memory), the program 'just' runs and puts variables on the stack or
retrieves them from it.
What is a clean language is quite a matter of opinion and programming
style.

You could also say whether my room is clean, is also a matter of opinion.

-- Gerbrand van Dieijen

WWW: http://twisted.warande.net
ICQ: 19345450
 
?

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

Wojtek said:
But what use is this? Is "f()" a static method? If so, then why have
the line "MyClass o" at all?

Think of it as a java primitive, but as an object. You don't use new
when using primitives in java. You wouldn't call java primitives useless?
To actually get an object MyClass, do you not need to "o = new
MyClass()"? And then, if you do not manage the "delete o", does not
the object become unreferenced and therefore a memory leak?

No. Myclass o creates an instance of MyClass on the stack. Once o goes
out of scope, the destructor is automatically called. In C++ MyClass o =
new MyClass() is not even legal syntax. Instead, you must use a pointer,
MyClass* o = new MyClass(). This will allocate o on the heap. It will
not get automatically destroyed, it must explicitly be deleted.

Stack based objects tend to be confusing if you are used to the Java
model where objects are always allocated on the heap and managed by
references (similar to pointers.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top