knowing java like c++

Started by
16 comments, last by Kaijin 19 years, 6 months ago
Well my main programming language is c++ and I feel quite comfortable with it. Now it turns out I have to use java for some stuff in the university. Ive used java before for little things but since this will count to my uni score I want to be as good as possible. Basically I want to write java code as I write c++ code. In order to do this I was wondering wether theres a tutorial out there which quickly explains all the functions of the tool: java(with examples) Things like polymorphism, inheritance, function overloading, type casting... Perhaps someone could also give me a brief summery as to what posibilities such as these java doesnt support and c++ does, so I dont try implementing these. Any further tips on major differences please tell me. -CProgrammer
Advertisement
All the information you need is on sun's website, you might wont to start looking here and for more in-depth articles javaworld is pretty good.

Off the top of my head things it doesn't have that c++ does:

macros (although you can get third-party tools to do something simillar)

templates (although java 1.5 & up has generics not exactly the same but they both give you parametric types).

operator overloading.

multiple inhertiance (although you can have multiple interface inheritance AKA type inheritance).

protected & private inheritance.

no free standing functions (although in java 1.5 & up there is something called static imports, so you can simiulate free functions using static/class methods).

all user-defined types & arrays are allocated on the garabage collected heap & are accessed through pointers they call them references not to be confused with C++ reference types although they are simillar but java references are really pointers underneath. You don't explicitly delete memory the garabge collector manages memory.

arrays are objects there not the same as C-style arrays.

all classes & abstract classes implicitly or explicitly derive from Object you have no choice in the matter.

i'm sure there is more but then again there also some nice things in Java that C++ doesn't currently support or have in the standard or standard library.
Quote:Original post by snk_kid
templates (although java 1.5 & up has generics not exactly the same but they both give you parametric types).

I disagree. The "generics" of Java are nothing more than an auto-cast.

Thermo
Quote:Original post by CProgrammer
Well my main programming language is c++ and I feel quite comfortable with it.
Now it turns out I have to use java for some stuff in the university. Ive used java before for little things but since this will count to my uni score I want to be as good as possible.


I wouldn't worry about it in the slightest if I were you. The vast majority of people in your degree won't have touched an OO language before (or any language probably) so you'll have a massive head start and I'd be very impressed if they were even close to knowing Java as well as you by the time you finish. I went from C++ to Java and it's so much easier it felt like cheating. Features such as no undefined behaviour, no header files, simple syntax and not having to free memory takes a huge weight off your mind when programming and you can just get straight on with your task without having to worry about hundreds of obscure gotchas.

Quote:
Basically I want to write java code as I write c++ code.
In order to do this I was wondering wether theres a tutorial out there which quickly explains all the functions of the tool: java(with examples)


Search on google for things like "Java for C++ programmers". There's quite a few articles like this out there.
Quote:Original post by Konfusius
Quote:Original post by snk_kid
templates (although java 1.5 & up has generics not exactly the same but they both give you parametric types).

I disagree. The "generics" of Java are nothing more than an auto-cast.

Thermo


So what exactly is your point, templates generate classes/functions like i said they don't work the same but they have something in common they conceptually give you parametric types & type safety (even thou in java there is still castings going on).
Quote:Original post by Konfusius
Quote:Original post by snk_kid
templates (although java 1.5 & up has generics not exactly the same but they both give you parametric types).

I disagree. The "generics" of Java are nothing more than an auto-cast.


That's like saying C++ templates are nothing more than macros. It doesn't matter how they're implemented, it's the function that matters.
Does Java have Pointers?

if(this.post == SATISFYING){money.send(1.00,&HemoGloben);return thanks;}
Quote:Original post by HemoGloben
Does Java have Pointers?


It has a type called a "reference" which is like a mutable C++ pointer which can point to an object (i.e. it can't point to a primitive type or another reference). You can't do pointer arthimatic with them either. It seems limiting, but it stops me having to maintain code where somebody has done some wacky pointer maths to save themselves 2 lines of code whilst giving me a 2 hour head ache trying to work out what is going on.
Quote:Original post by seanw
Quote:Original post by HemoGloben
Does Java have Pointers?


It has a type called a "reference" which is like a mutable C++ pointer which can point to an object (i.e. it can't point to a primitive type or another reference).


You can have a "non-mutable" reference, just tack the keyword final to the begining then it will always refer to the same instance, alot of java programmers forget or don't understand what final actually does but they are useful in method arguments like having constant pointers for function arguments in C/C++, but its also used to mark non-derivable class (all methods are not virtual), constant values aswell.
Quote:Original post by snk_kid
You can have a "non-mutable" reference, just tack the keyword final to the begining then it will always refer to the same instance, alot of java programmers forget or don't understand what final actually does but they are useful in method arguments like having constant pointers for function arguments in C/C++, but its also used to mark non-derivable class (all methods are not virtual), constant values aswell.


I was just trying to point out the difference between a C++ reference and a Java reference because the wording can be confusing. You're right about a lot of programmers not knowing how to use 'final' properly; it means the reference cannot be made to point to another object but the object it points to can still be changed. I've seen people with code in their 3D vector class with a field declared as "public static Vector3D ORIGIN = new Vector3D(0, 0, 0)". :)

This topic is closed to new replies.

Advertisement