Java octal value?

Discussion in 'App Development' started by blueraincap, Feb 16, 2017.

  1. Hi, can someone explain what is going on in the below question?

    1. class Ex1{
    2. public static void main(String[] args) {
    3. int a[] = { 1,2,053,4};
    4. int b[][] = { {1,2,4} , {2,2,1},{0,43,2}};
    5. System.out.print(a[3]==b[0][2] );
    6. System.out.print(" " + (a[2]==b[2][1]));
    7. }
    8. }

    Which is the result?

    A. true false
    B. false false
    C. false true
    D. true true
    E. Compilation fails

    Answer is D, why is the second true?
     
  2. tommcginnis

    tommcginnis

    Dammmnnnnnn.
    I dunnn-nnoooooo the answer but, I'm sure hoping to learn.

    TMc
    (who is just dipping the toe into Java *and* Python, JUST SO he can answer niggling little questions like this.......)
     
  3. H2O

    H2O

    I haven't programmed for a while, but I guess your confusion arises at line 6 (Line 5 is pretty straight forward and returns True).
    In Java, integers prefixed with '0' (like in a[2]) are interpreted as Octal.

    053 (Octal) corresponds with 43 in decimal (see below), hence line 6 also returns 'True':

    Step1: Write down the octal number:
    053
    Step 2: Multiply each digit of the octal number by the corresponding power of eight:
    0x8^2 + 5x8^1 + 3x8^0
    Step 3: Solve the powers:
    0x64 + 5x8 + 3x1
    Step 4: Add up the numbers written above:
    0 + 40 + 3 = 43.

    Hope this helps
     
    ahmedjolani and tommcginnis like this.
  4. southall

    southall

    053 in base 8 == 43 in base 10

    btw, Every time you use the value 0, eg x=0; you are using an octal literal!
     
    Last edited: Feb 16, 2017
    tommcginnis likes this.
  5. tommcginnis

    tommcginnis

    I'll be dipped.

    (The full quote is, "I'll be dipped gently in a bucket of $#!+." Thanks for that, Dad. You always knew he was *really* mad when he ripped through the entire thing..... This one, for me, was just a statement of wonder.....)

    I am jealous of you guys. *Totally* missed the "Octal" in the subject header.
    My Fortran book just fell off the bookshelf and cracked me on the head.
     
  6. Lee-

    Lee-

    I guess another way of looking at it is why do you think D isn't true? What do you think is correct? What do you think the values of a[3], b[0][2], a[2], and b[2][1] are? Since you specifically mentioned octal in the subject, my suspicion is that you are aware that:
    a[2] is 053
    b[2][1] is 43
    If you are in fact aware of this, then it seems your confusion is that you don't understand why 053 == 43 is true. Am I on the right track? The reason this is true is that this isn't comparing the string or human readable representation of the value. It is comparing the numerical value stored in the integer. The int stores the value -- how that value is input (decimal, hex, octal) is irrelevant. It's stored in the variable in the same way.

    Now if you made a class where you stored both the integer value AND the source type (ie decimal, hex, octal) and compared objects of this class, they would not be identical because of that extra bit of information. However, when comparing ints with Java in the fashion showed in the code, the "type" of the value before it was stored in the int is not of any importance -- only the actual underlying numeric value and 053 in fact represents the same numeric value as 43.
     
  7. southall

    southall

    Ultimately both are stored as binary 32bit values, 053 and 43 both = 101011 in binary.

    So when compared with == return the same result.

    You will need to know how numbers are represented in binary to understand how the ^ & | ~ << >> operators work.
     
    Last edited: Feb 16, 2017
  8. thanks, didnt know 0XX implies base of 8.