Results 1 to 14 of 14

Thread: Switch

  1. #1
    java4amanda is offline Member
    Join Date
    Jan 2012
    Posts
    55
    Rep Power
    0

    Default Switch

    hi all,

    i was a going through a book on switch statements which said " case constant must be a compile time constant that is it is assigned a literal value as soon as it is declared" And it gave the following code as an example saying that if the user attempts to run the code it will throw compile time error. below mentioned is the code

    final int a=1;

    final int b;

    b=2;

    int x=0

    switch(x)

    case a: ok

    case b: compile time error


    ok, now when i tried using the above the concept it doesn't throw me any error. FYI my java version is 5.0 and the book is also for java 5

    Can anyone explain?

    Thank you and regards!!

  2. #2
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Red face Re: Switch

    Quote Originally Posted by java4amanda View Post
    hi all,


    case b: compile time error


    ok, now when i tried using the above the concept it doesn't throw me any error. FYI my java version is 5.0 and the book is also for java 5
    Did u use this code? 'x' is integer, but 'b' is a String

    regards
    dhilip

  3. #3
    java4amanda is offline Member
    Join Date
    Jan 2012
    Posts
    55
    Rep Power
    0

    Default Re: Switch

    no i didn't

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default Re: Switch

    Quote Originally Posted by noobplus View Post
    Did u use this code? 'x' is integer, but 'b' is a String
    Don't talk nonsense; nowhere in the code is b declared to be a String.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default Re: Switch

    Quote Originally Posted by java4amanda View Post
    ok, now when i tried using the above the concept it doesn't throw me any error. FYI my java version is 5.0 and the book is also for java 5

    Can anyone explain?

    Thank you and regards!!
    I suspect the explanation is you didn't actually use the above code.
    Show us the actual code you compiled.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    java4amanda is offline Member
    Join Date
    Jan 2012
    Posts
    55
    Rep Power
    0

    Default Re: Switch

    grrr... the code that i mentioned above is just a sample code the book mentioned. am from my side i did some thing like this :


    class Sws

    {

    public static void main(String[] ags)

    {

    final int x;

    x=2;

    switch(x)

    {

    case 1:

    System.out.println("1");

    break;

    case 2:

    System.out.println("2");

    break;

    default:

    System.out.println("default");

    }

    }

    }

    Now this code using java 5 compiles fine. now the book named scjp by kathy sierra also for java 5 says that the above code will throw error unless am assigning the variable x in one single line.

    what is the issue? was that a typing mistake by the book or what?

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default Re: Switch

    As I suspected.
    Can you spot the difference between your code here and that in your first post?
    None of your case checks use a variable (whether final or not), whereas in your first post the case checks were against 'a' and 'b'.

    Whether 'x' (the variable being checked in the switch) is a compile time constant or not is irrelevant.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default Re: Switch

    Quote Originally Posted by java4amanda View Post
    Now this code using java 5 compiles fine. now the book named scjp by kathy sierra also for java 5 says that the above code will throw error unless am assigning the variable x in one single line.

    what is the issue? was that a typing mistake by the book or what?
    The book is wrong and the code is fine; according to the JLS:

    Quote Originally Posted by JLS
    4.12.4 final Variables
    A variable can be declared final. A final variable may only be assigned to once. It is a compile time error if a final variable is assigned to unless it is definitely unassigned (16) immediately prior to the assignment.
    A blank final is a final variable whose declaration lacks an initializer.
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    java4amanda is offline Member
    Join Date
    Jan 2012
    Posts
    55
    Rep Power
    0

    Default Re: Switch

    yes yes i knew it but i then the next question that comes is am i going through wrong book for scjp?

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

    Default Re: Switch

    Quote Originally Posted by java4amanda View Post
    yes yes i knew it but i then the next question that comes is am i going through wrong book for scjp?
    I don't think so although it surprises me a bit that Kathy Sierra made such an obvious blooper; otoh, there are many Java books that only deserve to be in the fire place; people think they can make money just by putting 'Java' on the cover of their book while the inside just contains utter crap.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default Re: Switch

    Can you post exactly the code in the Kathy Sierra book, because as I said, your code in post #1 is completely different to the code in post #6.
    The original code highlights the problem described in the quoted text:
    "
    case constant must be a compile time constant that is it is assigned a literal value as soon as it is declared
    "
    Your code in post #6 does not.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    java4amanda is offline Member
    Join Date
    Jan 2012
    Posts
    55
    Rep Power
    0

    Default Re: Switch

    Quote Originally Posted by JosAH View Post
    I don't think so although it surprises me a bit that Kathy Sierra made such an obvious blooper; otoh, there are many Java books that only deserve to be in the fire place; people think they can make money just by putting 'Java' on the cover of their book while the inside just contains utter crap.

    kind regards,

    Jos
    yes there are few more mistakes i encountered in kathy sierras' book. but this time i thought it was related to java version. thanks any ways for the response.

  13. #13
    java4amanda is offline Member
    Join Date
    Jan 2012
    Posts
    55
    Rep Power
    0

    Default Re: Switch

    Quote Originally Posted by Tolls View Post
    Can you post exactly the code in the Kathy Sierra book, because as I said, your code in post #1 is completely different to the code in post #6.
    The original code highlights the problem described in the quoted text:
    "
    case constant must be a compile time constant that is it is assigned a literal value as soon as it is declared
    "
    Your code in post #6 does not.
    hey dude the code at post no 6 is mine. but the code at post no 1 is sierras'... : ) any ways i got the answer from the moderator!!! thank you!!

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default Re: Switch

    Quote Originally Posted by java4amanda View Post
    hey dude the code at post no 6 is mine. but the code at post no 1 is sierras'... : ) any ways i got the answer from the moderator!!! thank you!!
    Which means the book is correct.
    As I would expect from a Kathy Sierra book.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. switch statements
    By jim01 in forum New To Java
    Replies: 7
    Last Post: 04-10-2011, 10:52 PM
  2. switch
    By dj kourampies in forum New To Java
    Replies: 17
    Last Post: 01-30-2009, 05:32 PM
  3. switch
    By dj kourampies in forum New To Java
    Replies: 2
    Last Post: 01-30-2009, 08:46 AM
  4. Switch help please!!!!
    By soc86 in forum New To Java
    Replies: 6
    Last Post: 11-23-2008, 07:25 PM
  5. What is wrong with my switch??
    By soc86 in forum New To Java
    Replies: 3
    Last Post: 11-20-2008, 12:34 AM

Posting Permissions

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