Basic Data Types in Java, Java Data Types

Source: Internet
Author: User

Basic Data Types in Java, Java Data Types
What is a basic data type?

We areData types that are frequently used during programming, Such as integer and floating-point data types, which are specially treated and imagined as "basic types ".

Basic Data Types in Java

JAVA basic type features
  • The basic data type speed is faster than the corresponding package type; // an article on the Internet has been tested to prove that it is faster than the package type. If you are interested, refer
  • The basic data type is not an object. // some people prefer this to refute that everything in Java is an object;
  • High portability. The size of the space occupied by the Basic Java types is fixed and will not change with the hardware architecture of the machine. // the size of the int space like the C language is not fixed.
  • There is no concept of unsigned number. Here it mainly refers to byte, short, int ..., in fact, char can be considered as the number of unsigned characters; // For example, the C language contains unsigned int, unsigned char, and other unsigned numbers.
  • They all have their corresponding packaging classes, which can be automatically converted between the basic type and the packaging class; // Because JAVA SE5 introduces the Automatic Disassembly box function
  • To be supplemented...
JAVA basic data type description boolean

Java does not explicitly define the size of the space occupied by the boolean type, but it can only be set to true or false. The default value of initialization is false;

The encapsulation class corresponding to the boolean type is Boolean;

Char

The character type occupies two bytes of space, that is, 16 characters. The characters in Java are stored in Unicode, and both Chinese and English characters can be stored in a char, as shown in the following example:

Character a is converted into an integer. The value is expressed in hexadecimal notation as 6211. The value is the serial number of the character in the unicode character set. The Unicode format of character a is '\ u6211'

Package tmp; public class Main {public static void main (String [] args) {char a = 'I'; System. out. println (Integer. toHexString (a) + '\ u6211 ');}}

The initial default value of char is 'u0000', and the value range is '\ u000000' --' \ uffff'. Its encapsulation class is Character.

Byte

Integer: the size of one byte. The initial default value is 0.

Short

Integer: the size of two bytes. The default value is 0.

Int

Integer, four bytes in size. The initial default value is 0.

Long

Integer, eight bytes in size. The initial default value is 0l.

Float

Float Type, four bytes in size, range: 32-bit IEEEE 754 single precision range, initial default value: 0.f.

Double

Float Type, eight bytes in size, range: 64-bit IEEEE 754 single precision range, initial default value: 0.0.

Void

Null type. It is mainly used before the method name, indicating that the method has no return value;

About automatic packing and unpacking

This function is introduced in JAVA SE5. In short, it is the automatic conversion between the basic type and the packaging class.

For example, int packing and Integer unpacking actually call the following methods in the Integer class:

Packing: valueOf (int I)

Unboxing: intValue ()

If you are interested, you can initiate a breakpoint test in the source code;

For example:

Package tmp; public class Main {public static void main (String [] args) {// automatic packing, actually calls Integer. valueOf (int); Integer i1 = 100; // automatically unpack. The intValue () method of the Integer object is actually called; int i2 = new Integer (100 );}}

References:

JAVA programming ideas

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.