INT and string types in Java

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.