Java Basic data types

Source: Internet
Author: User

1. Data type classification


2. Basic data types


3. Integer data type

A. Basic Concepts

Int x=0;

Allocate 4 bytes of memory, one byte to 8 bits, value range 2 31 to 2 31

bytex=-12;

Allocate one byte, machine code

shortx=12;

Allocate 2 bytes, emphasizing type, using method (short) 12;

The long type is denoted by the suffix l

Long width=12l;

Allocate 8 bytes to 64 bits

B. Data overflow

public class Demo1 {
public static void Main (string[] args) {
int max=integer.max_value;
System.out.println ("Shaping Max:" +max);
System.out.println ("max+1:" + (max+1));
System.out.println ("max+2:" + (max+2));
}
}

Results:

Plastic max:2147483647
The max+1:-2147483648//value becomes the smallest
max+2:-2147483647//value becomes sub-smallest

Workaround:

int max=integer.max_value;
System.out.println ("Shaping 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 this is a Long data type overflow, it cannot be resolved.

4. Character types

Constant: ' 123 ' A '! ' Hello

General use of Char declarations

Code:

Converting between characters and shaping
Char ch1= ' a ';
Char ch2=97;
System.out.println (ch1+ "---" +ch2);

Commonly used escape characters:


Code:

Char chinaword=' good ',japanword='? ';

Char you =' \u4f60 ';

int position= 20320;

System. out. Print (" Kanji:" +chinaword+ " position:"+ (int) Chinaword);

5. Floating-point types

Float type

Must have F or F in the back, usually 8-bit valid data reserved

Allocate 4 bytes

Double type

Suffix d or D after, 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 values for data types


Role: Sometimes there are variables in the Java program that are not assigned, which makes sense, and defines the default values to prevent this from happening.

Copyright Notice: Bo Master original articles, reproduced please indicate the source. Http://blog.csdn.net/dzy21

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.