Well-Off with you to learn JAVA -------- overflow occurs, well-off java --------

Source: Internet
Author: User

Well-Off with you to learn JAVA -------- overflow occurs, well-off java --------

This article describes a kind of problems that may occur during programming in JAVA-the occurrence of overflow.

First, let's take a look at the special value code of the JAVA integer constant:

01 // If the integer value exceeds the maximum value that can be expressed as the value range, overflow will occur.

02 public class TestJava3_4

03 {

04 public static void main (String args [])

05 {

06 int x = java. lang. Integer. MAX_VALUE; // obtain the maximum Integer value.

07

08 System. out. println ("x =" + x );

09 System. out. println ("x + 1 =" + (x + 1 ));

10 System. out. println ("x + 2 =" + (x + 2 ));

11}

12}

Output result:

X = 2147483647

X + 1 =-2147483648

X + 2 =-2147483647

When the maximum value is 1, the result is the smallest value in the range. When the maximum value is 2, the result is the small value in the range, which is the data type overflow. Readers can find that there will be a loop in this situation. To avoid this situation, you must add the value range check function in the program, or use a large data type that indicates a range, such as a long integer.

When an integer I is declared, the range is-2147483648 ~ Between 2147483647, when the value of I is set to the maximum value of 2147483647, it is still in the range of integers, but when x is added to 1 or 2, instead, the value of integer x is changed to-2147483648 and-2147483647, which is the minimum and minimum value that can represent the range.

The above situation is similar to the case where the counter automatically returns to zero when its content reaches the maximum value. In an integer, the minimum value is-2147483648. Therefore, when the value of integer x is the largest, adding 1 will change to the minimum value-2147483648, that is, overflow. See Figure 3-3 to learn about data type overflow.

To avoid int overflow, you can add an uppercase "L" or

Is to add long before the variable, as the forced type conversion. Take TestJava3_5 as an example.

To prevent overflow:

01 // when the integer overflow occurs in the following program, use the forced type for conversion.

02 public class TestJava3_5

03 {

04 public static void main (String args [])

05 {

06 int x = java. lang. Integer. MAX_VALUE;

06

08 System. out. println ("x =" + x );

09 System. out. println ("x + 1 =" + (x + 1 ));

10 System. out. println ("x + 2 =" + (x + 2L ));

11 System. out. println ("x + 3 =" + (long) x + 3 ));

12}

13}

Output result:

X = 2147483647

X + 1 =-2147483648

X + 2 = 2147483649

X + 3 = 2147483650

The younger brother is learning java. Now he has encountered a problem: Stack Overflow of quick sorting. I hope you can help me,

I helped you tune it for a long time and didn't see the specific error.
However, I can see from your rough algorithm that the two sides are compared with the key numbers at the same time, and after the comparison is successful, they are still exchanged, instead of changing the location (this is probably the cause of an error ). I think it may be that the exchange process on both sides creates an infinite loop.

In fact, we only need to compare the number from one direction to the key number. If a number satisfies the moving condition, it should be taken out at the head or end of the team, rather than exchanged. For example, you can view the first number and make the following judgment for each number:
1. if it is the last number, a comparison is completed;
2. If it is equal to a key number, skip it;
3. If it is smaller than the key number and on the left side of the Key number, skip it;
4. if it is smaller than the key number and on the right side, it is taken out of the queue and placed in the first place of the queue. (Note that it is removed to the first place of the queue, instead of exchanging with an element, your algorithm may be faulty)
5. If it is larger than the key number and on the left, it will be taken out of the queue and placed at the end of the team;
6. If it is larger than the key number and on the right side, skip it.

You can change the part of the exchange to the first and put it at the head or end of the team. It should be okay.

Learning JAVA for a long time

In my experience, since you have determined your goals in your life, you should devote yourself to them so that you can get everything. I learned C ++ first and then java. If you have been working in the company for almost three months, you need to work very hard. If you go to the training class, there is no pressure for you to learn well; the following is my reference for another friend in this regard:
If you really like JAVA, don't go to the training class. I also came out from the training class. Here I will talk about my feelings: I only learned a bit about it, I feel that I have not learned much in the training class within 10 days! When I first arrived at the company, the boss gave me a project for me to do. In order to seize this opportunity, I did not bring my sleep for the first 10 days, and the hair written in the program was straight, I learned a lot at the end. Now I am an NB programmer! If you go to the training class: I will not say that the tuition fee is very expensive (most of them are more than 10 thousand). If you are not good at learning, you cannot guarantee it. Let us think that since you want to learn JAVA, you should have understood computers before, and those who know computers should have understood how to play games. At that time, you should be unable to control yourself. After five or six months, the teacher did not dare to worry about you, even if you don't listen, it's very difficult to get a job when you graduate. There are several people in our class who are not in employment, because they don't take classes to play games. I recommend that you have an internship at the company. Why? A company is a company. It is a project. Every day, there is a necessary workload. There are rules that limit you. If you come in as a company employee, you should create benefits for the company and never be late, you are not allowed to leave early. Why do new college students need to go to the company for an internship? This is because the company has learned the true skills. If you are at school, it is very likely that you will ....
I am now outsourcing a software company in Beijing. Due to the current economic crisis, the company's performance is a little poor, and most programmers in the company are a little idle (the boss thinks it is idle and wants to squeeze our labor,) The boss wants to hire six interns and use our labor force to make money for him. My boss is the boss. I admire him very much. He is abnormal. He worked 12 hours a day. He was the first to come back. He has been working for eight years, I have saved two bookcases and I know almost everything about the program. JAVA is the best. I admire him very much. When can I reach his level! He encouraged me to stick to writing programs when I had difficulties and wanted to give up! I remember that I lived in the fairy tale world before my work, and now I have really tasted the hardships of my life! It is time to work hard for your own aspirations. I hope you will do the same. If you do not have the hard work to learn the true skills, you should go to a company for an internship, I don't have much money, so I am an apprentice!
If you want to know me this company, you do not have programming experience can also, direct someone to take you, I contact: d_aries@163.com, my company in Zhongguancun Software Park 10th Street, together with Lenovo's headquarters, coga, as a programmer, I actually wrote so many Chinese characters. It's really ugly, and the sentence is not fluent yet. It's really sweaty! I don't want you to go on the road. Most of the training courses are deceptive!

Related Keywords:
Related Article

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.