Preface & amp; precision loss in java & amp; + = mandatory conversion of statements

Source: Internet
Author: User

Preface & precision loss in java & + = mandatory conversion of statements. Preface how to write

1. When you first enter the park, write a blog

  About a month ago, I got an internship at Hainan Telecom Planning and Design Institute. As a communication student, it was a great opportunity to enter the Design Institute for an internship, however, two weeks after I entered the Design Institute for practice, I found that the design institute's work is basically simple and boring mechanical work. During the internship, we just took the planning design drawing of our predecessors and made the corresponding design planning book. The design planning book has a ready-made template. Our internship is to modify the template according to the design drawing, after the modification, a design plan is completed. My work is simple and boring, not just my internship. I also have a vision for my communication industry plan. After my internship, even if I became an excellent telecommunications planning designer, this is just a planning designer who places equipment in accordance with the telecommunications planning principles. In my opinion, there is basically no innovation in the Communication Industry (except for participation in R & D of new technologies in the Communication Field). There is no innovation or challenge. Simple and boring mechanical work is not suitable for young people, therefore, I chose to resign, give up the stable work of state-owned enterprises, and give up such a job as a zombie.

After I resign, I want to start my career as a programmer. After all, I personally feel very fond of programming. In my opinion, this is a challenging career. Last week, I received a telephone interview from a software company under CEN. After the interview, I knew that as a non-software engineering student, I still had many shortcomings in software development, so I started the basic learning of software development again. In addition, I also applied for a blog yesterday to record my current learning experience and problems and solutions for my future work.

 

2. Precision loss of data types in java

I found the java teaching video of Miss Bi Xiangdong on the Internet and started the course of self-study. During the course of the previous two days, I thought I should pay attention to it and write it here, A little help to me and friends passing by in the future.

Error about overflow of data type range value in java:

1 class Demo2 2 {3 public static void main (String [] args) 4 {5 byte a, B, c; 6/* assign the int type to the byte type, the byte type can be 1 byte in length and the value range is-128 ~ 127 */7 a = 5; 8 B = 128; // if the number exceeds the value range of the data type, an error 9 c = a + B is returned; // if the value of unknown size is assigned to byte type, an error of 10 System is returned. out. println (c); 11 12 int x, y, z; 13/* int is the default Integer type in java. The length is four bytes and the value range is-2147483648 ~ 2147483647 x/14 x = 154; 15 y = Integer. MAX_VALUE + 1; // assign the maximum value of int to y plus 1 out of the range, overflow, but no error 16 z = x + y; // assigning the value of unknown size to int type does not return 17 System. out. println (z); 18} 19}

Running result:

No error is reported for int type. The running result is-2147483494 (error result)

 Program code analysis:

Byte type: 1) if a value is assigned to a value out of the range, an error will be reported for loss of precision (Data overflow)

2) If you assign a value to an unknown number of values (that is, a variable), an error will be reported (Data overflow)

Int type: 1) the loss of precision (Data overflow) will not be reported if the value is assigned to a value out of the range)

2) No loss of precision (Data overflow) will be reported when you assign values to an unknown number of values (that is, a variable)

Summary:

In programming, you can use forced conversion to assign values to different types of data, but there may be loss of precision, which makes it difficult to find Bugs caused by such problems. Pay attention to this during programming, especially int type data, which is the default java data type, is not reported by the compiler during compilation. It is difficult to find the bug of precision loss, which deserves special attention.

 

3. Differences between ++ = and = (+) in java

In java, the + = Operator is used. Beginners may think that the + = Operator is equivalent to the = (+) operator. In fact, this is not the case.

1 class Demo2 2 {3 public static void main (String [] args) 4 {5 byte a; 6 a = 5; 7 a + = 2; // compiled successfully, no error 8 System. out. println (a); 9 10 byte B; 11 B = 5; 12 B = B + 2; // the error message "loss of precision 13 System" is returned when compilation fails. out. println (B); 14 15} 16}

Running result:

 Program code analysis:

+ = The result on the right is forcibly converted, and then assigned to the left.

The = (+) statement does not force type conversion, so the error precision is lost when compiling.

 Summary:

The + = Statement is equivalent to the = (forced type conversion) (+) statement.

Due to forced type conversion, you must pay attention to the loss of precision.

 

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.