Question

I got this example code from here:

http://www.tutorialspoint.com/java/java_string_copyvalueof.htm

public class Test{
   public static void main(String args[]){
      char[] Str1 = "This is really not immutable!!";
      String Str2;

      Str2 = copyValueOf( Str1 );
      System.out.println("Returned String " + Str2);

      Str2 = copyValueOf( Str1, 5, 10 );
      System.out.println("Returned String " + Str2);

   }
}

This code is not working for me.

  1. array of character is defined as a String.
  2. copyValueOf is not recognisable!

Now I change it to this:

    char[] Str1 = {'t','o','o','k'};
      String Str2;

      Str2 = copyValueOf( Str1 );
      System.out.println("Returned String " + Str2);

      Str2 = copyValueOf( Str1, 5, 10 );
      System.out.println("Returned String " + Str2);

Still copyValueOf is not working? I have checked this method and it exist on documentation!

No correct solution

OTHER TIPS

  1. Unlike C, char[] is not a String, and vice versa.
  2. You need to specify the class:

    Str2 = String.copyValueOf( Str1 );
    

You might be missing a static import from your code. Add this to the top of your file:

import static java.lang.String.copyValueOf;

Alternately, you could (and perhaps should) specify the class explicitly. Since copyValueOf is a static member of String that would look like this:

String.copyValueOf(Str1);

Here are the problems:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top