• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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!
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

Henry
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Likes 3
  • Mark post as helpful
  • send pies
  • 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.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
Ole Sandum
Ranch Hand
Posts: 76
3
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston. I believe GoLang's switch cases default to not fall through.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent, thank you for the explanation.
your most welcome!
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Daniel Cox
Ranch Hand
Posts: 231
12
  • Mark post as helpful
  • send pies
  • 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.
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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:

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Marshal
Posts: 55785
164
  • Mark post as helpful
  • send pies
  • 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#?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!