• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Dereferenced Error Code

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to experiment with character Methods and get a the error message that "char cannot be dereferenced".
Here is the code:
String s1 = args[0] ;
char c = s1.charAt( 0 ) ;
char c2 = c.charValue() ;
The error is in the last statement. What am I missing?
Thanks
 
Trailboss
Posts: 23756
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try char c2 = c ;
 
craig prothero
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about if I wanted to convert to String:
I tried
char c = args[0].charAt(0)
String c.toString()
and I got the same message.
Thanks
 
paul wheaton
Trailboss
Posts: 23756
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A char is not an object. It is what is called an "atomic data type" or "primitive data type" - it's the kind of stuff that objects are made out of.
So a char has no methods. It is raw data.
Creating a String out of one char is rather unusual. Try String.valueOf( c ) - this is a static method that will take in a char and return a String.
 
craig prothero
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am confused. What is the Character Class as defined on the Sun website.
It looks like there is a character method toString()
This looks exactly like the string method length.
Therefore if
int sampleLength = args[0].length()
works, then

char c = args[0].charAt(0) ;
String sample = c.toString() :
should also work considering that the syntax for each method is exactly the same.
If Char is a primitive type, what is the Class Character and what is the difference.
Thanks
 
paul wheaton
Trailboss
Posts: 23756
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is the primitive char and there is the class Character. Character is a wrapper class for char.
I suppose you could say:
String s = new Character( c ).toString();
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic