---
title: "Java 条件表达式"
author: "polim"
date: "2012-06-11T11:05:55Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2012-06-11T11:05:55Z"
id: 1558192
url: "https://polim.iteye.com/blog/1558192"
---

public class SanYuanTest {

	public static void main(String args) {		
		char x = 'X';
		int i = 0;
		System.out.println(true? x : 123);
		System.out.println(true? x : i);
		System.out.println(false? i : x);
		System.out.println("------------------------");
		
		int in = 80;
		System.out.println(in<100?90:100);
		System.out.println(in<100?90:100.0);
		
		
	}
}

输出结果为:
X
88
88
------------------------
90
90.0


条件运算符规则
1、如果第2个和第三个有相同的类型，那么它就是条件表达式的类型。
2、如果一个操作数的类型是T,T为byte、short、char,而另一个操作数是一个int类型的 <span style="color:red">常量</span> 表达式时，如果条件表达式的类型为T时，结果为类型T
3、否则将对操作数类型运用2进制数字提升，而表达式的类型就是第二个和第三个操作数被提升后的类型