Value types in Java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.