-1

I'm newbie to Java programming language. So I tried out a sample program (i.e :- Enum Comparison with Switch-Case Statement). Below is the program for your reference

public class TestClass {
    public enum Company {
        Google(1),
        Microsoft(2),
        JPMorgan(3),
        WellsFargo(4);

        private int companyRatings;

        private Company(int companyValue) {
            this.companyRatings = companyValue;
        }
    }

    public static void enumComparison(Company type) {
        switch (type) {
            case Google:
                System.out.println("Company Name : " + type + " - Company Position : " + type.companyRatings);
            case Microsoft:
                System.out.println("Company Name : " + type + " - Company Position : " + type.companyRatings);
                break;
            case WellsFargo:
                System.out.println("Company Name : " + type + " - Company Position : " + type.companyRatings);
                break;
            default:
                System.out.println("Company Name : " + type + " - Company Position : " + type.companyRatings);
                break;
        }
    }

    public static void main(String[] args) {
        enumComparison(Company.Google);
    }
}

So if you see the above program you can notice that i have missed break keyword in the 1st case (i.e:- Case Google:). In C# the break keyword is mandatory in the switch statements, It would result in compile time error if the keyword is missed. But I noticed in Java that it is not the case(No Compile time error for missing the break keyword).
So in the Main method I'm passing Company.Google as an argument. Since the break keyword is missed out in the 1st case(Case Google:) but it is moving to the second case(Case Microsoft:) and prints the value, even though there is a type mismatch. It's weird and I'm not sure why this happens? So it prints the duplicate values. I'm confused.

Output

Company Name : Google - Company Position : 1
Company Name : Google - Company Position : 1 
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
Aishu
  • 1,310
  • 6
  • 28
  • 52
  • 5
    You need to read this.. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html – Ruchira Gayan Ranaweera Jun 20 '16 at 10:37
  • 2
    That's just how it works with `switch`, if there's no `break` or `return` or `throw` then execution falls through to the next case. This is a feature that Java inherited from C++, where it works in the same way. There is no type mismatch involved here (enum constants do all have the same type, `Company`). – Jesper Jun 20 '16 at 10:38
  • Couldn't you just override the `toString()` method? – Mr. Polywhirl Jun 20 '16 at 10:39
  • @Jesper - I mean the Switch type is Google, but since it is moving to the next case that is Microsoft – Aishu Jun 20 '16 at 10:45
  • 1
    It does not matter what each switch-case is doing, it will print the same thing, because it is printing information pertaining to the incoming argument... That is why it says "Google" twice... Add a break or a return after each case. Or create a `toString()` in the `Company` enum and just print `this`... – Mr. Polywhirl Jun 20 '16 at 10:54
  • 1
    The switch type is not `Google`. `Google` is not a **type**, it is a **value** (one of the values of the type `Company`). There is no **type** mismatch. – Jesper Jun 20 '16 at 11:10

4 Answers4

1

You can move your print logic into the toString() of the Company enum and modify your switch statement to group companies by their type. This shows how to use a switch-case without each case having its own break.

public class TestClass {
    public enum Company {
        Google(1),
        Microsoft(2),
        JPMorgan(3),
        WellsFargo(4);

        private int companyRatings;

        private Company(int companyRatings) {
            this.companyRatings = companyRatings;
        }

        @Override
        public String toString() {
            return "Company Name : " + name() + " - Company Position : " + companyRatings;
        }
    }

    public static void enumComparison(Company type) {
        switch (type) {
            case Google:
            case Microsoft:
                System.out.println("[Technology Company]: " + type);
                break;
            case JPMorgan:
            case WellsFargo:
                System.out.println("[Investment Company]: " + type);
                break;
            default:
                System.out.println("[General... Company]: " + type);
                break;
        }
    }

    public static void main(String[] args) {
        enumComparison(Company.Google);
    }
}
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
0

Even though break is not mandatory, there are still many cases where it is a good idea to do so, such as here.

P.S.: You set companyRatings to private and access it later, it surprises me that this compiles.

Konrad Höffner
  • 11,100
  • 16
  • 60
  • 118
  • I don't see any compile time error – Aishu Jun 20 '16 at 10:47
  • 1
    *You set companyRatings to private and access it later, it surprises me that this compiles.* It's still the same class, so of course it works. More here: http://stackoverflow.com/questions/1801718/why-can-outer-java-classes-access-inner-class-private-members – Tom Jun 20 '16 at 11:05
  • Thanks @Tom! Learn something new every day :-) – Konrad Höffner Jun 20 '16 at 11:17
0

It is the way how switch statement is defined in java. Code is executed from case statement which satisfied condition, to break statement.

To understand why break statement is not required, consider this example:

  case Google:
  case Microsoft:
     System.out.println("Tech Company Name : " + type + " - Company Position : " + type.companyRatings);
  break;
  default:
   System.out.println("No Tech Company Name : " + type + " - Company Position : " + type.companyRatings);
                break;

as you see you can have multiple case statements for same block.

user902383
  • 8,420
  • 8
  • 43
  • 63
0

It is not weird at all. By omitting break from the case statements you can create some smart code. Here is a small code example:

This is an enum

public enum Month{
    January(1),
    February(2),
    March(3),
    April(4),
    May(5),
    June(6),
    July(7),
    August(8),
    September(9),
    October(10),
    November(11),
    December(12);
}

And this is a switch case block where we are passing the current month

switch (currentMonth) {
        case January:
            System.out.println("Months to come " + Month.February);
        case February:
            System.out.println("Months to come " + Month.March);
        case March:
            System.out.println("Months to come " + Month.April);
        case April:
            System.out.println("Months to come " + Month.May);
        case May:
            System.out.println("Months to come " + Month.June);
        case June:
            System.out.println("Months to come " + Month.July);
        case July:
            System.out.println("Months to come " + Month.August);
        case August:
            System.out.println("Months to come " + Month.September);
        case September:
            System.out.println("Months to come " + Month.October);
        case October:
            System.out.println("Months to come " + Month.November);
        case November:
            System.out.println("Months to come " + Month.December);
        case December:
            System.out.println("This is the last month of the year");
            break;
        default:
            System.out.println("Invalid year");
            break;
    }

You can see the trick by viewing the output.

Kaustav
  • 741
  • 1
  • 9
  • 18