DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2004

    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.

  2. #2
    Join Date
    Feb 2004

    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
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  3. #3
    Join Date
    Jan 2004
    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
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.