Results 1 to 4 of 4
  1. #1
    Denis2k11 is offline Member
    Join Date
    Oct 2012
    Posts
    35
    Rep Power
    0

    Default charValue method is not working? Char cannot be dereferenced?

    I'm asking my compiler to give my the value of two chaacters, a and d. However, it is refusing to compile giving me an error that char cannot be dereferenced. Can someone please explain why this is so?
    Java Code:
    public class Testing
    {
    	public static void main(String[]args)
    	{
    		char x ='a';
    		Character y = new Character('d');
    		System.out.println(x.charValue());
    		System.out.println(y.charValue());
    	}
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,032
    Rep Power
    6

    Default Re: charValue method is not working? Char cannot be dereferenced?

    The charValue method only applies to wrapper objects, not primitives. x is a primitive.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Denis2k11 is offline Member
    Join Date
    Oct 2012
    Posts
    35
    Rep Power
    0

    Default Re: charValue method is not working? Char cannot be dereferenced?

    So what exactly is a wrapper object? What is the difference?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,032
    Rep Power
    6

    Default Re: charValue method is not working? Char cannot be dereferenced?

    In general terms a wrapper object is an object which wraps another object. Sometimes it is done to hide some functionality (make it immutable). In others to add functionality outside of extending a class. In the case of the primitive types (int, char, double), a wrapper object (also known as a boxed type) wraps the primitive inside of an object. Boxed primitives are required when you need to pass a primitive to something that expects an object.

    The primtive types all begin with a lower case letter. The boxed types begin with an upper case letter.

    Other uses are to specify types in generically typed objects.
    e.g

    List<Integer> list;
    vs
    List<int> list; // not allowed

    I suggest you read about them in the tutorials listed in my signature. Check out the boxing and unboxing section in the Really Big Index.


    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 2
    Last Post: 10-19-2012, 05:23 PM
  2. Char to keycode=not working?
    By maximumtech in forum New To Java
    Replies: 6
    Last Post: 05-20-2012, 11:31 PM
  3. ERROR - char cannot be dereferenced HELP
    By jonytek in forum New To Java
    Replies: 6
    Last Post: 05-04-2011, 07:54 AM
  4. replaceALL(char oldChar, char newChar) method
    By arson09 in forum New To Java
    Replies: 0
    Last Post: 04-28-2010, 06:48 AM
  5. Replies: 15
    Last Post: 09-29-2008, 08:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •