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

            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?

Recommended Answers

All 4 Replies

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.