java基本语法之String类型变量的使用

/*
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
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值