Conversion of string and int in Java

Source: Internet
Author: User

Conversion of string and int in Java

String int

int i=12345;
String s= "";
Core: s=i+ "";

String-int

S= "12345";
int i;
Core:I=integer.parseint (s);

 1  package   Com.fry.util;  2  3  public  class   Transfer { 4  public  void   Stringtoint () { 5  String id= "20171091" ;  6  int  n= Integer.parseint (ID);  7   SYSTEM.OUT.PRINTLN (n);  8  }  9  }

The test result of this method is 20171091.

The following references:

string and int in Java convert each other-csdn blog http://blog.csdn.net/memray/article/details/7312817/

1 How do I convert string strings to integer int?

A. There are two methods:

1). int i = Integer.parseint ([String]); Or

i = Integer.parseint ([string],[int radix]);

2). int i = integer.valueof (my_str). Intvalue ();

Note: The string is converted to Double, Float, and Long in a similar way.

2 How do I convert an integer int to a string string?

A. There are three ways:

1.) String s = string.valueof (i);

2.) String s = integer.tostring (i);

3.) String s = "" + I;

Note: Double, Float, Long turn into a string in a similar way.

String int

int i=12345;
String s= "";
The first method: s=i+ "";
The second method: S=string.valueof (i);
What is the difference between these two methods? Does it work the same? Are they interchangeable under any circumstances?

String-int

S= "12345";
int i;
The first method: I=integer.parseint (s);
The second method: I=integer.valueof (s). Intvalue ();
What is the difference between these two methods? Does it work the same? Are they interchangeable under any circumstances?

Here's the answer:

The first method: s=i+ ""; produces two string objects
The second method: S=string.valueof (i); Use static methods of the string class directly, producing only one object

The first method: I=integer.parseint (s);//Use static methods directly, do not produce extra objects, but throw exceptions
The second method: I=integer.valueof (s). Intvalue ();//integer.valueof (s) equals the new Integer (Integer.parseint (s)), and also throws

Exception, but produces more than one object

--------------------------------------------------------------------

1 How do I convert string strings to integer int?

A. There are two methods:

1). int i = Integer.parseint ([String]); Or
i = Integer.parseint ([string],[int radix]);

2). int i = integer.valueof (my_str). Intvalue ();

Note: The string is converted to Double, Float, and Long in a similar way.
2 How do I convert an integer int to a string string?
A. There are three ways:

1.) String s = string.valueof (i);

2.) String s = integer.tostring (i);

3.) String s = "" + I;

Note: Double, Float, Long turn into a string in a similar way.

Java Data type conversions

Keyword type conversions

This is an example of the conversion of data numbers in Java. For everyone to learn

Package cn.com.lwkj.erts.register;
Import Java.sql.Date;
public class Typechange {
Public Typechange () {
}
Change the string type to the int type
public static int Stringtoint (String intstr)
{
Integer integer;
Integer = integer.valueof (INTSTR);
return Integer.intvalue ();
}
change int type to the string type
public static String inttostring (int value)
{
Integer integer = new integer (value);
return integer.tostring ();
}
Change the string type to the float type
public static float Stringtofloat (String floatstr)
{
Float floatee;
Floatee = float.valueof (FLOATSTR);
return Floatee.floatvalue ();
}
Change the float type to the string type
public static String floattostring (float value)
{
float floatee = new float (value);
return floatee.tostring ();
}
Change the string type to the Sqldate type
public static Java.sql.Date stringtodate (String datestr)
{
Return java.sql.Date.valueOf (DATESTR);
}
Change the Sqldate type to the string type
public static String datetostring (Java.sql.Date datee)
{
return datee.tostring ();
}

public static void Main (string[] args)
{
Java.sql.Date Day;
Day = Typechange.stringtodate ("2003-11-3");
String strday = typechange.datetostring (day);
System.out.println (Strday);
}

}
Common data type conversion functions in Java
Although all can be found in the Java API, tidy up to make a backup.

Conversion of string and int in Java

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.