This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
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 Building Microservices this week in the Design 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
Have you checked out Aspose?
subject: ~ operator
It's not a secret anymore!