Generic, "generic" means broad, and "type" means data type. As the name suggests, a generic type is a type suitable for multiple data types. It is usually defined by Object. In theory, it can store any type of data.
If we have studied arrays, we should know that the same data types are stored in arrays, such as integer and floating point. Can we use Object to define an array to store data of different data types?
class MyArray{ public Object[] array=new Object[10]; }
This array can store data of ten Object types. Let's write another method to assign value to it
class MyArray{ public Object[] array=new Object[10]; public void setVal(int pos,Object val){ this.array[pos]=val; } }
Note that when assigning values, its value val should also be defined by Object
When assigning a value to it, val can be of any type. We can also understand it as that no matter what type we store, it will be converted to Object type.
class MyArray{ public Object[] array=new Object[10]; public void setVal(int pos,Object val){ this.array[pos]=val; } } public class TestDemo { public static void main(String[] args) { MyArray myArray=new MyArray(); myArray.setVal(0,10); myArray.setVal(1,"hello"); myArray.setVal(2,3.14); } }
Because the stored data is converted to Object type, we should also use a variable of Object type to receive when reading, otherwise it will be forcibly converted to the corresponding data type
class MyArray{ public Object[] array=new Object[10]; public Object getVal(int pos){ return this.array[pos]; } public void setVal(int pos,Object val){ this.array[pos]=val; } } public class TestDemo { public static void main(String[] args) { MyArray myArray=new MyArray(); myArray.setVal(0,10); myArray.setVal(1,"hello"); myArray.setVal(2,3.14); Object ret0=(int)myArray.getVal(0); Object ret=String.valueOf(myArray.getVal(1)); Object ret1=Double.parseDouble(myArray.getVal(2).toString()); System.out.println(ret0); System.out.println(ret); System.out.println(ret1); } }
It can be written like this or below
class MyArray{ public Object[] array=new Object[10]; public Object getVal(int pos){ return this.array[pos]; } public void setVal(int pos,Object val){ this.array[pos]=val; } } public class TestDemo { public static void main(String[] args) { MyArray myArray=new MyArray(); myArray.setVal(0,10); myArray.setVal(1,"hello"); myArray.setVal(2,3.14); int ret0=(int)myArray.getVal(0); String ret=String.valueOf(myArray.getVal(1)); Double ret1=Double.parseDouble(myArray.getVal(2).toString()); System.out.println(ret0); System.out.println(ret); System.out.println(ret1); } }
The output result is:
For the concept of genericity, we can give two examples in life to help us understand it
For example, when we were young, we learned a text called painting carambola,
Carambola will not change, but our observation angles are different. From different angles, we can get different shapes. Isn't this the same as the generic type we learned? The data is the same (all Object type), but we can convert it to the type we want when reading. The data will not change. The difference is what type we use to receive it.
For another example, when we praise a thing or a person who is very powerful, we will use "666", so "666" has the meaning of describing someone or something who is very powerful and impressive. However, in the west, "666" is the symbol of devil, so we can think that "666" was originally just a string of numbers, It has no meaning in itself, but people in different places have given it different meanings.