Java Basic data types

Source: Internet
Author: User

Tag: arithmetic bool integer main AOT equals print static about

Java has 8 data types:

Data type Size Range of representations
Boolean 1 bytes or 4 bytes 0 or 1
Byte 1 bytes -128~127
Short 2 bytes -32768~32767
Char 2 bytes Unicode0~unicode2^16-1
Int 4 bytes -2^31~2^31-1
Long 8 bytes -2^63~2^63-1
Float 4 bytes
Double 8 bytes

Questions about the range and accuracy of float and double can be see in this article and in this article

Why can the length of a Boolean be either 1 bytes or 4 bytes?

The source is the description in the Java Virtual Machine specification: "Although the data type of Boolean is defined, it provides very limited support." In a Java virtual machine, there are no bytecode directives dedicated to Boolean values, and the Boolean values that are manipulated by the Java language expression are replaced with the int data type from the Java Virtual machine after compilation. The Boolean array is encoded as a byte array of the Java virtual machine, with each element having a Boolean element of 8 bits. In this way we can conclude that the Boolean type takes up a single use of 4 bytes and is 1 bytes in the array.

Conversion of data types:

8 data types, we can divide them into 3 large categories: integers, floating-point numbers, and Boolean values.

There are 5 integers, all of which are encoded in the complement of a byte, short, char, int, long five types.

Floating-point numbers are available in two types, float and double.

The Boolean value is only a Boolean.

The conversion rules between the same big categories are actually simple. A small range can be automatically converted to a large range. The size of the range is definitely related to the number of bits of the data type. The smallest range is definitely byte, and the largest range is long. So converting byte to long is not a problem at all. Can be directly connected by an equal sign, also known as implicit conversion. If you want to convert a large range to a small range, you must turn strongly.

Here's a code example:

 Public classmain{ Public Static voidMain (string[] args) {byteB1 = 100;  ShortS1= 200; intI1 = 300; LongL1 = 400L; Charch = ' A '; I1=B1; B1= (byte) I1; L1=I1; I1= (int) L1; S1= ( Short) ch; CH= (Char) S1; I1=ch; CH= (Char) (i1+1); System.out.println (i1);//OutputSYSTEM.OUT.PRINTLN (CH);//Output BSystem.out.println (ch+0);//Output 98    }}

int is the Java default Operation data type, and when the long type is defined, the data is appended with L.

Because Java operations are of type int, so:

Char a= ' a ';

Char b= a+1;

Compile error, must write this: char b= (char) (a+1);

 Public classmain{ Public Static voidMain (string[] args) {intI1 = 300; LongL1 = 400L; floatF1 = 23.7f; DoubleD1 = 28.99; F1=I1; I1= (int) F1; F1=L1; L1= (Long) F1; }}

Although both float and int are 4 bytes, float represents a larger and wider range, so int to float can, float to int must be strong, even long to flaot are strong.

I wonder if you have any questions like that?

int i=1;

Flaot f1=1f;

float F2 = 1.0f;

Are the three numbers equal?

 Public classmain{ Public Static voidMain (string[] args) {intI=1; LongL = 1; floatf1=1f; floatF2 = 1.0f; DoubleD1 = 1; DoubleD2 = 1.0; //The following output is all trueSystem.out.println (i==F1); System.out.println (i==F2); System.out.println (L==F1); System.out.println (L==F2); System.out.println (F1==F2); System.out.println (i==D1); System.out.println (i==D2); System.out.println (F1==D1); System.out.println (F1==D2); }}

Add one point:

 Public class main{    publicstaticvoid  Main (string[] args) {        String s= "1" ;         char ch = ' 1 ';        System.out.println (S.equals (CH));         // The following statement reports an exception and cannot compare objects and base data types with = =        System.out.println (s==ch);}    }

Char and string are not comparable and cannot be equal. When we go to the Equals () method source code of String, the method's arguments are object instanceof string.

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.