Java的数据类型
3、引用类型
引用类型的用法可以用String类来说明,如String str=new String("Hello!");
总结为:类名 变量名=new 类名();
引用类型与基本类型有很多区别,主要的方面在于引用类型多半是类的类型,在变量内存空间上和基本类型的并不一样。简单的方面主要是引用类型的变量通常拥有很多内部的成员,包含一些功能相关的函数。如String类中的一些方法,如toUpperCase(),length(),substring()等。
4、基本类型与引用类型之间的相互转换
一般来说,通常很容易通过引用类型变量自身提供的成员函数将基本类型转换成引用类型,如整型转换成字符串:
public class exec
{
public static void main(String args[])
{
String str=new String("1234");
str=str+String.valueOf(1);
System.out.println(str);
}
}
输出为12341
等价的简单写法为:
public class exec
{
public static void main(String args[])
{
String str=new String("1234");
str=str+1;
System.out.println(str);
}
}
将引用类型转换成基本类型通常是通过一些成为“基本类型封装类”的函数成员来进行的,如整型封装类是Integer,浮点型封装类是Float和Double,字符封装类是Character,等。具体使用例子为:
public class exec
{
public static void main(String args[])
{
String str=new String("1234");
int i=Integer.parseInt(str)+1;
System.out.println(i);
}
}
输出为1235
再如:
public class exec
{
public static void main(String args[])
{
String str=new String("1.25");
double i=Double.parseDouble(str)*2;
System.out.println(i);
}
}
输出为2.5
[此贴子已经被作者于2010-12-12 07:31:09编辑过]