4

The output for following print statement is same, is there any internal difference which is safe as per Privacy Violation: Heap Inspection

char[] ch ={'p','a','s','s','w','o','r','d'};

System.out.println(String.valueOf(ch));

System.out.println(new String(ch));
2
  • 3
    When you made the effort to read the javadoc, what didn't you understand? Commented Dec 6, 2016 at 16:17
  • 1
    Asked this question having privacy violation in mind. there is tool called fortify which scan the entire application and generate issue report privacy violation heap inspection is one of them. For String.valueof it was showing the heap inspection problem but when I used new String(chat array) it got solved though there is no difference between so asked. comment appreciated @SotiriosDelimanolis Commented Dec 7, 2016 at 1:27

2 Answers 2

5

There is no real difference because internal implementation of valueOf is the following:

public static String valueOf(char data[]) {
    return new String(data);
}

As you can see it calls directly new String(data)

Sign up to request clarification or add additional context in comments.

Comments

4

There is no difference

valueOf is static factory method which calls String constructor

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.