1

Here are my 2 examples:
1. Directly assigned value to long data type:

long a = 12243221112432;

I get an error:

integer number too large

  1. But when assisgned like this:

    ArrayList<Integer> al = new ArrayList<Integer>();
    al.add(12); 
    al.add(24);
    al.add(32);
    al.add(21);
    al.add(11);
    al.add(24);
    al.add(32);
    
    long a=0;
    
    for(int i=0; i<al.size(); i++){
        a = a*100 + al.get(i);
    }
    System.out.println(a);
    

    I get this output:

    12243221112432

Why doesn't java throw an error in second example?
It isn't allowing to assign large value directly(example 1) but indirectly(example 2) it stores it and also allows me to use it too!

What is the reason for this to occur?
Is it because i am using integer in arraylist or something else?

UPDATE
Why is the large value stored in 'long a' in second example without using literal L? It should have given me an error during 5th or 6th iteration of for loop...

Note
My question is not regarding the first example... I am asking why it worked for the second example... Dont mark the question duplicate, since the other questions do not have my answer..stated above

Dhyey Shah
  • 582
  • 7
  • 23
  • 6
    This is because `12243221112432` is interpreted as `int` by the compiler. Use `12243221112432L`. – Jai Jul 06 '18 at 09:17
  • 1
    Because long and Integer are different things. 'long' is a primitive, and 'Integer' is an object, with different storage usage – jr593 Jul 06 '18 at 09:17
  • 3
    @jr593 Absolutely wrong. This has nothing to do with int vs Integer or long vs Long. It is simply about the fact that a number literal **without** the letter l/L in it ... goes as **int** literal, not as long. And that means that "too large" numbers give you an error. – GhostCat Jul 06 '18 at 09:24
  • 1
    And hint: it is always good practice to first put error messages into your favorite search engine. Rest assured: most problems you can dream of running into, other newbies have been there before, and therefore there are many solutions available to you at your fingertips. Before spending 15 minutes to write up a (nicely written!) question ... do a bit of research. – GhostCat Jul 06 '18 at 09:26
  • @GhostCat i had written the question especially considering my second example, which allows to store value without using any literal...Either it should have given me an error while parsing loop for 5th or 6th time ...but it didnt – Dhyey Shah Jul 06 '18 at 11:25
  • 1
    Not at all. 12243221112432 is a valid **long** number. But when you express it in as literal, it needs the L character. it is perfectly fine to **compute** a long that results in 12243221112432. Your only problem is that number literals are by default of type int. And of course, when you use a number that is too large for the int type, you have a problem. – GhostCat Jul 06 '18 at 11:31
  • 1
    In the 2nd case, the arithmetic is performed on `long`s. When you do `a*100 + al.get(i)`, the `100` and `al.get(i)` are converted to `long` because (basically) `a` has the largest type in the expression. Even if Java did throw an exception when overflow occurs, it wouldn't overflow in that case. – Radiodef Jul 06 '18 at 14:14

2 Answers2

3

For your assignment to work, you need to write it that way

long a = 12243221112432L;

This will indicate the compiler that your input is a long, not an int

DamCx
  • 1,047
  • 1
  • 11
  • 25
0

integer number too large

as the error said, the number 12243221112432 is too large for an integer, it does not says that this number cannot fit into a long

Te be able to have this number, you have to make it a long by using the l or L indice : 12243221112432L

long   : l L : 1716L
float  : f F : 3.69F
double : d D : 6936D
azro
  • 53,056
  • 7
  • 34
  • 70