Ad
  • Default User Avatar

    There's nothing strange here))
    I kindly advise you to refine your knowledge of how the String class and its siblings that implement CharSequence interface work under the hood.

    Quick answer: every string instance maintains an array of bytes (from Java 9, prior - an array of chars) and it's simply impossible to create an array of length less than 0 or greater than Integer.MAX_VALUE (note that there're far more strikt limits for string literals).

    So there's nothing wrong with this solution. And yep, that is good practice.
    The actual usage of long type argument in this method is pointless, you have to downcast or may do some tricks) But at the end of the day result is the same.

  • Default User Avatar

    It's really strange that it works. Cause of long casted to int may return wrong number or even negative number.

  • Default User Avatar

    At first I thought of this solution as well. However, take into account that the parameter is long for some reason. In real life applications this would be a code smell. This is not a safe cast as you have no control over the argument at runtime. At some point, a client may pass a long, as the type parameter is long, and then the casting will fail with an overflow of the long value.