0
点赞
收藏
分享

微信扫一扫

+表示运算符与连接符的详细用法

殇感故事 2022-04-21 阅读 60
eclipsejava

详解在代码注释里,有些解释可能不够规范严谨但通俗易懂

public class PlusTest 
{
	public static void main(String[] args) 
	{
		
		//***********************
		//练习1(通过猜测输出结果判断哪个+是连接运算符,哪个是加法运算符)
		char c = 'a';//97   A:65
		int num = 10;
		String str = "hello";
		System.out.println(c + num + str);//107hello
//c与num均含数字,c自动进行类型提升从char类型变为int型,c + num中间的+表示加法,两者之和为97+65=107,第二个+后面的str为String类型
//无法变化为数字,此时+表示连接号,连接107与hello
		System.out.println(c + str + num);//ahello10
//c没有与数字相连,c无法自动进行类型提升从char类型变为int型,两个+号均表示连接号,
		System.out.println(c + (num + str));//a10hello
//有括号先判断括号里面的,c没有直接与数字相连,c无法自动进行类型提升从char类型变为int型,两个+号均表示连接号
		System.out.println((c + num) + str);//107hello
	}
}

运行结果如下便于代码理解

如果文章有用点个赞就是对小编最大的支持啦,有问题也欢迎评论区留言

举报

相关推荐

0 条评论