Java basic data type, java Data Type

Source: Internet
Author: User

Java basic data type, java Data Type

1. Data Type Classification


2. Basic Data Types


3. Integer Data Type

A. Basic Concepts

Int x = 0;

4 bytes of memory are allocated. one byte occupies 8 bits. The value ranges from-2 to the power 31 to the power 1 of 2.

Bytex =-12;

Allocate one byte, machine Encoding

Shortx = 12;

Allocate 2 bytes. When the type is emphasized, use method (short) 12;

The Long type is indicated by the suffix L.

Long width = 12L;

Allocate 8 bytes to 64-bit

B. Data Overflow

Public class Demo1 {
Public static void main (String [] args ){
Int max = Integer. MAX_VALUE;
System. out. println ("integer max:" + max );
System. out. println ("max + 1:" + (max + 1 ));
System. out. println ("max + 2:" + (max + 2 ));
}
}

Result:

Integer max: 2147483647
Max + 1:-2147483648 // The value is minimized.
Max + 2:-2147483647 // the minimum value

Solution:

Int max = Integer. MAX_VALUE;
System. out. println ("integer max:" + max );
System. out. println ("max + 1:" + (max + 1 ));
System. out. println ("max + 2:" + (max + 2L ));
System. out. println ("max + 2:" + (long) max + 2 ));

If the LONG data type overflows, the problem cannot be solved.

4. Character Type

Constant: '000000' 'A ''! ''Hi'

Generally, char declaration is used.

Code:

// Conversion between character and integer
Char character = 'a ';
Char ch2 = 97;
System. out. println (Bytes + "---" + ch2 );

Common escape characters:


Code:

CharChinaWord = 'ha ',Japan= 'Signature ';

Char You= '\ U4f60 ';

Int Position= 20320;

System.Out. Print ("Chinese Character:" + chinaWord + "Location:" + (Int) ChinaWord );

5. Floating Point Type

Float Type

F or F must be followed. Generally, 8-bit valid data is retained.

Allocate 4 bytes

Double Type

The suffix d or D can be omitted.

Allocate 8 bytes

Float num = 3.0f;
System. out. println ("num * num =" + num * num );

6. boolean type

Constant: true, false

Variable: bool on = true;

Code:

Boolean flag = true;
System. out. println (flag );
System. out. println (! Flag );

7. Default Value of Data Type


Purpose: Sometimes variables with no value assignment exist in java programs, which makes sense useless and defines default values to avoid this situation.

Copyright statement: original post of the blogger. For details, refer to the source. Http://blog.csdn.net/dzy21

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.