java基本数据类型转换 - 迷失在爪哇世界中的专栏 - CSDNBlog

来源:百度文库 编辑:神马文学网 时间:2024/04/29 15:38:15
java基本数据类型转换1 字符串转换成数据
字符串转换成整数:
String MyNumber ="1234";
int MyInt = Integer.parseInt(MyNumber);
字符串转换成byte, short, int, float, double, long等数据类型,可以分别参考Byte, Short, Integer, Float, Double, Long类的parseXXX方法。
2 数据转换成字符串
整数转换成字符串:
int MyInt = 1234;
String MyString = "" + MyInt;
其它数据类型可以利用同样的方法转换成字符串。
3 十进制到其他进制的转换
十进制整数转换成二进制整数,返回结果是一个字符串:
Integer.toBinaryString(int i);
Integer和Long提供了toBinaryString, toHexString和toOctalString方法,可以方便的将数据转换成二进制、十六进制和八进制字符串。功能更加强大的是其toString(int/long i, int radix)方法,可以将一个十进制数转换成任意进制的字符串形式。
byte, short, float和double等数据类型,可以利用Integer或者是Long的toBinaryString, toHexString, to OctalString和toString方法转换成其他进制的字符串形式。
4 其它进制到十进制的转换
五进制字符串14414转换成十进制整数,结果是1234:
System.out.println(Integer.valueOf("14414", 5);
Integer和Long提供的valueOf(String source, int radix)方法,可以将任意进制的字符串转换成十进制数据。
5 整数到字节数组的转换
public static byte[] toByteArray(int number)
{
int temp = number;
byte[] b=new byte[4];
for (int i = b.length - 1; i > -1; i--)
{
b[i] = new Integer(temp & 0xff).byteValue();
temp = temp >> 8;
}
return b;
}
6 字节数组到整数的转换
public static int toInteger(byte[] b)
{
int s = 0;
for (int i = 0; i < 3; i++)
{
if (b[i] > 0)
s = s + b[i];
else
s = s + 256 + b[i];
s = s * 256;
}
if (b[3] > 0)
s = s + b[3];
else
s = s + 256 + b[3];
return s;
}
7 短整数与字节数组之间的相互转换
short与int之间的区别在于short是两个字节的,而int是四个字节的。因此,只需要将5 与6 中的范例程序小做改动,即可实现短整数与字节数组之间的相互转换。
8 字节数组转换成双精度浮点数
public double toDouble(byte[] b)
{
long l = 0;
Double D = new Double(0.0);
l = b[0];
l |= ((long)b[1]<<8);
l |= ((long)b[2]<<16);
l |= ((long)b[3]<<24);
l |= ((long)b[4]<<32);
l |= ((long)b[5]<<40);
l |= ((long)b[6]<<48);
l |= ((long)b[7]<<56);
return D.longBitsToDouble(l);
}
9 布尔类型转换成字符串
第一种方法是:
boolean bool = true;
String s = new Boolean(bool).toString();//将bool利用对象封装器转化为对象
s.equals("true");
/* 其中,toString方法是一个继承方法。java中所有的类都是object的继承,object的一个重要方法就是toString,用于将对象转化为字符串。*/
第二种方法是:
boolean bool = true;
String s = String.valueOf( bool );
首先,从代码长度上讲第二种方法明显要比第一种方法简洁;其次,第一种方法在转化过程中多引入了一个完全没有必要的对象,因此,相对第二种方法来说这就造成了内存空间的浪费,大大减慢了运行速度。所以,推荐使用第二种方法。
10 数字类型与数字类对象之间的转换
byte b = 169;
Byte bo = new Byte( b );
b = bo.byteValue();
short t = 169;
Short to = new Short( t );
t = to.shortValue();
int i = 169;
Integer io = new Integer( i );
i = io.intValue();
long l = 169;
Long lo = new Long( l );
l = lo.longValue();
float f = 169f;
Float fo = new Float( f );
f = fo.floatValue();
double d = 169f;
Double dObj = new Double( d );
d = dObj.doubleValue();
*****************************************************************************************************
1如何将字串 String 转换成整数 int?
A. 有两个方法:
1). int i = Integer.parseInt([String]); 或
i = Integer.parseInt([String],[int radix]);
2). int i = Integer.valueOf(my_str).intValue();
注: 字串转成 Double, Float, Long 的方法大同小异.
2 如何将整数 int 转换成字串 String ?
A. 有叁种方法:
1.) String s = String.valueOf(i);
2.) String s = Integer.toString(i);
3.) String s = "" + i;
注: Double, Float, Long 转成字串的方法大同小异.
JAVA数据类型转换     ynniebo [收藏]
关键字   类型转换
出处
这是一个例子,说的是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);
}
}
JAVA中常用数据类型转换函数
虽然都能在JAVA API中找到,整理一下做个备份。
string->byte
Byte static byte parseByte(String s)
byte->string
Byte static String toString(byte b)
char->string
Character static String to String (char c)
string->Short
Short static Short parseShort(String s)
Short->String
Short static String toString(Short s)
String->Integer
Integer static int parseInt(String s)
Integer->String
Integer static String tostring(int i)
String->Long
Long static long parseLong(String s)
Long->String
Long static String toString(Long i)
String->Float
Float static float parseFloat(String s)
Float->String
Float static String toString(float f)
String->Double
Double static double parseDouble(String s)
Double->String
Double static String toString(Double
**************************************************************************************************
string->byte
Byte static byte parseByte(String s)
byte->string
Byte static String toString(byte b)
char->string
Character static String to String (char c)
string->Short
Short static Short parseShort(String s)
Short->String
Short static String toString(Short s)
String->Integer
Integer static int parseInt(String s)
Integer->String
Integer static String tostring(int i)
String->Long
Long static long parseLong(String s)
Long->String
Long static String toString(Long i)
String->Float
Float static float parseFloat(String s)
Float->String
Float static String toString(float f)
String->Double
Double static double parseDouble(String s)
Double->String
Double static String toString(Double d)