Eight basic data types in JAVA __java

Source: Internet
Author: User

They were byte, short, int, long, float, double, char, Boolean


Integral type

Where byte, short, int, and long all represent integers, except that they have different ranges of values.

Byte value range is -128~127, occupies 1 bytes (-2 of 7 times to 2 of 7 Times Square-1)

The short value range is -32768~32767, occupies 2 bytes (-2 of 15 times to 2 of 15 Times Square-1)

int has a value range of ( -2147483648~2147483647), occupies 4 bytes (-2 of 31 times to 2 of 31 times-1)

Long takes a range of ( -9223372036854774808~9223372036854774807), takes up 8 bytes (-2 of 63 to 2 of 63 times-1)

You can see that the range of byte and short values is small, and long is too large, takes up more space, and basically an int satisfies our daily calculations, and int is the most integral type to use.

In general, if there is an integer number such as 35 in Java, then the number is int, and if we want it to be byte, we can add a b:35b to the data, indicating that it is byte, and that the same 35S represents the short type, 35L represents a long type, int we can add nothing, but if you want to represent a long type, you must add "L" to the data.

Why is 2 of the 7 times, because it is 2 eight times, but the front 1 is the minus sign. After the 2 of the 63-time side,


Floating-point type

float and double are data types that represent floating-point type, and the difference between them is that their accuracy is different

Float 3.402823e+38 ~ 1.401298e-45 (e+38 is multiplied by 38 times 10, likewise, e-45 is multiplied by 10 minus 45) to occupy 4 bytes

Double 1.797693e+308~ 4.9000000e-324 occupies 8 bytes

The double type is larger and more accurate than the float type, so the usual floating-point data is double with no declaration, and if you want to indicate that a data is float, you can add "F" behind the data.

Floating-point data is not exactly accurate, so it's normal to have a float at the end of the decimal point at the time of calculation.


Boolean (Boolean type)

This type has only two values, True and False (true and not true)

Boolean t = true;

Boolean f = false;

Char type (text type)

The data type used to hold the character, occupies 2 bytes, is Unicode encoded, and its first 128-byte encoding is compatible with ASCII

Characters are stored in \u0000~\uffff, and when defining character data, be aware that ", for example, ' 1 ' means the character ' 1 ' instead of the value 1,

char c = ' 1 ';

We try to output C look, System.out.println (c), the result is 1, and if we do this output System.out.println (c+0);

It turned out to be 49.

If we define C like this, look at

char c = ' \u0031 '; The result of the output is still 1, because the character ' 1 ' corresponds to the Unicode encoding is the \u0031

char c1 = ' h ', c2 = ' e ', c3= ' l ', c4= ' l ', c5 = ' o ';

System.out.print (C1); System.out.print (C2); System.out.print (C3); System.out.print (C4); Sytem.out.print (C5);


String

We've seen this in the previous definition:

String s = "Hello";

System.out.println (s); the effect is the same as the 5 statements above, so what is a string? String is a string, it is not a basic data type, it is a class.

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.