Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

JDK6: about switch

Posted on 2010-11-27
2
757 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jar file executable 12 58
java jdbc batch example 8 39
Opening PDF on button click and fill new document 2 47
java example issue 3 19
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

790 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