• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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: 24001
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: 24001
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: 24001
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();
 
Space pants. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic