简体   繁体   English

为什么 System.out.println('a' == 97.0) 给出 true

[英]Why does System.out.println('a' == 97.0) give true

I know that '==' operator checks for if they exists in the same memory location in Java, but why, whrn I am comparing char and double data types, Java is giving me true ?我知道 '==' 运算符检查它们是否存在于 Java 中的同一内存位置,但是为什么,当我比较chardouble数据类型时,Java 给了我true Are they not different data types?它们不是不同的数据类型吗? And why it is not giving me a compile-time error, because according to the docs:以及为什么它没有给我一个编译时错误,因为根据文档:

If we apply == for object types then, there should be compatibility between arguments types (either child to parent or parent to child or same type).如果我们将 == 应用于对象类型,那么参数类型之间应该存在兼容性(子到父或父到子或相同类型)。 Otherwise, we will get a compile-time error.否则,我们将得到一个编译时错误。

I know that '==' operator ches for if they exists in the same memory location in Java...我知道'=='运算符是否存在于Java中的相同内存位置...

Not with primitives, only with reference types (objects).不适用于原语,仅适用于引用类型(对象)。 There are three sections for == / != in the Java Language Specificiation: Java 语言规范中有== / !=的三个部分:

  1. Numerical Equality Operators == and != 数值等式运算符==!=
  2. Boolean Equality Operators == and != 布尔等式运算符==!=
  3. Reference Equality Operators == and != 引用相等运算符==!=

double and char are both numeric, so you refer to the first one . doublechar都是数字,所以你参考第一个 It describes the steps it goes through ( conversion if necessary, promotion if necessary, value set conversion if necessary, unboxing if necessary) to take a char and a double and convert them so it can compare them numerically.它描述了它所经历的步骤(必要时转换,必要时提升,必要时值集转换,必要时拆箱)获取chardouble并转换它们以便可以对它们进行数字比较。

In Java, char and double are primitives, which are compared by value using == , not by memory location, that is only for objects (reference types).在 Java 中, chardouble是原语,它们使用==按值进行比较,而不是按内存位置进行比较,这仅适用于对象(引用类型)。

When you do 'a' == 97.0 , it is a comparison between char and double .当您执行'a' == 97.0时,它是chardouble之间的比较。 char is an unsigned integer, and when compared to a double , char undergoes a widening conversion to double . char是一个无符号整数,与double相比, char经历了扩大到double的转换。 The character 'a' corresponds to the numeric value 97, so when converted to double , it is 97.0.字符'a'对应于数值 97,所以当转换为double时,它​​是 97.0。 And given 97.0 == 97.0 is true, that is the result of the expression.并且给定97.0 == 97.0为真,这就是表达式的结果。

See also in the Java Language Specification:另请参阅 Java 语言规范:

  • 15.21.1. 15.21.1。 Numerical Equality Operators == and != 数值等式运算符 == 和 !=

    If the operands of an equality operator are both of numeric type, or one is of numeric type and the other is convertible ( §5.1.8 ) to numeric type, binary numeric promotion is performed on the operands ( §5.6 ).如果相等运算符的操作数都是数字类型,或者一个是数字类型而另一个是可转换的( 第 5.1.8 节)数字类型,则对操作数执行二进制数字提升( 第 5.6 节)。

  • 5.6. 5.6. Numeric Contexts 数值上下文

    Numeric promotion determines the promoted type of all the expressions in a numeric context.数字提升确定数字上下文中所有表达式的提升类型。 The promoted type is chosen such that each expression can be converted to the promoted type, and, in the case of an arithmetic operation, the operation is defined for values of the promoted type.选择提升类型使得每个表达式都可以转换为提升类型,并且在算术运算的情况下,该操作是为提升类型的值定义的。 The order of expressions in a numeric context is not significant for numeric promotion.数值上下文中的表达式顺序对于数值提升并不重要。 The rules are as follows:规则如下:

    1. [..] [..]

    2. Next, widening primitive conversion ( §5.1.2 ) and narrowing primitive conversion ( §5.1.3 ) are applied to some expressions, according to the following rules:接下来,根据以下规则,将加宽基元转换( §5.1.2 )和缩小基元转换( §5.1.3 )应用于某些表达式:

      • If any expression is of type double, then the promoted type is double, and other expressions that are not of type double undergo widening primitive conversion to double.如果任何表达式为 double 类型,则提升的类型为 double,并且其他非 double 类型的表达式经历扩展原始转换为 double。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么`System.out.println(null);`give“方法println(char [])对于类型PrintStream错误是不明确的”? - Why does `System.out.println(null);` give “The method println(char[]) is ambiguous for the type PrintStream error”? 为什么System.out.println(“ \\ t” + Scanner.next()); 给出错误? - Why does System.out.println( “\t” + scanner.next() ); give error? 为什么/什么时候 System.out.println(); 给出错误:方法打印(布尔)...不适用 - Why/when does System.out.println(); give error: method print(boolean)...not applicable 为什么 System.out.println(“0:00”.compareTo(“0”)); 结果是3? - Why does System.out.println(“0:00”.compareTo(“0”)); result in 3? 为什么 Java 中的 System.out.println() 会打印到控制台? - Why does System.out.println() in Java print to the console? 在 Java 中,为什么 System.out.println(“\\” \\\\“); 给出“\\”作为输出 - In Java, why does System.out.println(“\” \\“); gives ” \ as output 为什么System.out.println必须在方法内? - Why does System.out.println have to be inside a method? 为什么System.out.println()以ASCII码零终止 - Why does System.out.println() terminate at ASCII Code zero 为什么Java中的System.out.println(-1 &lt;&lt; 32)= - 1? - Why does System.out.println(-1<<32)=-1 in Java? System.out.println(true)和System.out.println(“true”)之间的区别 - Difference between System.out.println(true) and System.out.println(“true”)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM