import java.io.*;
public class t1
    public static void main(String a[])
        int i;
        char c='a';
        char[] ch={'a','b','c'};

            System.out.println(ch[i]+" ");
        //String s=c.toString();  //char cant be dereferenced
        String s=ch.toString();
        System.out.println(" S : "+s);

Q-1) I am getting an error while converting a character array to String aoject.
Q-2) why we can't change a single character to String?

Edited by devanshee

5 Years
Discussion Span
Last Post by stultuske

sure we can do that, but char is a primitive type, you are trying to treat it as an instance of a class, which it isn't.


You can easily create a String instance from an array of char, but not using the toString method (which exists on the array, but won't have the expected result unless you know what to expect which most people don't).

Search the API documentation, it's right there in the page relating to the String class.
If you don't have it yet, you can download it from http://www.oracle.com/technetwork/java/index.html just like the JDK itself (or browse it online, but you really want a local copy).
The documentation downloads are near the bottom of the page.


Thanx, jwenting . I have the local copy of jdk but I just wanted to solve it by toString methode.But now I know that it's not possible that way.


well... we kind of already assumed you had the jdk locally, otherwise it would be a pain trying to compile your Java code

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.