java表达式的类型,Java如何确定三元条件运算符表达式的类型?

Can anyone explain this?

public class Test {

public static void main(String[] args) {

char c = 'A';

int i = 0;

boolean b = true;

System.out.println(b ? c : i);

System.out.println(b ? c : (char)i);

System.out.println(b ? c : 0);

System.out.println(b ? c : (char)0);

}

}

Output:

65

A

A

A

It sure looks strange from where I'm standing. I would have expected only As to print out. And moreover: how come when I substitute 0 for i the output changes? The output seems to be the same for all values of i, not just 0.

解决方案

From the Java Language Specification, about type conversion and promotion (see the text in bold)

EVERY expression written in the Java programming language has a type that

can be deduced from the structure of the expression and the types of the literals,

variables, and methods mentioned in the expression. It is possible, however, to

write an expression in a context where the type of the expression is not appropriate.

In some cases, this leads to an error at compile time. In other cases, the context may

be able to accept a type that is related to the type of the expression; as a convenience,

rather than requiring the programmer to indicate a type conversion explicitly, the

Java programming language performs an implicit conversion from the type of the

expression to a type acceptable for its surrounding context.

The type conversion in your case that happens at compile time explains the output.

In the cases where i was involved, c has been promoted to integer.

In the cases where 0 was involved, it is treated as character and hence c remained as character.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/421523.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java里的sleuth_java基础之spring cloud微服务快速教程之(十一) Sleuth(zipkin) 服务链路追踪...

0、前言微服务架构上众多微服务通过REST调用,可能需要很多个服务协同才能完成一个接口功能,如果链路上任何一个服务出现问题或者网络超时,都会形成导致接口调用失败。随着业务的不断扩张,服务之间互相调用会越来越复杂。如何清晰地…

android Activity 之间传递复杂对象

、传递Object有两种方式来传递Object:Serializable和Parcelable2.1 使用Serializable方式前提:Object需要实现Serializable接口用Serializable方式传递Object的语法:bundle.putSerializable(key,object);用Serializable方式接收Object的语法:…

opengl模板缓冲区

相信大家有些人对opengl的模板缓冲区不是很理解,包括我最开始也是,opengl的模板缓冲区其实就是采用过滤的技术来控制那些颜色可以绘制,那些不能进行绘制。这里的过滤技术也就是我们的一个控制方法,主要体现在如下两个函数glStenci…

java ui awt_java试用(3)awt,UI

importjava.awt.*;importjava.awt.event.*;publicclassMyFrameextendsFrame{privateTextField tf;publicstaticvoidmain(String args[ ]){MyFrame frnewMyFrame("Hello Out There!");fr.InitUI();}publicMyFrame (String str){super(str);//调用父类的构造方法}publi…

Linux php 中文乱码解决

在ubuntu下php网页输出乱码,在不涉及数据库编码的情况下: 修改“/etc/php5/apache2/php.ini”将 default_charset "iso-8859-1" 修改为default_charset "utf-8" 然后重启apache: sudo /etc/init.d/apache2 restart 推荐一个自己业…

vue在java中的应用_开发知识-Vue篇:在Vue应用中集成O2OA

在前面的章节中,我们介绍了两种在O2OA中使用Vue开发应用的方式,已经可以满足绝大多数的情况了。如果您考虑完全脱离O2的web服务器,自己搭建web服务器,那就请阅读本章。我们还是使用Vue的Vue-CLI工具,创建Vue应用&#…

java高性能低功耗计算_实现低功耗的高性能深度学习

对于大多数功耗敏感型的嵌入式视觉应用而言 , 搭载专用CNN引擎的视觉处理器可 能是能否满 足设计功 耗 预算的关键区别因素。选择专用CNN引擎似乎是一件理所应当的事情 , 但如何在芯片实现之前就测量 出 功 耗呢?我们假设一项应用的性能阈值对功耗预算有严格要求&am…

C++ 虚函数在基类与派生类对象间的表现及其分析

近来看了侯捷的《深入浅出MFC》,读到C重要性质中的虚函数与多态那部分内容时,顿时有了疑惑。因为书中说了这么一句:使用“基类之指针”指向“派生类之对象”,由该指针只能调用基类所定义的函数,如果要让基类的指针使用…

Android---什么是3G

学习安卓之前我们必须了解什么是3g.这几年随着IT技术的革新。移动互联网已经渐渐的占据了市场的主导地位。而3g技术是移动互联网的一方面体现。那么究竟什么是3g? 3G有三大制式,GSM升级后的WCDMA,CDMA升级后的CDMA2000,以及我国自主开发的TD…

centos7硬盘挂载mysql_centos7 挂载新磁盘

一 、挂载新磁盘查看磁盘[rootlocalhost ~]# fdisk -lDisk /dev/vda: 53.7 GB, 53687091200 bytes, 104857600 sectorsUnits sectors of 1 * 512 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk lab…

autolisp 列表 resbuf

有以下 list : (1 2 3 (4 5 6) "Properties" (("id" . 3) ("name" . "hello lisp"))) 要求用: quote cons acutBuildList acutNewRb 方式构造 转载于:https://www.cnblogs.com/pangblog/p/3331246.html

如何看懂php源程序,概述 · thinkphp5.0 源码阅读学习 · 看云

**1 章节简介**概述: 记录框架的文件,类,函数或方法等索引到文章的具体章节,方便查找。框架流程:框架流程的重要文件分析。框架工具:框架自带的重要工具文件分析。框架驱动:框架底层的驱动与扩展目录分析。…

php评论盖楼怎么实现,dedecms评论盖楼实现楼层数,类似腾讯、网易的评论(5.5/5.6版)...

DEDE评论效果:修改后的效果:修改步骤一、织梦dedecms5.5版本(5.6版本请往下看)1、修改/plus/feedback_ajax.php文件的第131行(如果你没改过)下面是修改之前的代码:$qmsg {quote}{title}.$row[username]. 的原帖:{/title}{content…

Nokia 920全部CODE

code码都是7位,我是从别的地方copy过来的,code码后面带了些乱码懒得删了,自己数7位code就是了。 RM-820 NDT AMERICA ATT White 059N5T3RM-820 NDT AMERICA ATT Yellow 059N5T5RM-820 NDT AMERICA ATT Black 059L848RM-…

datagridview绑定与详细说明 (搜集)

1、实现一个用于处理数据库教程数据检索的详细信息的方法。 下面的代码示例实现一个 getdata 方法,该方法对一个 sqldataadapter 组件进行初始化,并使用该组件填充 datatable。 然后,将 datatable 绑定到 bindingsource 组件。请确保将 conne…

matlab显示二值直方图,图像灰度变换、二值化、直方图

1、灰度变换1)灰度图的线性变换Gnew Fa * Gold Fb。Fa为斜线的斜率,Fb为y轴上的截距。Fa>1 输出图像的对比度变大,否则变小。Fa1 Fb≠0时,图像的灰度上移或下移,效果为图像变亮或变暗。Fa-1,Fb255时,发…