Trinocular mixes different data clutter knowledge

Summary of different types of trinoculars

Integer and floating point data types mixed

Integers are directly aligned with floating point

 

Mixed between basic data types byte char short int long

1. Whether the data types are the same, the same return type is determined

2. The data types are different, depending on whether the two types can be recognized during compilation.

3. If it is not recognized, take the largest type

4. Able to identify two types of ranges, look at the big picture

5. If the range is over, choose the larger type, otherwise choose the smaller

        
Not all basic data types

1. xx? int: Object; find the largest parent class--Object

2. xx? int: Integer; Integer unboxed into int

Summary : When trinocular operations involve basic data types and packaging classes, the basic data types are used as standard output by default, and the box is unboxed. When basic data and non-packaged object references are involved, the largest parent object is used as the output. System.out.println(map == null? 1L: map.get("a"));//Involving basic types and references ~ Different types find the largest parent object Object, unboxing the same type

 

Attach the code directly:

import java.util.HashMap;
import java.util.Map;

public class Test {
	public static void main(String[] args) {
		test(2, 3);
		
//		三目不同类型混合总结:
//		整型直接向浮点看齐
		
//		基本数据类型byte char short int long:
//		1、数据类型是否相同,相同返回类型确定
//		2、数据类型不同,看编译期能否识别两个类型大小范围
//		3、不能识别则取最大类型
//		4、能识别 看两个类型范围,大朝小看
//		5、范围超,取大类型,否则取小
		
//		非全是基本数据类型:
//		1、xx? int : Object;找最大父类--Object
//		2、xx? int : Integer;Integer拆箱成int
//		总结:三目运算涉及基本数据类型和包装类时,会默认以基本数据类型为标准输出,拆箱。
//		涉及基本数据和非包装类对象引用时,会以最大父类Object为输出
//		System.out.println(map == null ? 1L: map.get("a"));//涉及基本类型和引用~不同类型找最大父类Object,相同类型拆箱
	}
	
	public static void test(int i, int i2) {
		System.out.println(i == i2 ? 9.9 : 9);//输出9.0 整型和浮点型,直接向浮点数看齐
		System.out.println(i == i2 ? 'a' : 98);//输出b 编译器可以识别类型大小范围,因为98在char范围内,向char看齐将98转为b
		System.out.println(i == i2 ? 'a' : Integer.MAX_VALUE);//输出2147483647 超过了char范围,向大类型int看齐
		System.out.println(i != i2 ? 'a' : i);//输出97 不能识别类型大小范围,直接向大类型int看齐
		
		//不同类型,1L : Object 输出null
		Map<String,Object> map2 = new HashMap<>();
		map2.put("b", 1L);
		System.out.println(map2 == null ? 1L : map2.get("a"));
		
		//相同类型,1L : map.get 输出报错 null转为Long异常
		Map<String,Long> map = new HashMap<>();
		map.put("b", 1L);
		System.out.println(map == null ? 1L : map.get("a"));
	}

}

operation result:

 

 

 

Guess you like

Origin blog.csdn.net/qq_41055045/article/details/102913676