• Post Reply Bookmark Topic Watch Topic
  • New Topic

Questions about switch ... case ?  RSS feed

 
Chen SanHau
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Question 1
A>
In Java,case can accept int、char primitive typ,
if I use byte、short primitive type as expression, only because of Java
will auto convert byte、short to int, so Java can compile and run?
But why case can't accept long type?? What does this design purpose for?
B>

Is there any simple way to instead of

like this or somethig:

Question 2

I use return instead of break in codes, and Java still compile&run OK.
Is there something difference between return and break when
I write codes in switch-case? I mean that can cause different results if I use return instead of break??
Question 3

If I rewrite codes like this:

Java compile error:
empty character literal
unclosed character literal
What does it mean?
case '' means case null ??? Is it right ??
If someone can help me, I would be very appreciated!!
[ March 10, 2004: Message edited by: Ha LoHa ]
[ March 10, 2004: Message edited by: Ha LoHa ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question 1 A
Because that's the way it is. When the original designers and implementors of the language put things together, for whatever reasons, this is just how they decided to do things. Perhaps they were considering some efficiencies with memory allocation and accesses. I don't know.
Question 1 B
Java case construct isn't the most robust. There isn't a simpler way to allow multiple cases to match a switch selection.
Question 2
Not for this simple example.
Of course, if you had included code after the switch construct, then returning from the switch would "skip" that code after the switch. If you were to only break out of the switch, then the code following it would be executed.
Question 3
As mentioned in a previous thread of yours, remember that char is a primitive integral data type in Java. What would an empty integer be? Java provides no such concept, and there isn't such a thing as a null char or an empty char.
Also, note that a pace is not an empty character - it's a space character.
 
Chen SanHau
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
char is a primitive integral data type in Java
What does integral mean here?
I am a newer learner of Java in Taiwan, and I may misinterpret "integral".
Thanx.
[ March 11, 2004: Message edited by: Ha LoHa ]
 
James Chegwidden
Author
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, it is a whole (no decimal) number.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ha LoHa:
char is a primitive integral data type in Java
What does integral mean here?
I am a newer learner of Java in Taiwan, and I may misinterpret "integral".
Thanx.
[ March 11, 2004: Message edited by: Ha LoHa ]

As James says, it means that a char is really a number without any decimal place, just like an int. The numerical value is used to find the character to display by using a table. On machines in the U.S. this is typically the ASCII chart. However, I think Java uses Unicode instead to support internationalization. The concept is still the same, though. The memory where a char variable is stored contains a number which is used to identify the symbol that is printed.
HTH
Layne
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!