Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

oca switch statement

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in the book of kathy sierra oca se 7 book the author said
the case constant must be a compile-time constant!
then i wanted to know about compile-time constant and i read this article https://coderanch.com/t/454384/java/compile-time-constant  here i found that we can also use relational operator in compile time constant.
but in book there is another line saying
the switch can only check for equality. This means that the other relational
operators such as greater than are rendered unusable in a case.

so does it mean we will not consider any operations that can be applied to a constant at compile time in the case of switch statement ?
 
author
Posts: 23835
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am not sure of how the two parts of your argument can be related. Switch statements doesn't work with booleans (well, the last time that I checked, that is). And I don't see how you can insert an relational operator into a constant expression, without making the result a compile time constant that is a boolean.

Henry
 
Henry Wong
author
Posts: 23835
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

fahmida tasnim prema wrote:
in book there is another line saying the switch can only check for equality. This means that the other relational operators such as greater than are rendered unusable in a case.



Hmmm... Read this a few times.  And to be honest, even though the sentence is a bit out of context, I am pretty sure that the author was talking about the relationship between the value in the switch clause being compared to the value in the case clause.  And not talking about the relationship between the components of the value of the case clause only.

In fact, I would be surprised if the examples in the book even attempted to use a constant expression versus simply literals, as the compile time constant. After all, aren't OCA books for beginners?

Henry
 
fahmida tasnim prema
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:
And I don't see how you can insert an relational operator into a constant expression, without making the result a compile time constant that is a boolean.

Henry



final boolean b=5<6;
can i say it is a compile time constant ??
 
Henry Wong
author
Posts: 23835
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

fahmida tasnim prema wrote:
final boolean b=5<6;
can i say it is a compile time constant ??



Yes. The b variable is a compile time constant that is a boolean type.  And as already mentioned, the last time I checked, the switch statement doesn't work with booleans.

EDIT: Okay, in thinking about this a bit more... I guess you can use the relational operator, which will cause the result to be a boolean; and then, use the ternary operator, using ints as the result, to get the result to be of int type. And then, use that as the value of case statement...

Regardless, as mentioned, I think it is safe to assume that the authors were talking about about the comparison between the switch and case values. And not a complex setup to get the relational operator into an expression that can work as part of the case clause.

Henry
 
fahmida tasnim prema
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:


EDIT: Okay, in thinking about this a bit more... I guess you can use the relational operator, which will cause the result to be a boolean; and then, use the ternary operator, using ints as the result, to get the result to be of int type. And then, use that as the value of case statement...


Henry



please give me an example
 
Enthuware Software Support
Posts: 4330
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

fahmida tasnim prema wrote:in the book of kathy sierra oca se 7 book the author said
the case constant must be a compile-time constant!
then i wanted to know about compile-time constant and i read this article https://coderanch.com/t/454384/java/compile-time-constant  here i found that we can also use relational operator in compile time constant.
but in book there is another line saying
the switch can only check for equality. This means that the other relational
operators such as greater than are rendered unusable in a case.

so does it mean we will not consider any operations that can be applied to a constant at compile time in the case of switch statement ?



Not sure in what context the book says this but given the limited text that you've quoted, the book may be incorrect. The following compiles and executes fine:

HTH,
Paul.
 
fahmida tasnim prema
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you so much for this example . now i am clear . i am pretty much sure that is what the book said . But as i am beginner i can make mistake .  
[b]Also, the switch can only check for equality. This means that the other relational
operators such as greater than are rendered unusable in a case. The following is an
example of a valid expression using a method invocation in a switch statement.
Note that for this code to be legal, the method being invoked on the object
reference must return a value compatible with an int.

}[/b]
that is what the book said .
 
Henry Wong
author
Posts: 23835
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

From my previous quote...

Henry Wong wrote: ... And to be honest, even though the sentence is a bit out of context, I am pretty sure that the author was talking about the relationship between the value in the switch clause being compared to the value in the case clause.  And not talking about the relationship between the components of the value of the case clause only.

In fact, I would be surprised if the examples in the book even attempted to use a constant expression versus simply literals, as the compile time constant. After all, aren't OCA books for beginners?



And I stand by it -- especially, since I predicted that the example only used literals... ... The author was talking about the comparison between the switch value and the case values. And not about the use of relational operators, in a constant expression, with the case statement.

Henry
 
Paul Anilprem
Enthuware Software Support
Posts: 4330
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:
From my previous quote...

Henry Wong wrote: ... And to be honest, even though the sentence is a bit out of context, I am pretty sure that the author was talking about the relationship between the value in the switch clause being compared to the value in the case clause.  And not talking about the relationship between the components of the value of the case clause only.

In fact, I would be surprised if the examples in the book even attempted to use a constant expression versus simply literals, as the compile time constant. After all, aren't OCA books for beginners?



And I stand by it -- especially, since I predicted that the example only used literals... ... The author was talking about the comparison between the switch value and the case values. And not about the use of relational operators, in a constant expression, with the case statement.

Henry


That does make sense.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!