Why is the following Java program giving a weird result?

I found a puzzle below in Java Puzzlers,

public class DosEquis {
    public static void main(String[] args) {
        char x = 'X';
        int i = 0;
        System.out.print(true  ? x : 0);
        System.out.print(false ? i : x); 
    }
}

I tried this code and ran it as well, but the result was not obtained according to my guess.

My output conclusion should be: XXbut in the output mode of realism:X88

I tried to understand a lot, but I could not, can someone give us an explanation? why is there another way out? since I was able to understand that the first print()one that will print the variable char x, and the second print()print the 88ASCII representation of the value in char x.if I simplify the expression of the triple operators in the second print()like this

if(false){
    System.out.print(i);
}else{
    System.out.print(x);
}

then the solution goes XX, rather strange, can someone alleviate this problem?

It would be very useful for me to understand ternary operators.

!

+4
8

, , , print.

true ? x : 0 0 char print(char). , i int, x int ( ) print(int) , . , i char, , .

- print, print(Object), :

void method(boolean b);
void method(Integer i);
...
method(cond? false:0);

, cond, , . , . Object *, method(Object).


* , : " () Test (Object & Serializable & Comparable <? > )", .

+6

, , , JLS 15.25 . :

, (. 5.1.8), , :

  • , short Short, .

  • T, T , char, (§15.28) int, T, T.

  • T, T - Byte, Short Character, (§15.28) int, U, unboxing T, U.

  • (§5.6.2), - .

. x char, i int, x int. , 88, int char x.

+3

, , . , :

System.out.print(true ? x : 0);

? char: const int

, , char, int. , 0 char, ""? char: char '. char 0. , char X.

System.out.print(false ? i : x);

? int: char, int int char, - , , int char. int , ( ). , char int. int "X" 88. , 88.

: Java Language Spec

+2

:

System.out.print(false ? i : x);

X int, char X

+1

? valTrue: valFalse. (true false). System.out.print(condition? i : x); int, true Char, false, int. System.out.print(condition? x : i); bothe Char.

+1

char, 0 a char; char. char zero = 0;

int char, char: (int)'X' 88.

, c ? "" : null, , , ().

+1

. . true vale , . Char x, int i, , ASCII- Char x.

    System.out.print(true  ? x : 0);
 /*ternary operator(compiler) consider the resulting data 
   type as char as the true Value type is char.*/
    System.out.print(false ? i : x); 
/*ternary operator(compiler) consider the resulting data 
   type as int as the true Value type is int.*/

/* but if you replace the print statement with if else condition 
 then there is no point of ternary concept at all*/
+1

, : (boolean ?, type : else type ), if else. :

  • if else , .
  • if else , , else.
  • else , , if.
  • 2 3 , .
  • , . *

:

System.out.println(true  ? x : 0); //X

X, if char, else - , 0. 0 char ( 0 65535). X 0 int, int (-2 31 2 31 -1).

4 (char).

System.out.println(true  ? x : i); //88

if - char, else - int. int, , 0 ( !).

, 2. if char else type int ( int ), else int if char, char ( i char).

int, , 2.

( , ):

System.out.println(false ? i : x); //88 (as per point 3)
System.out.println(false ? 0 : x); //X  (as per point 4)

:

final int i = 0;
System.out.println(true  ? x : i); //X
System.out.println(false ? i : x); //X

, ?

* Note. All types can ultimately be converted to Objectlossless, including primitive types that can be automatically loaded (in recent versions of Java). If the method is overloaded to accept a type Object, you will never be able to experience this case, since the compiler will convert the final type to Object.

+1
source

Source: https://habr.com/ru/post/1547699/


All Articles