• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Ternary operator problem

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I could not understand why the following problem gives compilation error.

public class ObjectTest {

public static void main(String[] args) {
byte b = 0;
(true) ? b = b + 1 : b = 100;

System.out.println("Output: " + b);
}
}


Even if try to assign left hand variable it still gives problem.

Can some one explain me this problem?

Any help would be nice.

Thanks,
Vivian
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sliding this back to the scjp forum
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two things to consider:
  • First, when you perform addition using a byte and an int, the byte is widened to type int, and the result is type int. So when you try to assign the sum (an int) back to a byte variable, you will need an explicit narrowing cast.
  • Second, the ternary operator (boo ? exp1 : exp2) returns the evaluation of either exp1 or exp2, depending on whether boo is true or false. To make this a complete statement, you need to assign the expression (exp1 or exp2) to something. For example, var = boo ? exp1 : exp2; Otherwise, this is like having a line that just says 7 + 1;
  •  
    Ranch Hand
    Posts: 247
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Write the line as:
    b = (int) ((true) ? b + 1 : 100);
     
    victor kamat
    Ranch Hand
    Posts: 247
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Correction !!

    Write the line as

    b = (byte) ((true) ? b + 1 : 100);
     
    Vivian Josh
    Ranch Hand
    Posts: 112
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by marc weber:
    Two things to consider:

  • First, when you perform addition using a byte and an int, the byte is widened to type int, and the result is type int. So when you try to assign the sum (an int) back to a byte variable, you will need an explicit narrowing cast.
  • Second, the ternary operator (boo ? exp1 : exp2) returns the evaluation of either exp1 or exp2, depending on whether boo is true or false. To make this a complete statement, you need to assign the expression (exp1 or exp2) to something. For example, var = boo ? exp1 : exp2; Otherwise, this is like having a line that just says 7 + 1;

  • Thanks marc for very good and detailed explanation.

    I got the point.

    b=b+1 : b=100 will never work at expression. The result of b+1 or value 100 will be assigned to a variable which is on LHS depending on boolean expression.

    So, as Victor said in other reply, it worked when trimmed like this.

    b = (true)?(byte)(b+1):100

    Cheers,
    Vivian
     
    Vivian Josh
    Ranch Hand
    Posts: 112
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by victor kamat:
    Correction !!

    Write the line as

    b = (byte) ((true) ? b + 1 : 100);



    Thanks Victor !

    That worked
     
    Ranch Hand
    Posts: 225
    IBM DB2 Eclipse IDE Spring
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hi
    b = (true) ? b += 1 : 100;
    works perfect for your scenario .
    // in the above case b will be expanded as b = (byte)(b+1) automatically
     
    Vivian Josh
    Ranch Hand
    Posts: 112
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Gowtham Ganamukala:
    hi
    b = (true) ? b += 1 : 100;
    works perfect for your scenario .
    // in the above case b will be expanded as b = (byte)(b+1) automatically



    Yes, that too works ! Thanks
     
    marc weber
    Sheriff
    Posts: 11343
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Vivian Josh:
    ...b=b+1 : b=100 will never work at expression...


    Oddly enough, these can be made to work with some additional parentheses, an explicit cast, and an assignment of the ternary result...

    [ July 09, 2007: Message edited by: marc weber ]
     
    Those are the largest trousers in the world! Especially when next to this ad:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic