• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Whizz Labs Diagnostic Test

 
Ranch Hand
Posts: 61
Mac IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following code:



Why the switch expression take case y? For me switch( x+y ) is 2, also it should print default. x and y are both final! And I thought (x+y) is allowed in a switch expression?

Thanks for help.
 
Marshal
Posts: 69810
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you start by evaluating the four cases; rather than writing case i: write case 4: or similar. Remember that the default: case is only chosen if none of the other cases is picked. Youi may find the bytecode useful: javap -c Whizz
 
Ranch Hand
Posts: 84
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Gualeni wrote:I have the following code:



Why the switch expression take case y? For me switch( x+y ) is 2, also it should print default. x and y are both final! And I thought (x+y) is allowed in a switch expression?

Thanks for help.

because the switch evaluates the x+y =2 then  compiler search for a related value, case y: has the value 2, so it jumps to case y but not the default reason is that switch has the ability to create a jump statement  whatever the cases you have, so answer is case y exicuited which has the value 2 related to case x+y
 
Mike Gualeni
Ranch Hand
Posts: 61
Mac IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello

You're right. But this behavior I didn't read in no book!! But I think it's an important behavior.

If I change the values in the code vice versa final int x = 2; and final int y = 0; the output is:

A
B
default
C



because no break stops from falling down! And now the program choose case x: {System.out.println("A");}!! Strange behavior!!??
 
sohail hussain
Ranch Hand
Posts: 84
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Gualeni wrote:hello

You're right. But this behavior I didn't read in no book!! But I think it's an important behavior.

If I change the values in the code vice versa final int x = 2; and final int y = 0; the output is:

A
B
default
C



because no break stops from falling down! And now the program choose case x: {System.out.println("A");}!! Strange behavior!!??

let me help you
In switch statements first of all, it looks for a perfect match by directly jumping into the matching object are values,, because switch assumes that all the cases are same in nature that's the reason it creates a jump statement and after it finds the correct value then fall into the immediate case and continue with rest of the cases until you provided the break statement to break the fall of switch
 
You save more money with a clothesline than dozens of light bulb purchases. Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic