Conversion between string and int in java, javastringint
Conversion between string and int in java
Int-> String
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.
See the following:
Conversion of string and int in java-CSDN blog http://blog.csdn.net/memray/article/details/7312817/
1 how to convert a String to an 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 methods for converting strings to Double, Float, and Long are similar.
2. How do I convert an integer int to a String?
A. There are three methods:
1.) String s = String. valueOf (I );
2.) String s = Integer. toString (I );
3.) String s = "" + I;
Note: the conversion of Double, Float, and Long into strings is similar.
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 ).
Exception, but one more object will be generated
--------------------------------------------------------------------
1 how to convert a String to an 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 methods for converting strings to Double, Float, and Long are similar.
2. How do I convert an integer int to a String?
A. There are three methods:
1.) String s = String. valueOf (I );
2.) String s = Integer. toString (I );
3.) String s = "" + I;
Note: the conversion of Double, Float, and Long into strings is similar.
JAVA data type conversion
Keyword type conversion
This is an example of data type conversion in JAVA.
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 they can all be found in java api, sort out and make a backup.