This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming 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
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

assigning boolean to integer.

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We get 4 after compiling this program.
I can not understand what is happening on line 1 and the following one. Can some one explain plz.
public class test {
public static void main(String args[]) {
boolean x = true;
int a;
if(x) a = x ? 1: 2;// Line 1
else a = x ? 3: 4;
System.out.println(a);
}
}

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got an output of 1. Since x is true, this executes:
a = x ? 1: 2;
Since x is already true, the first value will have to be assigned to a. Thus, a = 1.
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by ZEESHAN AZIZ:
We get 4 after compiling this program.
I can not understand what is happening on line 1 and the following one. Can some one explain plz.
public class test {
public static void main(String args[]) {
boolean x = true;
int a;
if(x) a = x ? 1: 2;// Line 1
else a = x ? 3: 4;
System.out.println(a);
}
}


It equals:
If(x)
{
if (x)
a=1;
else
a=2;
}
else
{
if (x)
a=3;
else
a=4;
}
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi friends
plz someone explain that why is a=x not assigning the value of x to a.see the code below
boolean x = true;
int a;
if(x) a = x ? 1: 2;// Line 1
else a = x ? 3: 4;
System.out.println(a);
}
}
originaly posted by zeeeshan aziz.
the answer is 4.

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi zeeshan..
that is because first the ternary is operator is applied and the following expr is evaluated:
x?1:2
and since x is true it returns 1 which gets assigned to a, hence a=1.. my output also comes out to be 1.
hope this helps..

------------------
Hima
 
A feeble attempt to tell you about our stuff that makes us money
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!