Solved

JDK6: about switch

Posted on 2010-11-27
2
755 Views
Last Modified: 2013-12-29
hi all,

the following codes give the output

9 10 10 d 13

but the variable s has actually value 9, 10 and 11.

can anyone explain how exactly here works?

Codes:

public class Ebb {

    static int x = 7;
    public static void main(String[] args) {
        // TODO code application logic here
        String s = "";
        for (int y = 0; y <3; y++) {
            x++;
            switch(x) {
                case 8: s += "8";
                case 9: s += "9";
                case 10: { s += 10; break;}
                default: s += "d";
                case 13: s+= "13";
            }
        }
        System.out.println(s);
    }

    static {
        x++;
    }
}

thanks,

wantime
0
Comment
Question by:wantime
  • 2
2 Comments
 
LVL 4

Expert Comment

by:aaver
ID: 34221775
In a switch clause, every case is performed after the match until break in encountered.
0
 
LVL 4

Accepted Solution

by:
aaver earned 500 total points
ID: 34221807
In more detail:
In the loop x has the values 9, 10, and 11.
x = 9 -> the match is at case: 9 -> s += "9" // s = "9"
since no break is encountered the case: 10 is also performed -> s += 10 // s = "9" + 10 = "910"
now a break is encountered and the loop continues with the next value

x = 10 -> the match is at case: 10 -> s += 10 // s = "910" + 10 = "91010"
a break is encountered and the loop continues with the next value

x = 11 -> there is no match -> the match is at default:-> s += "d" // s = "91010" + "d" = "91010d"
since no break is encountered the case: 13 is also performed -> s += "13" // s = "91010d" + "13" = "91010d13"
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 38
firstChar challenge 13 107
firstswap challenge 20 67
Impossible to extract MSI from new JAVA releases 2 47
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now