• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

operators

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What will happen if you attempt to compile and run the following code

public class TimDig{
public static void main(String argv[]){
TimDig td = new TimDig();
td.samcov();
}
public void samcov(){
int i=1;
int j=2;
if((i==20) && (j==(i=i*2))){
}

System.out.print(i);
if((i==20) & (j==(i=i*2))){}
System.out.print(i);

int x = i & 2;
System.out.print(x);
}
}

please let me know the result my answer is nt matching
 
lowercase baba
Posts: 13072
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what happened when YOU compiled and ran it?
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by anshi kohli:
please let me know the result

What do you think?
 
anshi kohli
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i thought it would be 110 but it was 122.

becase if first one is ealuated as false then it wont evaluate the second expresion .this is what i thougt
 
anshi kohli
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i got the answer correct ....122 is correct and no more doubts
 
fred rosenberger
lowercase baba
Posts: 13072
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
notice the difference between "&&" and "&" in a boolean condition. the first stops once the truth of the whole statement can be determined, and the other evaluates everything.

the last "x = i & 2" is a bitwise "and". it sets x = 2.
 
Acetylsalicylic acid is aspirin. This could be handy too:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic