The difference between "+=" and "=+" in java

The difference between "+=" and "=+", see example

First:

		float m=1.0f;
		int n=1;
//		Eclipse会检查出错误,Type mismatch: cannot convert from float to int
//		System.out.println(n=n+m);
		System.out.println(n+=m);

The first output reports an error (annotated, n is int, m is float, implicit conversion is not feasible), the
second output does not report an error (+= will force conversion)

the second

		short m=1;
		int n=1;
		System.out.println(n=n+m);
		System.out.println(n+=m);

No error is reported (all from low to high, which can be implicitly transferred)

The third

		short m=1;
		int n=1;
//		System.out.println(m=m+n);
		System.out.println(m+=n);

The first output reports an error (annotated, m is short, n is int, implicit conversion does not work) the
second output does not report an error (+= forced conversion)

Conclusion: High types cannot be implicitly assigned to low types in java. If you want to transfer, you must force transfer

Type level (picture source how2j):

Insert picture description here

The relationship between long and float

Insert picture description here

Reasons for more advanced float

https://blog.csdn.net/weixin_44296929/article/details/106902749

the fourth

		String m="1";
		int n=1;
		
		//The operator += is undefined for the argument type(s) int, String
//		System.out.println(n+=m);
		//Type mismatch: cannot convert from String to int
//		System.out.println(n=n+m);
		
		System.out.println(m+=n);
		System.out.println(m=m+n);
	

The first two comments are all wrong, but the
latter two are fine!
Why is the second one not forcibly transferred? Because String is not a fucking basic type! ! !

Guess you like

Origin blog.csdn.net/root_zhb/article/details/108404904