wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Set value of char and String ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Set value of char and String ?" Watch "Set value of char and String ?" New topic
Author

Set value of char and String ?

Chen SanHau
Greenhorn

Joined: Feb 06, 2004
Posts: 27

compile error:
empty character literal
unclosed character literal

compile OK!!
Why not:
empty String literal
unclosed String literal

Why String and char have such a different result ??
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Remember that char is a primitive integral data type in Java. What would an empty integer be? In Java, there's simply no such thing.
String is a class. That class has been defined to allow for empty String values, where a String object might exist that contains no characters.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Chen SanHau
Greenhorn

Joined: Feb 06, 2004
Posts: 27
empty character literal
unclosed character literal
"unclosed" mean what ?
Does it equal null ??
char myC1 = '';
Why don't it print "Illegal initialization"?
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
"unclosed" usually means that you have an opening delimiter in your code, but not closing one to match it. If you have only one single-quote (') without a matching single-quote after the next character, you can get this error. Similarly, if you only have one double-quote ("), you will get a similar error for an unclosed String constant.
HTH
Layne


Java API Documentation
The Java Tutorial
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
char myC1 = '';
Why don't it print "Illegal initialization"?

The guy who wrote the compiler decided to design things such that when you tried to specify an illegal, non-existant literal char value with '', that the compiler should complain about that. So, it does.
Chen SanHau
Greenhorn

Joined: Feb 06, 2004
Posts: 27
Thanks.
I try to express concepts learning from all you good guys to see if it's wrong or not.
If Java tries to compile codes
char myC1 = '';
Java will store ' in heap as value of myC1, but doesn't find
closing delimiter.
So JVM will show
empty character literal
unclosed character literal
?
Is it right?
 
Consider Paul's rocket mass heater.
 
subject: Set value of char and String ?