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
13
votes
4 answers

How does casting this object to a generic type work?

My understanding is generic types are invariant, so if we have B as a subtype of A, then List has no relationship with List. So casting won't work on List and List. From Effective Java Third Edition we have this snippet of code: //…
Hamza Belmellouki
  • 2,516
  • 1
  • 18
  • 39
13
votes
2 answers

Do we need a .build() method in the Builder Pattern?

I had a question regarding the "Builder Pattern" covered in "Effective Java". Do we need a .build() method for it to correctly implement the pattern? For instance, let's say that we have the following class: public class CoffeeDrink { private…
victormejia
  • 1,174
  • 3
  • 12
  • 30
13
votes
5 answers

What is the difference between a compile time type vs run time type for any object in Java?

What is the difference between compile time and run time type of any object in Java ? I am reading Effective Java book and Joshua Bloch mentions about compile time type and run time types of array instances in Item 26 many times mainly to describe…
Geek
  • 26,489
  • 43
  • 149
  • 227
12
votes
2 answers

Why did Joshua Bloch use 2*size + 1 for resizing the stack in Effective Java?

This is the code I am talking about: public class Stack { private Object[] elements; private int size = 0; private static final int DEFAULT_INITIAL_CAPACITY = 16; public Stack() { elements = new Object[DEFAULT_INITIAL_CAPACITY]; } …
Tintin
  • 2,853
  • 6
  • 42
  • 74
12
votes
3 answers

Effective Java Item 17: How can overriding removeRange() improve performance?

In the book Effective Java by Joshua Bloch, there is a discussion on how a class can provide "judiciously chosen protected methods" as hooks into its internal workings. The author then cites the documentation in AbstractList.removeRange(): This…
Atif
  • 942
  • 1
  • 6
  • 19
10
votes
1 answer

unchecked exception that would have been better as checked

I realize that there has been ample discussion of the relative merits of checked exceptions versus unchecked exceptions in Java, and it is not my intention to revisit the entire debate. Rather, I would like to ask a very specific question that came…
pohl
  • 3,158
  • 1
  • 30
  • 48
9
votes
1 answer

In constructor method references, difference between using generic type parameters and not?

I'm reading Effective Java 3 and noticed this code in Item 43: “Prefer method references to lambdas”: TreeMap::new Notice the type parameters. I've always just done: TreeMap::new I use Intellij and have never gotten a warning about this or…
Ivan
  • 239
  • 1
  • 13
9
votes
2 answers

Builder pattern validation - Effective Java

In Item 2 of Effective Java (2nd Edition), the author mentions the following about imposing invariants on parameters while using Builders: It is critical that they be checked after copying the parameters from the builder to the object, and that…
Abhigyan Mehra
  • 215
  • 1
  • 4
  • 7
9
votes
1 answer

When planning for inheritance, are constructers allowed to call overridable method?

From Effective Java 2nd edition, item 17: For each public or protected method or constructor, the documentation must indicate which overridable methods the method or constructor invokes Later in the same item it says: Constructors must not…
traveh
  • 2,700
  • 3
  • 27
  • 44
9
votes
3 answers

Why are the angle brackets before the return type omitted sometimes from the definition of a generic method

I was reading Effective Java chapter 5 about generics, in particular the items on preferring generic methods. I noticed that sometimes the type parameter(between angle brackets) in the method declaration before the return type is sometimes omitted.…
user2262955
  • 291
  • 2
  • 3
  • 12
9
votes
3 answers

Java Model Objects design

So I've been reading some Effective Java! And one of the most inspiring sections of the book is the Immutable Object/Builder section where Bloch writes about the "Builder" - class instead of just POJOs. NOTE: I am talking about model objects here:…
Johan S
  • 3,531
  • 6
  • 35
  • 63
9
votes
3 answers

What does Joshua Bloch mean by extra-linguistic?

From this Artima article on clone vs copy constructor: Object's clone method is very tricky. It's based on field copies, and it's "extra-linguistic." It creates an object without calling a constructor. There are no guarantees that it preserves…
Geek
  • 26,489
  • 43
  • 149
  • 227
8
votes
3 answers

Effective Java item 1 applicability with TDD and dependency injection

I have been reading Effective Java and I have some concerns regarding the first Item "use static factory method instead of constructor" in relation to TDD and dependency injection. The item says that you should avoid having…
Shekhar
  • 5,771
  • 10
  • 42
  • 48
8
votes
3 answers

Can an enum have a constructors for each of its constants

Please look at this link. In his book Effective Java, Joshua Bloch says Note that the Operation constants are put into the stringToEnum map from a static block that runs after the constants have been created. Trying to make each constant put…
Ankit
  • 251
  • 2
  • 9
8
votes
3 answers

how caching hashcode works in Java as suggested by Joshua Bloch in effective java?

I have the following piece of code from effective java by Joshua Bloch (Item 9, chapter 3, page 49) If a class is immutable and the cost of computing the hash code is significant, you might consider caching the hash code in the object rather…
brain storm
  • 30,124
  • 69
  • 225
  • 393
1
2
3
11 12