Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

generic method vs wildcarded parameter?

843793May 2 2010 — edited May 2 2010
In the following, 'doesntCompile' doesn't compile. As you can see, it uses a wildcard in it's parameter. The method 'alsoCompiles' compiles, but uses a type variable in it's parameter. Various books I've read discuss that type variables (in generic methods) are needed to resolve issues between multiple parameters, or between a parameter and the method's return type. But in this case, there is only one parameter and the return type is void. So I don't understand why 'doesntCompile' won't compile. I'd appreciate it if someone could clarify this issue. Thanks in advance...
import java.util.Iterator;

public class Test {
	class Bar<E> {}
	
	public void m() {
		compiles((Iterator<Bar<String>>) null);
		alsoCompiles((Iterator<Bar<String>>) null);
		doesntCompile((Iterator<Bar<String>>) null);
	}
	
	public void compiles(Iterator<?> x) {}

	public <T> void alsoCompiles(Iterator<Bar<T>> x) {}	
	
	public void doesntCompile(Iterator<Bar<?>> x) {}	
}

Comments

Processing
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on May 30 2010
Added on May 2 2010
3 comments
174 views