Two Laptop Bag*
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "~ operator" Watch "~ operator" New topic
Author

~ operator

sthorat
Greenhorn

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;
System.out.println(~a);
}
}
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.
sthorat
Greenhorn

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: ~ operator
 
Similar Threads
convert Postiive number to negative..
Picking variables with a given propability
Interview Question
Iterations - finding the same value for 2 variables
can any one explain this