0

So I created a custom linkedlist and while testing it to ensure functionality, I noticed something weird.

When I run this code:

    LinkedList<Integer> list = new LinkedList<>();
    
    System.out.println(list.isEmpty());
    
    list.add(13);
    list.add(423);
    list.add(23);
    list.add(022);
    list.add(122);
    list.add(25);
    
    System.out.println(list.get(1));
    
    System.out.println(list.isEmpty());
    
    for (Integer i: list) {
        System.out.println(i);
    }

I get this output:

true 423 false 13 423 23 18 122 25

Why is it that 022 got converted into 18?

  • 4
    Does this answer your question? [Why int j = 012 giving output 10?](https://stackoverflow.com/questions/23039871/why-int-j-012-giving-output-10) – Sudhir Ojha Jul 30 '20 at 06:01

1 Answers1

2

Because it's taken as an octal base (8) since that numeral has 0 in leading. So, it's corresponding decimal value is 18.

022

= (2 * 8 ^ 0) + (2 * 8 ^ 1)
= (2 * 1) + (2 * 8)
=  2 + 16
=  18  
Kaustubh Khare
  • 3,280
  • 2
  • 32
  • 48