c++ and java differences

Pages: 12
i've just learn the basics of c++ and figured out some differences of c++ and java. i'd like to post this for others to have a clue when learning a new language, either java to c++ or c++ to java. i personally learn java first and only know its basics so be free to add more on this article or to correct things.

namespace
c++ uses namespace to group related functions and classes and to avoid variable name conflict.
1
2
3
namespace name {
	//function and classes here
}

java uses package to group related classes since everything in java is inside a class. in this example its like a namespace inside a namespace
1
2
3
package string.manip.*; //namespace manip inside namespace string
class className {
}



include
c++ uses header files to include codes already written before and to be able to use this snippet later.
#include "mystringmanip.h"
though java uses package as namespace and also use like acts like header file
import java.io.*;


arguments to main
in c++ argc is the number of arguments and argv is the string arguments on an array
int main(int argc, char* argv[])
in java args is automatically parse as an array object
public static void main(String args[])


desctructor
in c++ ~classname()
in java void finalize()


passing arguments to function
c++ passes args by value but still can pass by reference with the operator &.
java automatically pass primitives(int, char, boolean) by value and by reference for objects


pointers
pointers are use in c++ to access data in memory and dynamic memory. in java i think there is no such thing though i'm not sure.


delete[] / garbage collection
in c++ its always the job of the programmer to release allocated memory. in java, garbage collection is automatically done when data in memory has already no way to access it. though you can force a garbage collection or assigning null to an object.


terminologies
in c++ foo() is called function
in java its called method

field - in java a field is a member of a class that describes the object

in c++ foo<int>() is called template in java its called generics



oh i'll continue this later, i'll have my lunch first.
closed account (S6k9GNh0)
C++ can have methods and functions while Java can only have methods. The reason being is that Java requires all of it's functions inside of a class making it non-independent. Therefore, making it a method. Where C++ on the other hand has the ability of having independent functions outside of a class. Although it has the option of making it non-dependant since it has the ability of being inside of a class. Same thing goes with C and structures.

A procedure is simply another (and kinda old school version of) the name function. Now adays, it's called function but when Pascal and BASIC was more popular, it was commonly known as procedure. Damn you computer science class that teaches 80's style programming languages!
Last edited on
thanks. can you please contribute more and arrange it like i did. i'll write more later.
Java is significantly more OOP-oriented than C++. C++ has several facets of OOP but it can also implement functional programming. That's the main paradigm difference between Java and C++ as I see it. The rest is just minor syntax differences.
closed account (S6k9GNh0)
Also, Java doesn't have a comparable to "namespace". Some believe that the namespace is useless and choose not to have it inside of their language. C++ can easily do without it but they choose to keep it in for more function in the language.
I don't think this is a sensible comparison myself. C++ code is meant to be compiled into native instructions that run on the system it's compiled for. C++ gets it's portability from not relying on a runtime; once it's compiled it will always run on the machine it was compiled for. Java code is meant to be 'compiled' into bytecode to be run on an interpreter and gets its portability by relying on the interpreter being ported to other architectures.

Java may have several similarities to C++, but IMO they're completely different languages; and I don't think they should be compared like this. Python and Perl is justified: both are scripting languages, both are interpreted languages and both begin with P. Python and Ruby is justified. C++ and Java is like comparing HTML and PHP. One is client-side and interpreted by the web browser; the other is server-side and parsed by the PHP preprocessor before the server sends it to you.
closed account (S6k9GNh0)
Yeah, in my honest opinion, they are completely different and I think it's stupid to compare. They each have their own place in the coding world. But syntactically and in style, they are rather similar. The both do perform instructions given. If that is enough to compare something, it might as well be compared. IMHO though, Java uses a virtual machine which then sends instruction to the native computer to execute. This (in my eyes) is like saying, "Hey, I see a road to the finish line!" and then someone else saying, "Who cares! Let's take the super long extra scenic route! It's pretty and has rails to protect us!". Though this analogy can be bad. C/++ can be safe if the developer we're to take the time invest in it where as Java forces it on you. C++ is also more flexible because of this but I've yet to figure out which is better in general. The reason being is that there are people (even on this forum probably) who execute functions and expect them to be perfect and DON'T EVEN CARE to take error checking into consideration. In Java this is bad but in C++, there can be serious consequences which I don't think people understand. Most libraries prevent this but for the library developers that do this and for those that play with native controls, you should think twice. Fortunately, most sane C and C++ developers do take this into consideration and anyone who doesn't is kicked off from there group or is told to f#$k off when releasing software.

