简体   繁体   English

String.valueOf() 和 new String() 的区别

[英]Difference between String.valueOf() and new String()

What is the difference between String.valueOf() and new String()? String.valueOf() 和 new String() 有什么区别? When would you use one over the other?您什么时候会使用其中一种?

example 1:示例 1:

public String fun(){

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

}

example 2:示例 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.我没有意识到这一点,因为我一直在使用 new String("something" + foo) 并且它已经工作了,正如 fastcodejava 所指出的那样。 So is there a difference between the two if I use new String("something" + foo) or String.valueOf(foo) ?那么如果我使用 new String("something" + foo) 或 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.然而,第二种方法只是一个构造函数,它创建了一个 String 类型的新对象。 It cannot take an integer as argument.它不能将整数作为参数。

There is no constructor for String that takes a single integer. String没有采用单个整数的构造函数。

  • 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在示例 2 中,它应该是

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.第一种方法尝试将类型转换为 String 并使用该值创建一个新字符串。 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);但是如果你确实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.除了其他人给出的点之外,我看不到 String.valueOf() 和 new String() 之间的任何区别,因为我看到两者都在创建一个新的 String 对象。 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 false stating that both are different String objects.两者都给出错误的说明,它们都是不同的 String 对象。
So I don't see any performance or memory consumption difference.所以我没有看到任何性能或内存消耗差异。

Strangely, the following code pieces produced different results.奇怪的是,以下代码片段产生了不同的结果。 Looks like there is some difference when it comes to handling Base64 encoded byte[]在处理 Base64 编码的字节 [] 时看起来有些不同

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.在读取文件并使用 readAllBytes() 时,使用 new String() 可以打印该文件的内容。

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' //文件的内容被记录为'你好,这是我的txt文件的内容'

However, when using String.valueOf, you get the address location instead.但是,当使用 String.valueOf 时,您将获得地址位置。

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

//an address location is logged instead like 'A@24gj49bj' //记录一个地址位置,而不是像'A@24gj49bj'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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