http://www.tudou.com/programs/view/A5Ty-oEgjqA/
转义字符等
字符中还要注意的是,有一系列转义字符。
\" \' \t’ \n
public class Hello {
public static void main(String args[]){
System.out.println("\"hello\nw\torld\"");
}
};
结果:
"hello
w orld"
题目:
一个字符能不能放下一个汉字?
能,放一个汉字。
class TestDemo1{
public static void main(String arg[]){
char c = '中';
int x = c;
System.out.print(x);
}
};
结果为20013。
布尔型
布尔是一个数学家的名字。在布尔类型中只有true false两种取值。从实际使用中,布尔类型的数据往往用于程序的控制结合,与IF等结合。
public class Hello{
public static void main(String arg[]){
boolean flag=true;
if(flag){ //if(flag==true)
System.out.print("welcome");
}
}
};
字符串精髓。String因为字母开头大写,所以是一个类,但是这个类比较特殊,可以按照一般基本数据类型来使用。
但是在使用String有一点需要注意。各个数据类型中可以互相转型,但是所有的类型只要是碰到String 则都向String转换。
public class Hello{
public static void main(String arg[]){
int s = 10;
float y = 300.3f;
System.out.print(y / s);
}
};
结果为30.2999999
public class Hello{
public static void main(String arg[]){
int s = 20;
int y = 30;
String x = "s"+"y";
System.out.print(x);
}
};
结果为sy.
public class Hello{
public static void main(String arg[]){
int x = 20;
int y = 10;
String str = "x" + "+" + "y" +"="; //字符串连接
System.out.print(str+x+y);
}
};
结果为 x+y=2010。结果不对。因为所有的变量此处都变成了字符串。如果想要解决以上的问题,则就需要加上括号,要求先进行计算。这样才能保证一个正确的操作结果。
public class Hello{
public static void main(String arg[]){
int x = 20;
int y = 10;
String str = "x" + "+" + "y" +"="; //字符串连接
System.out.print(str+(x+y));
}
};
x+y=30.
基本数据的默认值。
如果是数字,则为0.如果是字符,则为null.如果是布尔值,则为false.
但是,定义在方法体中的话,默认值是无效的。所以以后只要声明了变量,一定要给他一个初始值。
public class Hello{
public static void main(String arg[]){
int x;
System.out.print(x);
}
};
error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The local variable x may not have been initialized
at Hello.main(Hello.java:5)
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/22117706/viewspace-625976/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/22117706/viewspace-625976/