File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ~ operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "~ operator" Watch "~ operator" New topic

~ operator


Joined: Dec 26, 2000
Posts: 5
Does Java allow you to use ~ operator for integer type variables?
Tom Tang
Ranch Hand

Joined: Dec 24, 2000
Posts: 133
Yes. Just test it out:
public class Try {
public static void main(String args[]){
int a=3;
output is -4( the same if you change a to byte, char, short and long ).

Sun Certified Java Programmer
sabyasachi mondal
Ranch Hand

Joined: Dec 26, 2000
Posts: 33
Thank you Tom. It's simple but mostly overlooked.

Joined: Dec 26, 2000
Posts: 5
Thanks Tom,
So ~ operator works with any integral types as opposed to floating types!!
Is that right?
Sivaram Ghorakavi
Ranch Hand

Joined: Nov 30, 2000
Posts: 56
TIP: ~i = (-i) -1
It is sorta covered in the JavaRanch Style Guide.
subject: ~ operator
jQuery in Action, 3rd edition