# How to learn Java as a beginner?

I want to start learning Java from scratch. But as a beginner, I don't know where to start or what to learn first. I have basic knowledge in C++ but Java is an entirely new area to me. Can anyone give me some instructions and good books to start properly?

The first thing you need to understand is that your second language is, perhaps the hardest of all to learn. You already know "how to program" and so the second language seems to get in the way of that. Alternatively you just use the second language in the same way you used the first, resulting in ugly and sub-optimal programs. So, you want to guard agains that.

It is actually somewhat easier to learn a second language if it is nothing at all like the first, since you can't try to impose habits from one language on the other and have to absorb the actual philosophy of the new language. So, Scheme, for example, after C++ (or before) forces you to think in a new way.

So, learning Java after C++ is especially difficult, since the lowest level syntax of the two is nearly the same, but the underlying philosophy (paradigm) is quite different. In particular, Java and C++ both use what they call classes but they are really different creatures.

One book I'd recommend very highly (since I know and trust one of the authors) is by Barnes and Kölling. It does, however depend on also using the BlueJ IDE which they created and is available free online.

The reason I recommend it is that it introduces objects from the beginning, which is the fundamental difference between C++ and Java. So, if you put aside what you think you know about programming and believe in the authors, you should do well. They both have a very (very) deep understanding of what Object Orientation means.

An alternative that would work, if you somehow object to BlueJ is Cay Horstmann's Big Java Early Objects. It, too, starts with objects which is my primary recommendation. I also know Cay. He also has a Late Objects version that I don't recommend for a bunch of reasons, but one is that the first part of that book would be likely to bore you. While it might seem an easier path, it won't take you where you need to go as directly as the Early Objects version.

A third possibility, though it is intended more for first time programmers rather than for someone with experienced in another language, is Karel J Robot by Bergin, Stehlik, Roberts, and Pattis. This book focuses entirely on object oriented programming in Java but does not try to teach the entire language. It does, however, provide a system in which any program can be written. Like Pattis' original Karel the Robot it is an attempt to provide a minimal but complete system including a simulator in which interesting (and difficult) programs can be written.

• Thanks for the insightful advice. Can I ask more about what differs the two languages specifically ? I would like to have a throughout understanding so I might not get on the wrong foot with the Java. Feb 15 at 6:14
• An "object" in C++ is created on the stack normally, though you can also create one on the heap. In Java it is always created on the heap and will be garbage collected automatically. Every C++ program that creates objects on the heap has to also have a management strategy for them. The difference may seem small but affects how you think about problem solution. There is no "natural" dynamic polymorphism in C++. Feb 15 at 12:29
• Re, "an 'object' in C++ is created on the stack..." I think what you are trying to say is that a C++ object can be stored in a variable. A variable could be a local (on the stack), or it could be elsewhere. And, a C++ assignment, a=b; is a function call that is conventionally expected to create a copy of object b in the variable a after it first destroys the previous value of a. In Java, on the other hand, a variable can never hold an object, it can only ever hold a reference to an object, and an assignment can only ever create a new reference to the same object. Feb 16 at 15:50
• @SolomonSlow, yes, all of that is true and well stated. Thanks. Feb 16 at 15:54

I'd like to reassure you that if you are accustomed to C++ Java won't be something really difficult to understand. Yeah, it's another language with its peculiarities but nothing enormously hard to cope with. Start from the basics - this rule always works the best. It's OK to be afraid of new things but they are interesting challenges at the same time. Regarding the books, I recommend you this one - Java: Programming Basics for Absolute Beginners by Nathan Clark (found it in the list with various best java books). It helps to structure the programming language and to get its concept even for a newbie.

Along with books, you may add some online java courses. It's a great starter pack and the point where to start from.