21st definition of Java data types, and automatic conversion and casting of underlying types

Source: Internet
Author: User

Hello everyone, today I am I learned some of the knowledge points to share to everyone, I hope that after reading, if there is anything you do not understand, please feel free to contact me, if you feel that I write is not very good, but also hope you can understand, because I just learned to write blog, many formats and methods are not very perfect, I hope that we all forgive, good nonsense will not say more, to talk about today I learned some of the knowledge point it;

Today I mainly learned some Java data types, in which the data types in Java are divided into two classes------basic data classes and logical data classes, the following I described are some of the basic data class classification, as for the logic class, because of the use of too few, so today is no longer here to do more introduction.

OK, the basic data class contains 8 kinds of data types, from small to large arrangement is, byte/shot/int/long/float/double/and some single-character char/boolear a total of eight;

As for the usage of these eight commonly used data types, I believe that knowing a little bit about Java fundamentals knows how to use it, and here I don't introduce you to everyone.

Regression topic, what is the automatic conversion and casting of the underlying type??? At the beginning of the contact I did not understand what is the same thing, but in the literal sense is not simply the different types of values to convert it, I was the first to think of it, but then I found that I was wrong, and wrong, because it contains a lot of knowledge, which is what I said the automatic conversion and forced conversion;

Now to think about the second question, why the cast, there may be many programmers think that i+=1 is only i=i+1 shorthand, but in fact, they are a simple assignment operation, a use of compound assignment operation, The biggest difference between a simple assignment operation and a compound assignment operation is that the compound assignment operator automatically transforms the result of the operation into the type of its left operand. Look at the following two ways of writing, and you'll know where the difference is:
(1) byte i=5;
I+=1;
(2) byte i=5;
i=i+1;
The first form of compilation is no problem, but the second one is compiled to pass. The reason is that when using the compound assignment operator, even if the result on the right is the int type, the system will convert its value to the left byte type, and without such a benefit when using the simple assignment operation, the system will assume that assigning the value of i+1 to I is to assign the int type to byte. Therefore, a cast is required. After understanding this, let's look at an example:
BYTE b=120;
b+=20;
System.out.println ("b=" +b);
In this case, you should understand that the value of output B in the example above is not 140, but-116. Because the value of 120+20 is already beyond the range of a byte representation, when we use the compound assignment operation, the system automatically makes the type conversion, converting the 140 strong to byte, so the result is-116. This shows that you have to be careful when using the compound assignment operator, because this type conversion is done unconsciously, so the resulting result may not be the same as what you expected.

Well, because of the time, today's knowledge points to the end of this, and the daily plans and goals have to remind themselves again.

1, daily exercise fingering an hour;

2, every day to write a blog or log;

3, every day class 15 minutes before entering the classroom;

4, every day to give themselves a word of encouragement------learning knowledge is only a stepping stone, can let you ahead of others to the company, but when you go in, you will find that everyone in the knowledge of the amount of is not much, so say in the future life on the road, or to see you learn how to behave.




21st definition of Java data types, and automatic conversion and casting of underlying 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.