Java traps have you noticed?

Source: Internet
Author: User

I watched a video from Beijing wind network and summarized several typical Java traps for everyone.

The answer is hidden. CTRL + A is displayed. We recommend that you first think about the results and then run the code test. Maybe you will suddenly realize.

1. Search for odd numbers:

Public static Boolean isodd (int I) {<br/> return I % 2 = 1; <br/>} 

Can the above method find all the odd numbers?

A: The negative number is not taken into account. If the parameter is a negative number, the result will never be obtained! It should be: return I % 2! = 0;

2. Floating Point Number Subtraction

System. Out. println (2.0-1.9 );

Will 0.1 be printed on it?

A: No. You will know the result after your test. Correct practice: Use decimal.

3. Exchange

Int x = 2010; <br/> int y = 2012; <br/> x ^ = y ^ = x ^ = y; <br/> system. out. println ("x =" + x + "; y =" + y );

Is the value of X and Y called?

A: No. The Java operation sequence is left to right. It should be written as follows: Y = (x ^ = (y ^ = x) ^ y;

4. characters and strings

System. Out. println ("H" + "A"); <br/> system. Out. println ('H' + 'A ');

Are the output results of the above two statements the same?

A: Do not want to be the same. The characters will be converted to numbers. Therefore, the first sentence is output: Ha, and the second sentence is the number of the asⅱ code of the two characters.

5. Infinite Loops

Public static final int end = integer. max_value; <br/> Public static final int start = end-100; <br/> Public static void main (string [] ARGs) {<br/> int COUNT = 0; <br/> for (INT I = start; I <= end; I ++) <br/> count ++; <br/> system. out. println (count); <br/>}

What is the result of the above program running?

A: Infinite loops. Change I <= end to I <End? Why? You know, huh, huh!

6. counter problems

Int minutes = 0; <br/> for (int ms = 0; MS <60*60*1000; Ms ++) <br/> If (MS % 60*1000 = 0) <br/> minutes ++; <br/> system. out. println (minutes );

Is the result the same as you think?

A: Well, I don't want to talk about the brackets!

7. What is returned?

Public static Boolean demo-() {<br/> try {<br/> return true; <br/>}finally {<br/> return false; <br/>}< br/>} 

True? False?

A: Generally, in a try/catch code block, finally is always executed.

8. Aggregate traversal in errors

Public static void main (string [] ARGs) {<br/> vector v = new vector (); <br/> v. add ("one"); <br/> v. add ("two"); <br/> v. add ("three"); <br/> v. add ("four"); <br/> enumeration enume = v. elements (); <br/> while (enume. hasmoreelements () {<br/> string S = (string) enume. nextelement (); <br/> If (S. equals ("two") <br/> v. remove ("two"); <br/> else {<br/> system. out. println (s); <br/>}< br/> system. out. println ("what's really there... "); <br/> enume = v. elements (); <br/> while (enume. hasmoreelements () {<br/> string S = (string) enume. nextelement (); <br/> system. out. println (s); <br/>}< br/>}

Run the code to see if the result is the same as what you think?

A: It is generally not recommended to perform clustering operations during clustering traversal. Why is the result like this? Check the JDK source code to get the answer. Enumeration does not implement the Fail fast operation. If you change to arraylist, the above Code may fail. The iterator introduces Java and pattern.

 

Related Article

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.