简体   繁体   中英

Difference between String.valueOf() and new String()

What is the difference between String.valueOf() and new String()? When would you use one over the other?

example 1:

public String fun(){

 int foo = 55;
 return String.valueOf(foo);

}

example 2:

public String fun(){

int foo = 55;
return new String(foo);

}

Update: Yes, the second example doesn't compile as pointed out by others below. I didn't realize it because I have been using new String("something" + foo) and it has worked, as pointed out by fastcodejava. So is there a difference between the two if I use new String("something" + foo) or String.valueOf(foo) ?

The first method takes an integer and converts it to String. However, the second method is just a constructor that creates a new object of type String. It cannot take an integer as argument.

There is no constructor for String that takes a single integer.

  • String(byte[] bytes)
  • String(char[] value)
  • String(String original)
  • String(StringBuffer buffer)
  • String(StringBuilder builder)

You should use:

Integer.toString(foo);

In example2, it should be

return new String(Integer.toString(foo));

Your one doesn't compile. So there's no need to think about the difference between them, they take different parameters.

The first method tries to convert the type to String and creates a new string with that value. The second method is a constructor which just creates a new string out of a string-like format (not a typeconversion in the sense of Integer -> String).

So the comments are right...it doesnt compile :)

The second code won't even compile. But if you did return new String("" + foo); , it would. They are pretty much same after that.

I can't see any difference between String.valueOf() and new String() apart from the points given by others, as I see both are creating a new String object. I tried using -

    char[] chars = {'c','a','t'};
    System.out.println(String.valueOf(chars) == String.valueOf(chars));
    System.out.println(new String(chars) == new String(chars));

Both give stating that both are different String objects.的说明,它们都是不同的 String 对象。

Strangely, the following code pieces produced different results. Looks like there is some difference when it comes to handling Base64 encoded byte[]

Incorrect:

byte[] bytesArray = new byte[(int) a_imageFile.length()];
FileInputStream fis= new FileInputStream(a_imageFile);
fis.read(bytesArray);
byte[] encoded = Base64.getEncoder().encode(bytesArray);            
result = "data:image/jpg;base64,"+ String.valueOf(encoded);

Correct:

...
result = "data:image/jpg;base64,"+ new String(encoded);

Another example of how they differ:

When reading in files and using readAllBytes(), using new String() allows you to print the content of that file.

String template = new String(fileName.readAllBytes());
Log.info("File content: " + template;

//content of the file is logged like 'hello, this is the content of my txt file'

However, when using String.valueOf, you get the address location instead.

String template = String.valueOf(fileName.readAllBytes());
Log.info("File address: " + template;

//an address location is logged instead like 'A@24gj49bj'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM