/*
String类型变量的使用
- String属于引用数据类型,翻译为:字符串
- 声明String类型变量时,使用一对""
- String可以和8种基本数据类型变量做运算,且运算只能是连接运算:+
- 运算的结果仍然是String类型
*/
class StringTest {
public static void main(String[] args) {
String s1 = "Hello World!";
System.out.println(s1);
String s2 = "a";
String s3 = "";
//char c = '';//编译不通过
//***********************
int number = 1001;
String numberStr = "学号:";
String info = numberStr + number;// +:连接运算
boolean b1 = true;
String info1 = info + b1;// +:连接运算
System.out.println(info1);
//***********************
//练习1
char c = 'a';//97 A:65
int num = 10;
String str = "hello";
System.out.println(c + num + str);//107hello
System.out.println(c + str + num);//ahello10
System.out.println(c + (num + str));//a10hello
System.out.println((c + num) + str);//107hello
System.out.println(str + num + c);//hello10a
//练习2
//* *
System.out.println("* *");
System.out.println('*' + '\t' + '*');
System.out.println('*' + "\t" + '*');
System.out.println('*' + '\t' + "*");
System.out.println('*' + ('\t' + "*"));
//***********************
//String str1 = 123;//编译不通过
String str1 = 123 + "";
System.out.println(str1);//"123"
//int num1 = str1;
//int num1 = (int)str1;//"123"
int num1 = Integer.parseInt(str1);
System.out.println(num1);//123
}
}
一个字符串可以串接另一个字符串,也可以直接串接其他类型的数据。例如:
str = str + “xyz” ;
int n = 100;
str = str + n;
通常,字符串不能直接转换为基本类型,但通过基本类型对应的包装类则可以实现把字符串转换成基本类型。 如: String a = “43”; int i = Integer.parseInt(a);
boolean类型不可以转换为其它的数据类型。
判断是否能通过编译
1)short s = 5;
s = s-2; //判断:no
2) byte b = 3;
b = b + 4; //判断:no
b = (byte)(b+4); //判断:yes
3)char c = ‘a’;
int i = 5;
float d = .314F;
double result = c+i+d; //判断:yes
4) byte b = 5;
short s = 3;
short t = s + b; //判断:no