Java as a second language?

Started by
13 comments, last by Drathis 13 years, 8 months ago
I've been programming in C++ for the last two years, but now I have to learn Java. The problem is that I placed out of a CS course with AP credit, but I barely learned any Java in APCS.

What would the best way to go about this be? What are the most important differences that would trip up a C++ programmer? What do you think that a person who has taken an introductory college course in Java would be expected to know?
I trust exceptions about as far as I can throw them.
Advertisement
Once knowing C++ it should not be hard at all to adapt to Java so I dont think you need to take a whole beginners course since all you will be needing to learn is some coding conventions and syntax.

This should be fine for you http://download.oracle.com/javase/tutorial/java/index.html
I made the same transition: 2 years of C++ in college, transferred to a different school for my BS and had to switch to Java.

What tripped me up was the transition to "reference by default". Where C++ passes by value, Java will always pass by reference because all objects are treated like references. (and if I just horribly mis-described that, someone more knowledgeable feel free to smite me, this is just how it was explained to me).

I miss operator overloading and copy constructors as well, but you can hack around that with .copy() and .add()/.subtract() methods (I did this while putting together a vector class for a game programming course).

As well: no header/implementation file woes, it's all in one file (one file per class). No free-floating methods either, they have to belong to a class. Static classes will let you call methods without a class instance (so if I create a static Math class, I can use its methods without creating an instance of that class, etc)

Your data structures and programming paradigms should mostly be unchanged past that...the Java API is a great reference for little differences, and I found the programming tests at CodingBat (formerly JavaBat) to be great hands-on practice in Java.

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

Quote:Original post by Storyyeller
What are the most important differences that would trip up a C++ programmer?
No pointers and the likes - in Java all class types are reference types while primitive types are value types and all references and values are passed-by-value.

Boxing - primitive types like int turning into reference types like Integer and vice versa.

Anonymous inner classes - Pretty straightforward and self-descriptive, you just don't get them in C++, that's all.

No default parameters on methods - Sorry. At least you can chain constructors, unlike C++.

No copy constructors - There is an element of 'duh' about this when you wrap your head around the lack of value types for classes. You can make things that look and work similarly to copy constructors and that's fine but obviously, unlike C++, Java isn't treating it as anything special.

Garbage Collection - Memory is managed for you, almost...

Non-deterministic destruction - You can't guarantee when or if an object will be finalised by the garbage collector so for objects that wrap unmanaged resources, like a file stream, you need to remember to call close() else you leak resources. Which brings me swiftly to:

Almost no RAII - One of the best features of C++ is RAII and Java almost totally lacks it although you can glean it back with complex arrangements of try-catch-finally blocks and perhaps clever use of exceptions, but it's barely a substitute IMHO. I gather the next version of Java may be adding RAII, I hope so.

Checked and unchecked exceptions - Some exceptions you absolutely have to catch and handle; if you don't then you have have to explicitly declare them on the method signature so that the caller has to handle them. All of C++'s exceptions behave like unchecked exceptions; Java has both kinds.

Off the top of my head that's all I can think of. Of course there are other differences but they shouldn't be too surprising or pitfall-like.

Quote:What do you think that a person who has taken an introductory college course in Java would be expected to know?
Only the basics, conditional logic, loops, methods, classes etc. If you know all these things already then an introductory Java class will be a doddle.

[Edited by - dmatter on August 23, 2010 11:47:54 AM]
Quote:Original post by Storyyeller
What are the most important differences that would trip up a C++ programmer?

The fact that no matter how good or bad the code you write is - it will work. Perhaps not correctly, but it will work. No segfaults, no unusual values, no undefined behavior, no core dumps, no leaks.

Second issue is that in Java ecosystem, memory and other resources are unlimited. You *do not* optimize for memory use - JVM knows better than you how to handle it. You *never* write your own algorithms, since standard library knows better and provides List<> and Map<>, which is entire CS knowledge wrapped into two classes. You don't organize project, today's OSes are more than capable of dealing with 100,000+ files in your Maven repository and Eclipse gives you refactoring and code completion.

There are no good frameworks. In open source community there are only tens of thousands of frameworks. None of them are any good. So you must invent your own. Make sure to have no experience with actual development, just use patterns everywhere. It will work better and make your job much easier.

Also - XML. You cannot have too much of it. If anything can be done using XML - use XML to describe that XML. Then make XML to configure it. Rumor has it that XML is a complex thing with DTDs, namespaces and such - which is clearly wrong. XML is stuff that has <> in it. So use more of it.

If your Eclipse launches faster than 30 seconds, you are not enterprisy enough - install more helpers - they make you more productive. And if your app server restarts faster than in 3 minutes, then you are wasting time with premature optimization - stop it now, you're making maintenance nightmare. Add more patterns and XML. There are clearly not enough abstractions.

Quote:What do you think that a person who has taken an introductory college course in Java would be expected to know?

All of GoF patterns. At least first 7 of them. Definitely Singleton and Factory. Visitor if you want to be fancy. Don't even think about algorithms, data structures or anything similar. You'll be labeled as old COBOL guy who is out of touch with the times.
Hilarious Antheus. You should be a stand-up. I don't think that kind of post belongs in For Beginners.
Upon the first lines of reading, I thought that would be all serious. Then the entire CS knowledge thingy made me thinky for some seconds, and then I roffled all over the place. As often as I disagree with Antheus (I think), this one made me rate him up without any further ado.
If you know singleton you don't need to know any other patterns because it replaces all of them cause you only need one, doesn't it?
"It's like naming him Asskicker Monstertrucktits O'Ninja" -Khaiy

As much as I disagree with how Antheus attacked the situation, the truth is pretty much all of what he said is how we were taught at uni, lucky for me I taught myself correctly way before going to uni :p.
Quote:Original post by phresnel
Upon the first lines of reading, I thought that would be all serious.


First paragraph is serious. To a C++ programmer, Java is impossible to break.

The rest is true as well, just shouldn't be taken literally. It's more of a difference in culture between two language ecosystems.

This topic is closed to new replies.

Advertisement