search
HomeJavaJavaBaseThe operation principle of ternary operator in java

The operation principle of ternary operator in java

Format: Data type variable name = Boolean type expression? Result 1: Result 2

Operation principle:

The result of the Boolean type expression is true, and the overall result of the ternary operator is result 1, which is assigned to the variable.

The result of the Boolean expression is false, and the overall result of the ternary operator is result 2, which is assigned to the variable.

Recommended related video tutorials: java learning

Examples are as follows:

package test;

public class Test {

	public static void main(String[] args) {
	    // 方式一
		Object o1 = true ? new Integer(1) : new Double(2.0);
		// 方式二
		Object o2;
		if (true)
			o2 = new Integer(1);
		else
			o2 = new Double(2.0);
		
		System.out.println(o1);
		System.out.println(o2);
		// 方式三
		Integer i = new Integer(1);
		if (i.equals(1))
			i = null;
		Double d = new Double(2.0);
		Object o3 = true ? i : d;    // 空指针异常
		System.out.println(o3);
	}

}

Operation results:

The operation principle of ternary operator in java

In my impression, the first and second methods should be equivalent, but the results obtained are different. It can be seen that the ternary operator will promote the type of the operand when necessary. Note: Only when needed, otherwise a null pointer exception will be thrown.

More related article recommendations:Introduction to java programming

The above is the detailed content of The operation principle of ternary operator in java. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)