Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer number too large [duplicate]

Tags:

java

Hi I'm having trouble understanding why this isn't working

if(Long.parseLong(morse) == 4545454545){
     System.out.println("2");
}

Where morse is just a String of numbers. The problem is it says Integer number too large: 4545454545, but I'm sure a Long can be much longer than that.

like image 622
Strife Avatar asked Nov 29 '22 15:11

Strife


1 Answers

You need to use 4545454545l or 4545454545L to qualify it as long. Be default , 4545454545 is an int literal and 4545454545 is out of range of int.

It is recommended to use uppercase alphabet L to avoid confusion , as l and 1 looks pretty similar

You can do :

if(Long.valueOf(4545454545l).equals(Long.parseLong(morse)) ){
     System.out.println("2");
}

OR

if(Long.parseLong(morse) == 4545454545l){
   System.out.println("2");
}

As per JLS 3.10.1:

An integer literal is of type long if it is suffixed with an ASCII letter L or l (ell); otherwise it is of type int (§4.2.1).

like image 139
AllTooSir Avatar answered Dec 21 '22 02:12

AllTooSir