Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

isn't this strange?

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test {
public static void main(String[] args) {
byte b = 127;
int i= ~b;
byte b2= ++b;
System.out.println("i= " + i);
System.out.println("b2= " + b2);
System.out.println(i == b2); //true
System.out.println(~b == ++b); //false
}
}
For the above program, I think i== b2 and ~b==++b should both output true, but why it's not? Please help.
 
Bin Wang
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
opps, I think I know what's wrong here. Anyway, it's tricky, at least for me. :-)
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Can you post the answer? It is tricky for me too.
Vanitha
 
Bin Wang
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please see the comments:
public class Test {
public static void main(String[] args) {
byte b = 127;
int i= ~b; //i = -128 and b is still a byte with value 127
byte b2= ++b; //b2 = -128, b is -128, they are both byte
System.out.println("i= " + i);
System.out.println("b2= " + b2);
System.out.println(i == b2); //true because -128 == -128
System.out.println(~b == ++b); //false becasue 127 != -127
}
}
Hope this help and hope the real exam will not have this kind of nonsense question.
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply.
Vanitha
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please explain these two lines even more
int i= ~b; //i = -128 and b is still a byte with value 127
byte b2= ++b; //b2 = -128, b is -128, they are both byte
Thanks in advance
Lusha
 
Bin Wang
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think that's the Java rule.
byte b=127, then ~b = -128 and ++b = -128
The same rule also apply to int:
~(largest int) = (smallest int)
++(largest int) = (smallest int)
 
reply
    Bookmark Topic Watch Topic
  • New Topic