/* String类型变量的使用: 1、String属于引用型数据类型,翻译为:字符串 2、声明String类型变量时,使用一对"" 3、String可以和八种基本数据类型变量做运算,且运算只能是连接运算:+ 4、运算的结果仍然是String类型 */
/* 练习1 String str1=4; //判断对错:no.原因:声明String类型变量时,使用一对"" String str2=3.5f+""; //判断str2对错:yes System.out.println(str2); //输出:“3.5” System.out.println(3+4+“Hello!”); //输出:7Hello! System.out.println(“Hello!”+3+4); //输出:Hello!34 System.out.println(‘a’+1+“Hello!”); //输出:98Hello! System.out.println(“Hello!”+‘a’+1); //输出:Hello!a1 练习2 判断是否能通过编译 1、short s=5; s=s-2; //判断:no:2是int类型 2、byte b=3; b=b+4; //判断:no:4是int类型 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:应该是int类型 / class StringTest{ public static void main(String[] args){ String s1=“Hello World”; System.out.println(s1); String s2=“a”; String s3=""; System.out.println(s2); System.out.println(s3); //char c=’’;//编译不通过,char类型里面需要放字符 //********************************************* 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
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'+'*');//93
System.out.println('*'+"\t"+'*');//* *
System.out.println('*'+'\t'+"*");//51*
System.out.println('*'+('\t'+"*"));//* *
//***************************************
//String str1=123;//编译不通过,要加英文格式双引号
String str2=123+"";//"123"
System.out.println(str2);
//int num2=str2;//编译不通过,String类型无法转换成int类型
//int num2=(int)str2;//编译不通过,String类型无法转换成int类型
int num2=Integer.parseInt(str2);
System.out.println(num2);//123
}
}
|