Given the cache class:
public class Cache {
private T t;
public void setValue (T t) { this.t=t; }
public T getValue() {return t; }
}
What is the result of the following?
Cache<> c = new Cache<Integer>(); // Line 1
A.
SetValue(100); // Line 2
System.out.print(c.getValue().intValue() +1); // Line 3
B.
101
C.
Compilation fails at line 1.
D.
Compilation fails at line 2.
E.
Compilation fails at line 3.
Explanation:
Compilation failure at line:
illegal start of type
type cache.Cache does not take parameters.
C)
it must be:
Cache c = new Cache(); // Line 1
instead of:
Cache c = new Cache(); // Line 1
Answer is 101;
here the correct source-code:
public class Cache {
private T t;
public void setValue(T t) {
this.t = t;
}
public T getValue() {
return t;
}
public static void main(String[] args) {
Cache c = new Cache(); // Line 1
c.setValue(100); // Line 2
System.out.print(c.getValue().intValue() + 1); // Line 3
}
}
This isn’t correct source-code.
“T cannot be resolved to a type”
must be :
public class Cache { …
public class Cache{ …
The HTML has error for viewing angle brackets
Below is the correct code.
public class Cache{
private T t;
public void setValue(T t){
this.t=t;
}
public T getValue(){
return t;
}
public static void main(String[] args){
Cache c=new Cache();
c.setValue(100);
System.out.print(c.getValue().intValue()+1);
}
}
The HTML has error for viewing angle brackets
Becaue they are empty in the first angle brackets of Line 1, so it Compilation fails at line 1.
The correct answer is C.
Should be
public class Cache
Instead of
public class Cache
And
Cache c = new Cache();
Instead of
Cache c = new Cache();
Angle brackets disappeared as Ray said!!!
1. The type Cache is not generic; it cannot be parameterized with arguments
2. The class Cache not compile : T cannot be resolved to a type for field and method
This question is incomplete
it should be like this:
public class Cache <T> {
private T t;
public void setValue (T t) { this.t=t; }
public T getValue() { return t; }
public static void main(String[] args) {
Cache<Integer> c = new Cache<>(); // Line 1
c.setValue(100); // Line 2
System.out.print(c.getValue().intValue() +1); // Line 3
}
}
Correct Answer: C.
Compilation fails at line 1.
+1
+1
+1
I agree with C because of 2 things:
1-The Cache class doesn’t have a type T
2-When creating a List for example the diamond presentation is like this:
List list = new ArrayList();
C