Results 1 to 12 of 12
Like Tree2Likes
  • 1 Post By jim829
  • 1 Post By JosAH

Thread: Puzzled by enums

  1. #1
    radiodave is offline Member
    Join Date
    May 2015
    Posts
    15
    Rep Power
    0

    Default Puzzled by enums

    I'm coming from C/C++, where enums were pretty straightforward. I'm making a program that uses enums, but I'm a little puzzled why in a case statement, the enums don't need to be fully qualified, but in an "if" statement it does. Example:

    class class_1
    {
    public static enum values
    {
    value1,
    value2,
    value3
    }
    public values getvalue()
    {
    return values.value1;
    }
    }

    in another class:

    class_1 newclass = new class_1;
    class_1.values newvalue = newclass.getvalue();
    switch(newvalue)
    {
    case value1: // works

    break;
    }

    if(newvalue == value1) //nope

    if(newvalue == class_1.values.value1) //works

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Puzzled by enums

    What's not straightforward about it? That's just how the switch statement works. If you put an integer in there then you can use integers in the case statements, and if it is a String you can put String literals in the case statements - and when it is an enum you can put the enum values in the case statements.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    2teez is offline Member
    Join Date
    May 2015
    Posts
    6
    Rep Power
    0

    Default Re: Puzzled by enums

    Quote Originally Posted by radiodave View Post
    I'm coming from C/C++, where enums were pretty straightforward. I'm making a program that uses enums, but I'm a little puzzled why in a case statement, the enums don't need to be fully qualified, but in an "if" statement it does. Example:

    class class_1
    {
    public static enum values
    {
    value1,
    value2,
    value3
    }
    public values getvalue()
    {
    return values.value1;
    }
    }

    in another class:

    class_1 newclass = new class_1;
    class_1.values newvalue = newclass.getvalue();
    switch(newvalue)
    {
    case value1: // works

    break;
    }

    if(newvalue == value1) //nope

    if(newvalue == class_1.values.value1) //works
    I would be surprised if this code compiled at all!
    When you used
    PHP Code:
    ...
       // what is value1?
       if (newvalue == value1) {
            ...
       }
    ...
    What is value1 in the if statement in your code? Am sure you know that is not referring to one of "constant" in class_1.values.
    Last edited by 2teez; 05-12-2015 at 09:52 AM.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Puzzled by enums

    Did you read the question at all?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    2teez is offline Member
    Join Date
    May 2015
    Posts
    6
    Rep Power
    0

    Default Re: Puzzled by enums

    Quote Originally Posted by gimbal2 View Post
    Did you read the question at all?
    I suppose your question is addressed to me. If so, yes, I did read the question and am sure what "puzzled" the OP is not why h(is|er) code work worked in the switch statement, but why the code is not working using the if statement. Hence, the question I asked!

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Puzzled by enums

    Quote Originally Posted by 2teez View Post
    If so, yes, I did read the question and am sure what "puzzled" the OP is not why h(is|er) code work worked in the switch statement, but why the code is not working using the if statement.
    As the OP said, it doesn't work in the if statement.

    if(newvalue == value1) //nope
    And if if doesn't compile, it doesn't "work"

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    2teez is offline Member
    Join Date
    May 2015
    Posts
    6
    Rep Power
    0

    Default Re: Puzzled by enums

    Quote Originally Posted by jim829 View Post
    As the OP said, it doesn't work in the if statement.



    And if if doesn't compile, it doesn't "work"

    Regards,
    Jim
    Hi Jim,
    That its doesn't work in the if statement doesn't say anything much. That was why the OP is asking the question. The issue here is WHY?
    More so, it doesn't work doesn't translate to it doesn't compile alone. It could mean several other things, hence my statement I would be surprised if the OP codes compile to start with.

    I don't see why asking the OP an obvious question should be an issue here.

    Yes the OP showed

    Java Code:
     if(newvalue == value1) //nope
    then I asked what do you think value1 is? Am sure if S(he) knows that value1 is not what S(he) think is it and that S(he) can't be using it that way in the if statement, we wouldn't be discussing this.

    Regards,
    Tim

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Puzzled by enums

    I understand your question. The best answer as far as I can figure to the OP's question is the following(from the JLS):

    Every case constant associated with the switch statement must be assignment compatible with the type of the switch statement's Expression.
    If the type of the switch statement's Expression is an enum type, then every case constant associated with the switch statement must be an enum constant of that type.

    If you change the switch expression to an integer or string value, then the compiler reports the same error as with the if statement. This implies to me that the switch statement dictates the expected case types and the compiler resolves the best it can.

    I believe that because if statements are more liberal in the arguments they accept on either side of the equals sign, the compiler can't make that distinction. Of course, this is all speculation on my part.

    Regards,
    Jim
    gimbal2 likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Puzzled by enums

    Correction: it is educated speculation.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Puzzled by enums

    It's the truth; have a look at the following (contrived and useless) example:

    Java Code:
    public class Test {
    	private enum E1 { one, two, three };
    	private enum E2 { two, three, four };
    	
    	public static void main(String[] args) {
    		
    		E1 expr= E1.two;
    		
    		switch (expr) {
    			case one: System.out.println("one"); break;
    			case two: System.out.println("two"); break;
    			case three: System.out.println("three"); break;
    		}
    	}
    }
    The compiler knows the type of 'expr' (it's a E1) in the switch clause, so there is no need to further qualify 'one' as in 'E1.two'. When 'expr' is being initialized a full qualification is needed 'E1.two' because the type inference mechanism can't deduce which 'two' is meant. (the other 'two' from E2 causes a type mismatch). The same goes for an if statement:

    Java Code:
    if (expr == E1.two) ...
    If you leave out the qualification as in 'two' instead of 'E1.two', the compiler's type inference mechanism will be confused (which 'two' is it?) The power of enums being classes comes with a small sacrifice; enums in Java aren't simple ints as in C++.

    kind regards,

    Jos
    2teez likes this.
    Build a wall around Donald Trump; I'll pay for it.

  11. #11
    2teez is offline Member
    Join Date
    May 2015
    Posts
    6
    Rep Power
    0

    Default Re: Puzzled by enums

    Hi Jim,

    ...If you leave out the qualification as in 'two' instead of 'E1.two', the compiler's type inference mechanism will be confused (which 'two' is it?) ...
    Am glad JosAH made my point clearly!

    In the if statement
    Java Code:
    if (newvalue == value1)
    value1 variable is not the constant in the enum the OP is using.

    regards,
    Tim
    Last edited by 2teez; 05-12-2015 at 11:47 PM.

  12. #12
    radiodave is offline Member
    Join Date
    May 2015
    Posts
    15
    Rep Power
    0

    Default Re: Puzzled by enums

    Thanks, I think Jim829 and JosAH answered my question, and to clear things up, the "nope" example didn't compile. So if I'm understanding correctly, the switch statement is a bit "smarter" than the if statement in that it knows what to expect based on the argument type, but the if statement is only evaluating a boolean expression, so that expression has to be evaluated properly by comparing apples to apples.

Similar Threads

  1. Puzzled about System Input / Output to a file.
    By CodeMonkeyAlx in forum New To Java
    Replies: 4
    Last Post: 03-28-2013, 07:44 PM
  2. Array puzzled
    By Newbieprogrammer in forum New To Java
    Replies: 17
    Last Post: 09-17-2012, 07:19 PM
  3. Replies: 15
    Last Post: 10-31-2011, 04:55 AM
  4. Enums taking in enums?
    By rizowski in forum New To Java
    Replies: 7
    Last Post: 06-11-2011, 01:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •