Java Basic data types

Source: Internet
Author: User

Overview

The eight basic data types of Java are Byte,short,int,long,float,double,char,boolean, each corresponding to their respective packages. Why does Java keep basic data types? Because basic data types are commonly used and stored in stacks, with less space, better performance than objects, and a default value for the base data type, the default value of the object is null. Why should there be a class object corresponding to the basic data type? Because Java is an object-oriented language, the existence of basic data types is due to the advantages of performance, and the corresponding wrapper class is designed to conform to object-oriented development, especially after the automatic packing and unpacking operation has been implemented.

Get the base data type
    • Get the Byte.class type by Class.getprimitiveclass ("byte")

    • Byte.class==byte.class return False

    • Class.getprimitiveclass ("byte") ==byte.class returns True

Packing Box Unpacking

The code is as follows

Packingandunboxing() {        int a=1; Integer b=2; a=b;}   

The execution of the Javap-c Classname.class came out as

Parse byte code

    • Row 0-1 is the parsing of int a=1, which assigns a value to the basic data type

    • Row 2-3 is the resolution of the integer b=2, note that there is a integer.valueof (), which means that this sentence is correctly executed by the integer b=integer.valueof (2), which needs to be verified at the ValueOf method break point debugging

    • Row 6-8 is the execution a=b the code actually executed here is A=b.intvalue ()

Java's automatic boxing unboxing is handled by the compiler.

The base type corresponds to the cache of the encapsulated class

In some encapsulation classes, when the inner class is defined as the cache is instantiated without going to new and fetching the data directly from the cache, we can look at a piece of code like this

Integer a=1;  Integer b=1;a=b?  

The answer is a=b, even if a, B are objects, we have said that the integer a=1 is actually an integer a=integer.valueof (1), the method is as follows

ValueOf(new Integer (i);} 

That is, if our value is between the cache minimum and the maximum, then the values in the cache are used directly, and the same cache used by new,a,b is not naturally equal.

So what types of packages have a cache?

    • The byte cache range is 128 to 127,

    • Short cache range-128 to 127

    • Integer default cache range 128 to 127, can be set to maximum value, such as: Add-djava.lang.integer.integercache.high=1000 to the VM, the maximum value is 1000

    • Long cache range-128 to 127

    • Character Cache Range 0-127

Character Range 0-127 is the cache of commonly used ASCII characters, ranging from 128 to 127 for one byte, so the cache is almost also this range. For details, see the format of the computer's stored data

Regardless of whether there is a cache, the data created using the New keyword will not be used in the cache, such as Integer a=1 and integer b=new integer (1)

Welcome to join the Learning Exchange Group 569772982, we learn to communicate together.

Java Basic data types

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.