• Post Reply Bookmark Topic Watch Topic
  • New Topic

Core Java for the Impatient question

 
Tomas Linhart
Ranch Hand
Posts: 86
2
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

would you suggest this book as a first book to read for someone, who already knows basic Java, but haven't got in touch with Java 8 yet? I've been currently learning Java from a book covering Java 6 but want to learn all the new features once I'm on a decent level.

Thanks
 
Cay Horstmann
author
Ranch Hand
Posts: 172
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I wrote this book from scratch (instead of updating the 20-year old Core Java) for a simple reason. It is ineffective to learn Java by reliving its history. There have been a good number of false starts, both in the language and in the library. You are better off learning the good ways first, and then maybe, if you need to deal with legacy code, learn the ways of the past.

For example, look at interfaces. In Java 8, you can have default methods, which are really very nice. I use them all the time. But before, you had to make an interface and then an abstract class. There is nothing to be gained by learning and then un-learning that.

Or look at anonymous inner classes. They are complex, and 99% of them can be replaced by lambdas. Pretty soon the only ones who need to know about anonymous inner classes are the poor souls who study for certification exams.

And, perhaps most importantly, concurrency. The old way of explaining it is all wrong. It starts with threads and locks, and then after learning all about them, there is the admission that you should probably not use locks unless you are a concurrency wizard. And then you learn about volatile, and why that is fraught with complexity. And then all the low-level concurrency constructs in the library that again are for wizards. So, I decided to start with the things that you *should* be using: immutability, parallel streams, parallel algorithms, threadsafe data structures, executors, atomics.

Cheers,

Cay

 
Tomas Linhart
Ranch Hand
Posts: 86
2
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Cay, that seems reasonable :-) The "problem" is, at the time I made the decision to start learning Java, Java 8 was still very new. When I was looking for a good book to learn from, there was not really a lot to choose from. So I sticked to time proven best Java books, which cover older releases though. But you are right, it's better to start with current best practices, when there is a good choice (as your book seems to be :-)).
 
Rob Spoor
Sheriff
Posts: 20819
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cay Horstmann wrote:Or look at anonymous inner classes. They are complex, and 99% of them can be replaced by lambdas. Pretty soon the only ones who need to know about anonymous inner classes are the poor souls who study for certification exams.

I partly disagree. Yes, lambdas can (and probably should) replace anonymous inner classes which are implementations of functional interfaces (e.g. Runnable, Callable, Predicate and ActionListener). However, there are other interfaces that have more than one method that can be implemented (e.g. MouseListener). There are also abstract classes that can be implemented through anonymous inner classes (e.g. AbstractAction). These will remain because Java 8 hasn't provided a new, better alternative for them.
 
Cay Horstmann
author
Ranch Hand
Posts: 172
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sherriff! In theory, you are absolutely right. If you have an interface with multiple abstract methods, then lambdas won't help you at all. But in practlce, this is astonishingly rare. I mean, how many mouse listeners have you written in your life? I have written them at the rate of maybe one every year or two. If I had to write all ten of them as top-level classes, so what? With Java 8, the investment in learning the intricacies of anonymous inner classes just isn't going to pay off.

Now admittedly, anonymous inner classes are a part of the language, and you'll find them in legacy or smartass code, and yes, I do cover them in "Java for the Impatient". But only as a corner case, kind of like "labeled break", another language construct that hasn't exactly caught the hearts and minds of the developer community.

When you learn Java today, you should learn lambdas and understand them well. They are the present and the future. Then, if you find yourself with time on your hands and a penchant for archaeology (or the need to study for an outdated certification exam), go ahead and investigate the mysteries of anonymous inner classes.

I speak with some feeling here, because this is so often done wrong. It is NOT OK to tell beginning students that they need to relive the evolutionary history that you and I went through. For example, where I am spending my sabbatical, there is a band of professors who insists that freshman students need to know how to process strings in C because they might need it for embedded programming. Hello??? This makes no sense. Everyone uses C++, and the string class is universally available and far less error-prone. The freshmen should study algorithms and design, and not C pointers.

Cheers,

Cay
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do define a lot of methods which take an interface and it is still convenient to be able to create an anonymous class as an argument in-situ during the invocation. I'm used to tuples in python, so this is how I get my fix in Java8 - unless you can suggest an improvement.

As for labelled breaks; I use them very often. I probably have used them three times last year.
 
Cay Horstmann
author
Ranch Hand
Posts: 172
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't call "three times a year" very often :-)

It's more instructive to look at a large body of code, for example the JDK library source (src.zip inside the JDK distribution).

To locate labeled breaks, let's unzip and run



With Java 8, you get 34 files (out of a total of 7713). I think it's reasonable to conclude that there is no widespread love for labeled breaks.

We'll have to see what will happen to anonymous classes. When you look at your code, how many times do you produce an anonymous instance with more than one method? Other than those mouse listeners :-)?

I am not sure I understand how it helps with tuples. The standard remedy is to create parameterized classes Tuple2[T1, T2], Tuple3[T1, T2, T3], etc., but there is no need to subclass them.

Cheers,

Cay

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cay,

I have found the solution to Tuples in java8. I use Lombok to create throwaway value objects.

This creates an object with immutable properties.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!