Questions tagged [effective-java]

Effective Java is a book by Joshua Bloch "designed to help Java programmers make the most effective use of the Java programming language and its fundamental libraries"

Effective Java is a book by Joshua Bloch of patterns "designed to help Java programmers make the most effective use of the Java programming language and its fundamental libraries".

178 questions
5
votes
2 answers

Why are many Android API classes non-final, even though they are not explicitly documented for inheritance?

Effective Java (Joshua Bloch) Item 17 says : "Design and Document or inheritance or else prohibit it" However, just a cursory glance through the Android APIs reveals that most of the API classes are non-final; which is OK if they are also…
curioustechizen
  • 10,572
  • 10
  • 61
  • 110
5
votes
2 answers

Item 9 (equals contract) from Effective Java: is the example correct?

Bloch's wonderful book "Effective Java" points out that if equals is not symmetric then the behavior of Collections contains is indeterminate. In the example he gives (reproduced with small modifications below), Bloch says that he sees a "false",…
adnan
  • 196
  • 7
5
votes
0 answers

Java reserved type name "var" best practices?

Unfortunately, there is no item in Effective Java that discusses the reserved type name "var"(since it is introduced in java 10). There is a saying to use interfaces as types whenever you can (Item 64: Refer to objects by their interfaces). But…
Hamza Belmellouki
  • 2,516
  • 1
  • 18
  • 39
5
votes
2 answers

Effective Java item 16 (2nd edition) - Is Forwarding class only used to allow re-use?

I am going through Effective Java, Item-16 Favor composition over inheritance. I looked at the Forwarding class example below. I am wondering what's the point of having a ForwardingSet class? InstrumentedSet can very well implement Set and have a…
linuxNoob
  • 600
  • 2
  • 14
  • 30
5
votes
1 answer

Why javadoc parameter-less constructors?

In item 56 of Effective Java (Third Edition), Joshua Bloch states: "Public classes should not use default constructors because there is no way to provide doc comments for them." The default contructor doesn't do anything unexpected, though, it just…
Tom Tresansky
  • 19,364
  • 17
  • 93
  • 129
5
votes
1 answer

What is wrong with this Java Puzzlers piece of code?

In new, third edition of Effective Java Joshua Bloch mentions piece of code from Java Puzzlers (it's about closing resources in try-finally): For starters, I got it wrong on page 88 of Java Puzzlers, and no one noticed for years. In fact,…
ctomek
  • 1,696
  • 16
  • 35
5
votes
1 answer

Why UnaryFunction can be casted to UnaryFunction?
When I read Effective Java item 27, the type casting between UnaryFunction and UnaryFunction confused me. interface UnaryFunction { T apply(T t); } public class Main { private static final UnaryFunction IDENTITY = new…
Robin Han
  • 101
  • 7
5
votes
3 answers

bitwise operator >>> in hashCode

I have two related questions: the bitwise operator >>> means that we are shifting the binary number by those many places while filling 0 in the Most Significant Bit. But, then why does the following operation yields the same number: 5>>>32 yields 5…
Gaurav
  • 1,570
  • 4
  • 20
  • 25
5
votes
3 answers

Effective java Item no 74(on serialization): Implement Serializable judiciously

It item no 74 of effective java book there is a paragraph (2nd para from last of the item 74) which mentions as per below: Inner classes (Item 22) should not implement Serializable. They use compiler-generated synthetic fields to store…
Trying
  • 14,004
  • 9
  • 70
  • 110
5
votes
3 answers

In what ways can putting some code in try catch blocks prevent the JVM from doing optimization?

From Exceptions chapter in Effective Java: Placing code inside a try-catch block inhibits certain optimizations that modern JVM implementations might otherwise perform Why and how does a try-catch block prevent optimization by JVMs ?
Geek
  • 26,489
  • 43
  • 149
  • 227
5
votes
2 answers

Need explanation for hashcode example in Effective Java textbook

Here's the sample code from Item 9: public final class PhoneNumber { private final short areaCode; private final short prefix; private final short lineNumber; @Override public int hashCode() { int result = 17; result = 31 * result…
Kes115
  • 2,070
  • 2
  • 21
  • 37
5
votes
2 answers

Why is the volatile field copied to a local variable when doing double check locking

I am reading about double check locking from Effective Java. The code does the following: private volatile FieldType field; FieldType getField() { FieldType result = field; if (result == null) { // First check (no locking) …
Jim
  • 18,826
  • 34
  • 135
  • 254
4
votes
2 answers

Is terminating an object is the same as nulling it?

So i have been going through "Effective Java 2nd Ed." In item 7 he talks about not using finalizers because they can cause a lot of problems . But instead of using finalizers we can " provide an explicit termination method" and an example of those…
titorat
  • 83
  • 8
4
votes
1 answer

Effective Java Item 76: Serialization & Security - How exactly hacker gets references to the internal Date fields of immutable Period object?

While I understand the overall point this item makes, I'm interested in understanding in depth how exactly the hacker gets the internal references of the mutable Period object. He corrupts the internal Date fields through these references so it's…
Saurabh Patil
  • 4,170
  • 4
  • 32
  • 33
4
votes
1 answer

ElvisStealer from Effective Java

Here is the class stealing reference to a copy of a singleton while singleton is deserialized. public class ElvisStealer implements Serializable { static Elvis impersonator; private Elvis payload; private Object readResolve() { …
ctomek
  • 1,696
  • 16
  • 35