• 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
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Set value of char and String ?  RSS feed

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

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 ??
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chen SanHau
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
empty character literal
unclosed character literal
"unclosed" mean what ?
Does it equal null ??
char myC1 = '';
Why don't it print "Illegal initialization"?
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
And when my army is complete, I will rule the world! But, for now, I'm going to be happy with this tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!