Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Questions about switch ... case ?

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Author
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically, it is a whole (no decimal) number.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic