views:

294

answers:

11

What books can you recommend for learning Java for a C++ programmer? Also what do you recommend to read for learning JVM features for using JVM-based languages effectively?

A: 

I switched from C++ to Java a few years back.

I found it useful to read Java in a Nutshell as I didn't need an intro to OO practices - just some grounding in the background theory of Java.

I seem to recall it had a section for C++ programmers but I could be wrong or it may not be in there for newer versions. I just checked in the 5th edition on Safari Books Online and it does indeed contain a section discussing difference between C++ and Java.

Darren Greaves
+4  A: 

You could start with the Head first book for Java. Then jump to Effective Java once you've gotten hold of the basics.

sylv3rblade
Head first book good for start
SjB
I would have thought if they are intelligent enough to program in C++ they can skip the comic book and move to a real book like the Nutshell one. Sorry, not a fan of the whole Head First series - if you need stupid pictures to keep you interested in a technical book, maybe you picked the wrong profession.
U62
Head first got a recommendation for it's contents not it's pics or the way it's written to keep the reader's attention FYI.
sylv3rblade
The content in the book (especially the little notes at the side) are great. Funny thing, myself and another instructor love the books (because of the content) but the students cannot stand them (because of the pictures). Too bad since a lot of what I tell the students as advice is exactly what is written in those little comments at the side.
TofuBeer
+1  A: 

I read Deitel for C++ and also read Deitel for Java and I think Deitel for Java better than Deitel for C++ .

Java How to Program, 7th Edition

alt text

book description :

The Deitels' groundbreaking How to Program series offers unparalleled breadth and depth of object-oriented programming concepts and intermediate-level topics for further study. The Seventh Edition has been extensively fine-tuned and is completely up-to-date with Sun Microsystems, Inc.’s latest Java release — Java Standard Edition 6 (“Mustang”) and several Java Enterprise Edition 5 topics. Contains an extensive OOD/UML 2 case study on developing an automated teller machine. Takes a new tools-based approach to Web application development that uses Netbeans 5.5 and Java Studio Creator 2 to create and consume Web Services. Features new AJAX-enabled, Web applications built with JavaServer Faces (JSF), Java Studio Creator 2 and the Java Blueprints AJAX Components. Includes new topics throughout, such as JDBC 4, SwingWorker for multithreaded GUIs, GroupLayout, Java Desktop Integration Components (JDIC), and much more. A valuable reference for programmers and anyone interested in learning the Java programming language.

SjB
+1; I second Deitel^2 as an excellent and very comprehensive Java book, well and thoroughly presented. It could well be the only Java book you'll ever need. But I think it's only fair to point out that this is a very big book. Only by printing on thin paper do they manage to get my 3rd edition's 1350 pages into less than 2 inches. A "whirlwind tour" this is not!
Carl Smotricz
This book sucks, it is written by people who do not have a clue. I use this as an example of why people who write books generally are terrible programmers (however no -1 since this is all opinion :-)
TofuBeer
@TofuBeer - could you elaborate? Do they show bad code? Give bad advice? When explaining a language feature one may show code that's over-simplifed. There's plenty of counter examples of book authors who can code: Kernighan, Ritchie, Knuth, Bloch, Meyer, Botzum, Hines to name but a few.
djna
I wasn't going to say this, but D^2's ponderous expository style makes their books about as exciting as telephone directories. They suck in terms of gaining and captivating an impatient reader's interest. But I have used the book as a reference on some occasions when I didn't have access to other materials, and for any needed topic I found their treatment thorough and correct. I rarely come across a book with such a low incidence of errors. If this is sucking, I wish more books sucked!
Carl Smotricz
+1  A: 

Most of the time, when coding against APIs such as those provided in the Java SDK the fact that there's a JVM involved doesn't really intrude.

Two places where the JVM facilities come into play that we do need to consider are synchronisation and garbage collection. The Java memory model is a little more subtle than it may first appear, and the implications of synchronisation more extensive than just establishing a mutex. You might start at this SO question to get a flavour.

Then GC - is it quite easy to write code that produces unreasonable amounts of garbage and hence results in lots of GC activities. So studying up on useful classes such as StringBuilder and the treatment of String constants by the compiler may also be useful to you. A starting point for study of GC could be here.

I too found Java in a Nushell to be useful when making the transition from C++ to Java. Also Bloch's Effective Java.

djna
+1  A: 

"Back when I was your age, we had to walk... "

But seriously, I've been doing Java since before the deluge of "teach yourself Java in 21 hours" books, and some of the books I hazarded upon were not that useful.

What I found tremendously useful was The Java Tutorial, straight from the horseSun's mouth. I'm talking about the online resource here. Having casually picked up the basics (but they're also described there), I'd end up just needing the lowdown on how to deal with some particular API, be it networking or Swing GUIs or whatever. So I could just flip through the tutorial and let them show me how to use it, with examples and pretty pictures and all.

I'd be overstating only mildly if I were to say that everything I learned about Java, I learned from the Tutorial.

The (online) Tutorial is kept up to date and occasionally expanded, which is an advantage over dead-tree books. On the other hand, Sun is only too happy to sell it to you in book form. It's up to its 4th edition now. alt text

Carl Smotricz
The Tutorial only covers things in the Java runtime. If you have not learned more than that, you still have a lot to learn :D
Thorbjørn Ravn Andersen
+1  A: 

Remember that Java does not have automatic objects, so objects initialized inside the scoped code block don't get cleaned up immediately, they just become eligible for garbage collection. In some situations it may never happen ( or not happen for a very long time ).

The point of this, finalize method id java is not a java's destructor. Objects that consume system resources have to be specifically told to release them.

Case in point IO object ( Input/OutputStream and their derivatives ). You must call close method after you've done using them otherwise you are going to run out of file handles if your process is running for long enough time.

I am telling you this, because this was one of the first things that bit me in the @$$ when I switched from C++ to Java. Later I confirmed this to be a recurring pattern with other developers switching from C++ to Java.

Use this pattern for objects that are holding limited resources

final InputStream in = new FileInputStream( "foo.txt" );
try
{
  // use in
}
finally
{
  in.close( );
}

As for the books I recommend Effective Java, and for newer features of the language Java Generics and Collections.

Alexander Pogrebnyak
+1  A: 

"The Java Programming Language" from inventor of java lang "James Gosling" is nice book

Ashish
+8  A: 

Bruce Eckel's Thinking in Java was written for just this purpose, the PDF version is free for download.

rsp
+1 since it was the book I was going to say :-)
TofuBeer
+1  A: 

I think the Deitel book, Java How to Program, 7th Edition is great. I really like the Deitel books, they explain concepts very concisely and they have a lot of really good examples. They also give examples that are used to build on other examples through out the book. For example you may be working on some example in Chapter 1 and then you may go back and use that code in say Chapter 5.

I learned C and C++ in college using these books and did the same with JAVA, I highly recommend them.

http://www.amazon.com/Java-How-Program-Harvey-Deitel/dp/0132222205/ref=sr%5F1%5F3?ie=UTF8&s=books&qid=1260034507&sr=8-3

ralphL
A: 

I was in a similar situation as you, and the book I liked the most is O'Reilly's Learning Java. It's got a pretty good combination of breadth and depth, and is easy to follow.

Jason S
A: 

You already know OO, why repeat the basics, just get a feel for the language:

Update: Whoops, the image is now gone, but I was suggestiong Java In A Nutshell.

Xepoch