When it comes to understanding Java, it is worthwhile to know what value types are in Java, and how to determine whether a type is a value type or a reference type, based on the experience of C #.
In C #, value types are stored in the stack, do not require garbage collection, reference types are stored in the heap, and garbage collection is required. Converting a value type to a reference type can cause boxing (creating a reference type that requires garbage collection), and when it is actually used, it needs to be disassembled, and objects that need to be garbage collected will be created when the
If there are too many unboxing, the performance of our program has a large impact, so it is necessary to understand what the value types are, and which are reference types, so that it is convenient to distinguish in development.
Reading is a way, and exploring yourself is also a way.
What are the value types in Java? In the Chinese version of the <java Programming idea > 4th edition of Page 23, the basic types of Java are listed: Boolean,char, Byte, Short,int, Long, float,double, void. They are also listed for their packing period. (Capital Letter) <
(Java seems to be different from C #, the basic type of Java should be placed in the stack, <java programming Ideas > Chinese Fourth edition 25 pages: Java objects do not have the same life cycle as the basic type)
Based on the experience of C #, I would like to find a similar is,as in C # to determine if the value type is better.
In C #, it can be judged as follows:
Type T = typeof (T);
T.issubclassof (typeof (ValueType))
Is there a similar approach in Java?
I wrote one myself:
Public final class Hitypehelper {
// ...
public static Boolean Isnumer (Class<?> t) {
if (t = = null) {
return false;
}
if (t = = Object.class) {
return false;
}
if (t = = Void.class) {
return false;
}
Class<?> superclass = T.getsuperclass ();
if (superclass = = null) {
return true;
}
return superclass = = Number.class;
}
// ...
}
The results of the validation are as follows:
@Test
public void Test_valuetype () {
Class<?> up = Number.class.getSuperclass ();
Assert.assertnotnull (UP);
up = Integer.class.getSuperclass ();
Assert.assertnotnull (UP);
up = String.class.getSuperclass ();
Assert.assertnotnull (UP);
up = Float.class.getSuperclass ();
Assert.assertnotnull (UP);
up = Char.class.getSuperclass ();
Assert.assertnull (UP);
up = Int.class.getSuperclass ();
Assert.assertnull (UP);
up = Float.class.getSuperclass ();
Assert.assertnull (UP);
up = EnumTest.class.getSuperclass ();
Assert.assertnotnull (UP);
up = Enum.class.getSuperclass ();
Assert.assertnotnull (UP);
up = Object.class.getSuperclass ();
Assert.assertnull (UP);
}
Value types in Java