The ubiquitous bug of "Java FAQ"

Source: Internet
Author: User


The code is as follows:

Public class example023 {public static void main (String[] args)  { Errormethod (); Rightmethod ();} Private static void errormethod ()  {StringBuffer word = null; Random rnd = new random ();switch  (Rnd.nextint (2))  {case 1:word =  New stringbuffer (' P '); Case 2:word = new stringbuffer (' G ');d efault:word =  New stringbuffer (' M ');} Word.append (' a '); Word.append (' I '); Word.append (' n '); SYSTEM.OUT.PRINTLN (word);} Private static void rightmethod ()  {StringBuffer word = null; Random rnd = new random ();switch  (Rnd.nextint (3))  { // error1case  1:word = new stringbuffer ("P");// error2break;// error3case 2:word =  new stringbuffer ("G");// error2break;// error3default:word = new  StringBuffer ("M");// error2break; error3}word.append (' a '); Word.append (' I '); Word.append (' n '); SYSTEM.OUT.PRINTLN (Word);}}


Output Result:

The first line of output is Ain, the second line output achieves the desired effect, and outputs a pain,gain,main random.


Code parsing:

The first line of the output corresponding function is Errormethod, the second line corresponding to the function is Rightmethod. In Errormethod, there are three bugs.

    •   The first bug is that the selected random number causes the switch statement to reach only two of its three cases. The specification of Random.nextint (int) describes the word: "Returns a pseudo-random, evenly distributed int value from 0 (inclusive) to a specified numeric value (not included)." This means that the expression Rnd.nextint (2) possible values only 0 and 1,switch statements will never reach the case 2 branch, which means that the program will never print gain. The Nextint parameter should be 3 instead of 2.

    • last and most subtle. The bug is that the expression new StringBuffer (' m ') may not be doing what you want it to do. You might not be familiar with the StringBuffer (Char) constructor, which is easy to explain: it doesn't exist at all. The StringBuffer has a parameterless constructor, a constructor that takes a string as the initial content of the string buffer, and a constructor that accepts an int as the initial capacity of the buffer. In this example, the compiler chooses the constructor that accepts int to convert the character value ' m ' to an int value of 77 by widening the original type conversion. In other words, new StringBuffer (' m ') returns an empty string buffer with an initial capacity of 77. The remainder of the program adds the characters a, I, and N to the empty string buffer and prints out the string buffer that is always ain content. The point to remember here is that char is not a string, it is more like an int.


(Note: This "Java doubts" series, are bloggers read "Java doubts" original book, the original book on the explanation and example part of the adaptation, and then write a blog post. All examples are tested in person and shared on GitHub. Use these examples to motivate yourself and benefit others. At the same time, all posts in this series will be posted in the blogger's personal public number (search for "Love Ape" or "ape_it") for easy reading. If there is any infringement of the original rights of the author's content, please inform the blogger in time, in order to promptly delete, if the reader has objection to the content of the text or questions, welcome to the blog or public messages and other ways to discuss together. )

Source code Address: Https://github.com/rocwinger/java-disabuse



This article from "Winger" blog, declined reprint!

The ubiquitous bug of "Java FAQ"

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.