This article is a small compilation collected in a technical forum. The questions and answers are concise but complete and detailed. Therefore, we recommend this article to beginners.
INT-> string
Int I = 12345;
String S = "";
Method 1: S = I + "";
Method 2: S = string. valueof (I );
What are the differences between the two methods? Is the role the same? Is it interchangeable under any circumstances?
String-> int
S = "12345 ";
Int I;
Method 1: I = integer. parseint (s );
Method 2: I = integer. valueof (s). intvalue ();
What are the differences between the two methods? Is the role the same? Is it interchangeable under any circumstances?
The answer is as follows:
Method 1: S = I + ""; // two string objects are generated.
Method 2: S = string. valueof (I); // directly use the static method of the string class to generate only one object
Method 1: I = integer. parseint (s); // directly use the static method. No redundant objects are generated, but an exception is thrown.
Method 2: I = integer. valueof (s ). intvalue (); // integer. valueof (s) is equivalent to new INTEGER (integer. parseint (s), an exception is thrown, but an object is generated.