aspose file tools*
The moose likes Beginning Java and the fly likes a method of Integer class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "a method of Integer class" Watch "a method of Integer class" New topic
Author

a method of Integer class

mert �zkaya
Ranch Hand

Joined: Jan 26, 2006
Posts: 33
Hi everyone,
I did not understand the usage purpose of toBinaryString()method.
If anyone know, please inform me.

Regards
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Purpose: the first sentence in the API for toBinaryStringg sums it up:

Returns a string representation of the integer argument as an unsigned integer in base 2.

Usage: have you tried using it? Did it do what you expected?


There is no emoticon for what I am feeling!
mert �zkaya
Ranch Hand

Joined: Jan 26, 2006
Posts: 33
Hi,
I do not understand why people use it.I do not see any advantages of its usage.If you disagree with me , please inform me.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
What do you use instead?
mert �zkaya
Ranch Hand

Joined: Jan 26, 2006
Posts: 33
What is the purpose of it???
Would you mind explaining???
mert �zkaya
Ranch Hand

Joined: Jan 26, 2006
Posts: 33
In fact, I still do not understand its purpose.
Would you help me by explaining??
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Its purpose is to return a string representation of the integer argument as an unsigned integer in base 2. (That sentence may seem familiar ) Are you asking why would anyone ever need this method? Suppose you are using an int as a bit mask. (Take a look at InputEvent's getModifiers or getModifiersEx method for an example.) Often you want to see the bit pattern in a mask to help you understand what is happening, hence the need for toBinaryString.
[ January 26, 2006: Message edited by: Jeff Albrectson ]
mert �zkaya
Ranch Hand

Joined: Jan 26, 2006
Posts: 33
Thanks for quick replies.
I also wonder that why it is not consist of 32 bits instead of a few bits.

Regards
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Because Integer (and int) are 32 bit representations. Long, too, has a toBinaryString and it is for 64 bit representations. Are you looking for some specific functionality? Is there some binary operation you are trying to do?
mert �zkaya
Ranch Hand

Joined: Jan 26, 2006
Posts: 33
No, I only wonder.

Can you make this more clear by giving a simple example??

Often you want to see the bit pattern in a mask to help you understand what is happening

Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780


Beyond this, it sounds like you could benefit from finding a good introductory book and reading about binary representation. Here is a tutorial about bitwise operators, but I think it may be too advanced for you at this point: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/bitwise.html
Adam Richards
Ranch Hand

Joined: Nov 03, 2005
Posts: 135
There are 10 kinds of people in the world: Those who understand binary, and those who don't.
mert �zkaya
Ranch Hand

Joined: Jan 26, 2006
Posts: 33
If you attempt to say the same thing again, you can make sure that something will change in your life!!!
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Adam Richards:
There are 10 kinds of people in the world: Those who understand binary, and those who don't.


LOL! Ain't that the truth!

Layne


Java API Documentation
The Java Tutorial
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by mert �zkaya:
Hi everyone,
I did not understand the usage purpose of toBinaryString()method.
If anyone know, please inform me.

Regards


The purpose of this method is to display an int value in its binary representation. Personally, I don't use this very often since I find the hexadecimal representation more useful when I am dealing with ints at the bit level. If you need more information, you should google for "number systems" and "twos compliment" to understand how integer values are represented in binary.

Layne
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

If you are asking why this method exists, it's because it comes in handy sometimes. Bit masks are used for many low-level things. it's nice to be able to see the thing easily. perhaps this method would be used in debugging to see if you have your mask set right.

perhaps you want to use it to simulate a binary clock.

perhaps you are using it to write a binary calculator for a middle school math class...

perhaps it is something you will never use. But it was once decided that enough people need to use it often enough to just make it part of the api.

and if you ever DO need it, you'll be glad it's there.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a method of Integer class