-
Confused about toBinaryString
Based on this documentation, it seem to me that toBinaryString is applied to an integer and takes an integer as an argument. "5.toBinaryString(2);"? I don't understand why it needs two integers to work with.
Thanks in advance for any clarification.
Edit: I have another assumedly simple question to throw in with this. I'm trying to make my own binary and back converting functions. What does it mean when you get an error saying something can't be "dereferenced?" Here is my offending code portion.
int BinaryToInt(String binary) {
int integer=0;
for (int i=0; i<binary.length(); i++)
if (binary.charAt(i).equals("1")) integer+=Power(2, i);
return integer;
}
Thanks again.
-
correction:
toBinaryString is a method of OBJECT Integer, that takes a parameter of PRIMITIVE type int
int and Integer are very different things!
int is a java primitive, 4 bytes big, that hols a single number
Integer is a java object, representing an int, and having utility methods for the same
Integer.toBinaryString(int) is a utility method to take a primitive int, and generate a String in binary representation of that int, hence
Integer.toBinaryString(254); would make:
"1111 1110"
remember Integer and int are not the same thing! 
similarly, the primitive types:
boolean short byte char long double float
have object representations for added functionality:
Boolean Short Byte Character* Long Double Float
note the different name with Character/char and Integer/int
if it starts with a capital letter, its an object. Strings are objects, hence you can call methods on them:
"hello world!".toUpperCase();
primitives are NOT objects, you cant call methods on them:
10.toString(); //NO! 
use primitives wherever you can, not Integers, you cant add two Integers together, but you can add two ints
-
Wow, you're helpful. Thanks for the clarification.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks