Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the question mark in java mean?

Tags:

java

I have encounter a code snippt

putAll(Map<? extends K, ? extends V> map)

on the Android developer site, I know that the K and V are placeholders, but what does the question mark ? mean? Does it mean that the param must be a reference type or something else?

like image 748
twlkyao Avatar asked May 26 '14 06:05

twlkyao


People also ask

What is the ?: operator in Java?

The ternary conditional operator ?: allows us to define expressions in Java. It's a condensed form of the if-else statement that also returns a value.

What does a question mark and a colon mean in coding?

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.13-Sept-2022.

What does the & symbol mean in Java?

The symbol & denotes the bitwise AND operator. It evaluates the binary value of given numbers. The binary result of these numbers will be returned to us in base 10. When the & operator starts its operation, it will evaluate the value of characters in both numbers starting from the left.

What is single colon in Java?

It is used in the new short hand for/loop final List<String> list = new ArrayList<String>(); for (final String s : list) { System.out.println(s); }


2 Answers

In Java ? is known as Wildcard, you can use it to respresent an unknown type.

The upper bounded wildcard, , where Foo is any type, matches Foo and any subtype of Foo. The process method can access the list elements as type Foo:

public static void process(Map<? extends A> list) {
  /* code */
}

In your case it is known as Upper bounded wildcard.

http://docs.oracle.com/javase/tutorial/java/generics/upperBounded.html

putAll(Map<? extends K, ? extends V> map)

It means, that any object, that can extend the A class is applicable in this conditionn.

like image 57
Afzaal Ahmad Zeeshan Avatar answered Oct 13 '22 02:10

Afzaal Ahmad Zeeshan


It's a wild card for class type, in your case

putAll(Map<? extends K, ? extends V> map)

the first question mark indicates that any type which extends K is applicable, and so on.

You can read more here

like image 36
Ahmad Dwaik 'Warlock' Avatar answered Oct 13 '22 03:10

Ahmad Dwaik 'Warlock'