This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java and have Dr. Raoul-Gabriel Urma & Richard Warburton on-line!
See this thread for details.
Win a copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

ternary Operator .

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Marshal
Posts: 68044
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always say where such questions come from. It looks familiar to me. As you know, 'X' is the same as (char)0x58, which comes to 88 in decimal. The Java® Language Specification should give you that answer. Yes, it does. It tells you how the type of the whole expression depends on its middle and right operands.
 
Rancher
Posts: 3570
38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In first case it picks overloaded version of println(char c), and in the second case it picks println(int i) -- you can prove by hovering mouse with pressed Ctrl (at least in Netbeans), or by tracing in debugger.

so, it similar to:





Note: overloaded method picked at compile time.

 
Ng Sharma
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Please always say where such questions come from. It looks familiar to me. As you know, 'X' is the same as (char)0x58, which comes to 88 in decimal. The Java® Language Specification should give you that answer. Yes, it does. It tells you how the type of the whole expression depends on its middle and right operands.



Thank you..!
 
Campbell Ritchie
Marshal
Posts: 68044
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mikalai Zaikin wrote:. . . hovering mouse with pressed Ctrl (at least in Netbeans), or by tracing in debugger. . . .

I think you get something similar with Eclipse. Probably also with IntelliJ.

But what NS needs to work out, preferably by going to the JLS, because I won't simply tell him, is why those particular overloadings are chosen.
 
Campbell Ritchie
Marshal
Posts: 68044
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Ng Sharma
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
own code.



Please anyone suggest me and correct me.. I'm little bit confusion.
 
Saloon Keeper
Posts: 11470
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
https://docs.oracle.com/javase/specs/jls/se13/html/jls-5.html#jls-5.6.2

5.6.2. Binary Numeric Promotion

When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order:

If any operand is of a reference type, it is subjected to unboxing conversion (§5.1.8).

Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:

  • If either operand is of type double, the other is converted to double.
  • Otherwise, if either operand is of type float, the other is converted to float.
  • Otherwise, if either operand is of type long, the other is converted to long.
  • Otherwise, both operands are converted to type int.

  • In the first case, the operands are a char and a byte, so both operands are converted to int.

    In the second case, the operands are a char and an int, so a special rule kicks in as you can see in this table: https://docs.oracle.com/javase/specs/jls/se13/html/jls-15.html#jls-15.25-400-A

    That section also says:

    The form "T | bnp(..)" is used where one operand is a constant expression of type int and may be representable in type T, where binary numeric promotion is used if the operand is not representable in type T.


    Since the value 0 is representable in type char, no binary numeric promotion is applied. The return type is simply char.
     
    Campbell Ritchie
    Marshal
    Posts: 68044
    258
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This question is so similar to your previous question that I thought the two should be merged into one threa‍d.

    What did you understand from the JLS link I gave you here? It all has to do with how the second and third operands are promoted, as in the table Stephan linked to.
     
    Campbell Ritchie
    Marshal
    Posts: 68044
    258
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Ng Sharma wrote:own code.

    The following is your own code, but I have seen the code in your original question elsewhere.

    . . .. . .

    Whether numeric promotion is or isn't performed in line 13 depends on whether b is or isn't a compile‑time constant (=constant expression) as in the second link Stephan and I both gave you. Since it isn't a compile‑time constant, it remains an int and the char is promoted. It is exactly the same reason as why you got 88 in the first post.
    Line 32 has its third operand a compile‑time constant in the range of a char, so the type of that expression becomes &'x201c;T” (= char) and the middle operand doesn't undergo promotion. It is explained in the source whence the first code came.

    Please don't write <-- in code tags. The idea of code tags is to contain code that can be compiled. If you had written a /* comment */, the code would have compiled, but you shouldn't exceed the screen's width with long lines.
     
    Ng Sharma
    Ranch Hand
    Posts: 57
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    finally i gotted thanks you again @Campbell Ritchie & @Stephan van Hulst
     
    Campbell Ritchie
    Marshal
    Posts: 68044
    258
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Ng Sharma wrote:finally i gotted

    thanks you . . .

    That's a pleasure

    That is the sort of question that might come up in the exam because it causes so much confusion. I think the best practice would be:-

    Always use the same declared type of expression for the second and third (=middle and right) operands.

     
    Friends help you move. Good friends help you move bodies. This tiny ad will help:
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!