package org.my.java;
public class TestTypeVariable {
static <T,A extends T> void typeVarType(T t, A a){
System.out.println(a.getClass());
System.out.println(t.getClass());
}
public static void main(String[] s){
int i= 1;
typeVarType("string", i);
}
}
when run, following is the output :
class java.lang.Integer
class java.lang.String
How can A
be of type Integer
when it has been already upper-bounded to String
?
Please explain me on it.
Yes, variables belonging to different function can have same name because their scope is limited to the block in which they are defined. Variables belonging to different functions in a single code can have same name if we declare that variable globally as shown in the following code snippet below .
You can change the data type for a variable at any time by using the variable Type setting in the Variables tab. Existing values are converted to the new type.
The class interface or enum expected error is a compile-time error in Java which arises due to curly braces. Typically, this error occurs when there is an additional curly brace at the end of the program.
It is called generics.
Two things here:
getClass()
happens at runtime, and therefore returns the specific class of the objects passed - independent on what the compiler thinks about generics at compile time.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With