Type conversion of int, string in Java

xiaoxiao2021-03-05  21

This article is collected in a technical forum, whether it is a problem or an answer, but it is complete and detailed, so it is recommended here to beginners.

Int-> string

INT i = 12345; string s = ""; first method: s = i ""; second method: s = string.Valueof (i); What is the difference between these two methods? Is the same thing? Is it interchangeable?

String -> int

S = "12345"; INT i; first method: i = integer.parseint (s); second method: i = integer.Valueof (s) .intValue (); What is the difference between these two methods? Is the same thing? Is it interchangeable?

The following is the answer:

The first method: s = i ""; // will generate two String objects second method: s = String.Valueof (i); // Directly use the static method of the String class, only one object

The first method: i = integer.Parseint (s); // Directly use the static method without producing an extra object, but will throw an exception: i = integer.Valueof (s) .intValue () // Integer.ValueOf (s) is equivalent to New Integer (INTEGER.PARSEINT (S)), it will also throw it, but will generate an object.

转载请注明原文地址:https://www.9cbs.com/read-38523.html

New Post(0)