Basic Data Types

Source: Internet
Author: User

Basic Data Types

What is the data type? The data type is the definition of data. Each data type will allocate a certain amount of memory space, which is usually
When variables are used together, variables can change in a certain range.
I. Ask questions
Below is a company interview question
byte b1 = 3,b2 = 4,b; b = b1 + b2; b = 3 + 4;

The problem is where the program will go wrong. Why?
The execution result of this program is as follows:
An incompatible type problem occurs in the third line of the Code.
Ii. Problem Analysis
In java, the data type is byte, 1 byte; int (default type); 4 bytes
When declaring variables, b1, b2, and B are of the byte type, and the initial values of b1 and b2 are 3, 4, respectively.
1. Because the default data type in java is int, the sum is automatically converted to int type when b1 + b2 is executed, that is, four bytes,
ThenIn B, because the byte type has only one byte length, the conversion from the int type to the byte type requires three bytes, so
Current data may cause loss errors;
2. This problem may also occur in the third row, but no error is reported because constants are completed in advance During computation.
The reason is as follows:
After writing the code, we need to compile the program first and then run the program. Constant computation is automatically executed during compilation, and
The results have been calculated, so there will be no data loss errors during running. For variables, because the data is not necessarily
When computing is executed, we will find that the data types before and after compilation are inconsistent, and then we will be prompted
It is not difficult to solve this problem. You only need to forcibly convert b1 + b2 to the byte type.
Iii. Summary
The data types in java are automatically compatible with the upstream. If all data types are declared as long, no errors will occur.
1. byte, short, char-int-long-float-double conversion from small to large
2. byte, short, and char do not convert each other
I was not so concerned about some things with special details before. problems may occur when they are actually used.

 

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.