Even then, there are languages that don't go the VM route and compile directly to ASM that have type and safe checking to stop the developer from making idiotic mistakes that will affect the user. I personally see no reason for Java other than platform flexibility. That's why the moment I put research into this, I stopped and learned something that might not go out of style in the near future. ASM and C/++ with a side of D for the win.
Last edited on
I hope Java does "go out of style."

f#$k

You spelt fuck wrong.
i think you miss my point here, and it's my fault. i am not comparing how this two languages are compiled or where they run. i am indeed comparing its syntax instead and the equivalent code of doing the same task on another language. i remember before that i have compiled java code before that can natively run in windows using GCC compiler for java which they call GCJ.

why not compare the way they run? well i think there are many geniuses out there who can make a compiler for a language that can run on a certain platform. if you were to write a dreamcast game you probably won't use compilers like MingW am i right? so does anyone could make a compiler for c++ to be compiled in bytecode just like java so it runs on JVM. isn't that interesting?

anyway please feel free to correct me if you think i am wrong. i'd love to learn from you guys
closed account (S6k9GNh0)
blackcoder41, though you say that, Java and C++ do not do "the same thing" at any occasion in time. The instructions executed are always different and there are almost no perfect similarities. Just because they call something that looks and acts like a class in Java doesn't mean it acts and is the same thing as a class in C++. What they do internally is completely different and sometimes even from a users point of view. They have two completely different locations in the world.

You'll also find many C/++ developers and many Java developers that shun GCJ as it's not natural. It's C++ that takes Java source and converts (what's supposed to convert to Java bytecode) into native machine code. This is in many ways BAD and can cause a lot of problems (although I've heard it's rather stable). I'd hate to maintain that crap...
Last edited on
Just because they call something that looks and acts like a class in Java doesn't mean it acts and is the same thing as a class in C++.

yeah i know that they do different things internally.

i also get rid GCJ off my computer a long time ago, i think it defeats the purpose of java which is "write once run anywhere"

i think C++ and Java or any language should never be compared for it is made for different purposes, sorry for posting this thread.

anyway lets go on and and code C++, that's why we're here right?
You can dev for the dreamcast with ANY C/++ compiler. What matters is the API. There is an API for the dreamcast called KallistiOS that can be used for dreamcast dev.
IMO, java is moving more towards the nub friendly programming style, in fact. As far as file handling goes next release is going to be retardedly easy. no try/catches no error checking. just a simple;

openfile(), process it as required...

that's it, you don't even need to close the damn thing because aparently they are working on that problem too... rewriting syntax to upgrade poor programming. and make poor programmers slightly less poor.
Neither in C++ you need to close file streams
Last edited on
?? I always close my file streams in C++
I think he means to be read like this:
Bazzy wrote:
Neither; in C++ you need to close file streams.

But I agree, java doesn't need deletes, destructors, etc.
me too.

IMO, java is moving more towards the nub friendly programming style, in fact. As far as file handling goes next release is going to be retardedly easy. no try/catches no error checking. just a simple;

openfile(), process it as required...
i don't like that, IMO too much high level is bad.
No, you don't have to close files in C++. fstream's destructor closes the file if one is open IIRC.
You don't need to close files in C, either. A FILE is a dynamically allocated pointer to the FILE structure, so not fclose()ing it is the same as not free()ing a dynamic pointer.

Anyway, wouldn't most operating systems close files a process has open automatically anyway when the process terminates? I'm not saying you should leave files open, but...?
Pages: 12