• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

From Java in 21 days (newest edition covering Java 8)

 
Ranch Hand
Posts: 186
1
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I saw a practice certification question that kind of confused me. I thought I had the answer thinking that the result would be 10.0, I was completely wrong based on the code snippet from below:





I don't understand why case 1 and case 2 are implemented.....I thought that since there are no conditional tests, it would go immediately to the default. Please tell me where I went wrong. Thanks!
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Question. What is the value of "z"? And hint. The type of "z" is an int.

Henry
 
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At this point 9 and 5 are int then implicitly widening conversion took place so they become float like x = 9.00f and y = 5.00f
float x = 9;
float y = 5;

( x / y) = 9.00f / 5.00f and answer is 1.8f i.e. float then we are storing it in z which is an int variable. Explicit Narrowing conversion took place
so z = 1 now.

case 1 gets executed x = x + 2 means 9 + 2, x becomes 11. Then there is no break in that case so case 2 also gets executed x = x + 3 means 11 + 3 x becomes 14 then
again there is not break even after case 2, so default also gets executed x = x + 1 means 14 + 1 so x becomes 15
Answer is 15.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ganish Patil wrote:Then there is no break in that case...


Has anyone else ever thought that switch would have been much better off designed to default to break, but allow a continue statement if you want to continue to the next case?

Winston
 
Naziru Gelajo
Ranch Hand
Posts: 186
1
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ganish Patil wrote:At this point 9 and 5 are int then implicitly widening conversion took place so they become float like x = 9.00f and y = 5.00f
float x = 9;
float y = 5;

( x / y) = 9.00f / 5.00f and answer is 1.8f i.e. float then we are storing it in z which is an int variable. Explicit Narrowing conversion took place
so z = 1 now.

case 1 gets executed x = x + 2 means 9 + 2, x becomes 11. Then there is no break in that case so case 2 also gets executed x = x + 3 means 11 + 3 x becomes 14 then
again there is not break even after case 2, so default also gets executed x = x + 1 means 14 + 1 so x becomes 15
Answer is 15.



Excellent, thank you for the explanation.
 
Ranch Hand
Posts: 76
3
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Winston. I believe GoLang's switch cases default to not fall through.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Has anyone else ever thought that switch would have been much better off designed to default to break, but allow a continue statement if you want to continue to the next case?

yes absolutely,there might be some reasons that switch hasn't been designed with default break. There could be a situation where if a case 1 is executed then case 2 must also get executed along with case 1. But if case 2 is executed then case 1 is not mandatory to get executed, in such kind of situation switch without default break is useful. For an example

 
Naziru Gelajo
Ranch Hand
Posts: 186
1
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In regards to switch, it can not be used to convert say a String array to a long array, am I correct? It can only be used on byte, short, int, String but not on float, double and long right?
 
Ganish Patil
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

can only be used on byte, short, int, String but not on float, double and long right?

yes correct, It also works with enumerated types and you can also have switch with String statements from JDK 7 and later.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Excellent, thank you for the explanation.

your most welcome!
 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:Has anyone else ever thought that switch would have been much better off designed to default to break, but allow a continue statement if you want to continue to the next case?



You could make a good case for this, but when I learned C (which has the same control flow), the examples of how to use the fall-through behavior looked like this:



When there is no code separating related cases, defaulting to fall-through (without a continue statement) seems like what the code should be doing. Adding code is where it gets more complicated. If break were the default, then I'd want the case statements to allow for lists, like case 'y' , 'Y':. But, it is what it is.
 
Ranch Hand
Posts: 234
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ganish Patil wrote:...there might be some reasons that switch hasn't been designed with default break.


I agree. A switch statement was designed to have a larger number of possible execution paths than an if-else statement.
 
Ranch Hand
Posts: 175
17
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you suspect that your code may have fall-through bugs, you can ask the compiler to lookout for these bugs by using the javac command’s -Xlint:fallthrough option

When I compiled the code with this option, I got the following warnings:

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ganish Patil wrote:there might be some reasons that switch hasn't been designed with default break.


Probably because it's a feature that already existed in C and C++ in this way - Java inherited a lot of its features from those languages. When Java was invented in the 1990's, one of the goals was to create a language that would look familiar to C++ programmers.
 
Ole Sandum
Ranch Hand
Posts: 76
3
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In GoLang, if I remember correctly, a case in a switch defaults to break unless the next case statement directly follows the current (i.e there is no logic in between).
If you want to fall through you use continue at the end of the case logic.

Thoughts? Do you think this is better than how it is done in Java and C?
 
Ole Sandum
Ranch Hand
Posts: 76
3
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ole Kristian Sandum wrote:In GoLang, if I remember correctly, a case in a switch defaults to break unless the next case statement directly follows the current (i.e there is no logic in between).
If you want to fall through you use continue at the end of the case logic.

Thoughts? Do you think this is better than how it is done in Java and C?



I was somewhat mistaken. For one the correct keyword to use would be 'fallthrough' - just a small semantics error.

Furthermore, a case without logic wouldn't fall through to the next case. Consider the following Java code.


The Go equivalent would be


So a bit more different than what I originally thought. Question still stands though

Ole Kristian Sandum wrote:Thoughts? Do you think this is better than how it is done in Java and C?

 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:Probably because it's a feature that already existed in C and C++ in this way - Java inherited a lot of its features from those languages. When Java was invented in the 1990's, one of the goals was to create a language that would look familiar to C++ programmers.


I'm sure you're right. However, it was already a bone of contention even when Java was designed. And since they specifically removed the ability to use Duff's device, I'm not quite sure why they hung onto it.

And I'm not saying "don't allow dropthorughs"; just don't make them the default behaviour.
Java already has a perfectly good continue keyword, so why not use it?
Using Ole's example:
Ah well. Barking at the moon again...

Winston
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it would be far better for switch not to fallthrough by default, but that would have made it look different from C++.
What does it look like in C#?
 
Not looking good. I think this might be the end. Wait! Is that a tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic