0

My apologies in advance as I assume this has already been answered, but I can't seem to find it anywhere. I'm passing an Integer with a leading zero to a method and instead of resolving as the value passed like I expect (leading zero not an effect) the value is changing. Here is my call and method:

assertEquals("1800", job.validateTime(1800, 0400));

I test the output:

public String validateTime(Integer startTime, Integer endTime) {
        System.out.println(endTime);
}

...and the output is 256 where I would expect it to be 400 or 0400. Would someone please help me understand what is happening here? Thank you in advance!

  • 6
    A literal integer with a leading 0 is an octal (base 8) number, not base 10. The spec: https://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.1 – markspace Jan 31 '20 at 19:33
  • 1
    Integers are numbers. Real numbers do not start with `0`; if you need prepended `0`s, you need to use a `String` object. – Matt Clark Jan 31 '20 at 19:33
  • 2
    This does not compile. The method does not return a `String`. – f1sh Jan 31 '20 at 19:34
  • 6
    Does this answer your question? [Integer with leading zeroes](https://stackoverflow.com/questions/565634/integer-with-leading-zeroes) – Jordan Jan 31 '20 at 19:35
  • That makes sense. I was just wondering if there was a way to not treat this as base 8. This will be user input so I need to account for a possible leading zero. This info is helpful and I will be able to come up with a solution now. Thanks all. – Kevin Goodenough Jan 31 '20 at 21:17

0 Answers0