0

I have created an ArrayList:

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
class Main{
    static ArrayList<Long> fibo_list=new ArrayList<Long>();
    static int current_index;
    public static void main(String args[]){
        fibo_list.add(0);
        fibo_list.add(1);
        fibo_list.add(1);

The three fibo_list.add() throws the same type of error. Here's the first one:

error: no suitable method found for add(int)
    fibo_list.add(0);
             ^
method ArrayList.add(int,Long) is not applicable
  (actual and formal argument lists differ in length)
method ArrayList.add(Long) is not applicable
  (actual argument int cannot be converted to Long by method invocation conversion)
method AbstractList.add(int,Long) is not applicable
  (actual and formal argument lists differ in length)
method AbstractList.add(Long) is not applicable
  (actual argument int cannot be converted to Long by method invocation conversion)
method AbstractCollection.add(Long) is not applicable
  (actual argument int cannot be converted to Long by method invocation conversion)

Now it worked putting it like this :

fibo_list.add((long)0);
fibo_list.add((long)1);
fibo_list.add((long)1);

But why didn't it implicitly cast itself ?

3 Answers3

2

You are getting confused between casting and boxing.

When int gets converted to Integer - it's called boxing and revrese process is called unboxing.

JAVA supports autoboxing, which means your primitive will eb automatically converted to it's wrapper class when required & vice versa. E.g int -> Integer, long -> Long etc.

Java also supports casting among primitives which means int primitive can be implicitly casted to long primitive.

What you are trying to achieve is a combination of above to operations. You want to convert int to Long which will require 2 steps, which may be achieved in 2 different ways(let's assume for a moment following are possible):

Way 1:

  1. casting int to long
  2. Boxing long to Long

Way 2:

  1. boxing int to Integer
  2. casting Integer to Long

Since there is no clarity which way to choose, therefore JAVA does not allow allow any of the 2 step process as an automatic conversion.

tryingToLearn
  • 10,691
  • 12
  • 80
  • 114
1

When you try the following code:

fibo_list.add(0);
fibo_list.add(1);
fibo_list.add(1);

you are trying to add primitive integer values to a collection of Long objects. This doesn't work, and neither does boxing these values, which would yield an Integer. This also fails, because an Integer cannot be stored inside a collection of Long. However, the following code would work:

fibo_list.add(0L);
fibo_list.add(1L);
fibo_list.add(1L);

Here we are passing primitive long values, which can then be boxed to Long.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

A boxing conversion needs to be applied here, because lists contain boxed primitives, not primitives.

This conversion is described in JLS Sec 5.1.7:

Boxing conversion converts expressions of primitive type to corresponding expressions of reference type. Specifically, the following nine conversions are called the boxing conversions:

  • ...
  • From type int to type Integer
  • From type long to type Long
  • ...

But there is no boxing conversion "from int to type Long". As such, this method invocation cannot be applied, and so it's a compiler error.

Either explicitly cast to long, explicitly box using Long.valueOf(intValue) (in which a widening conversion converts the int parameter to a long), or use the L suffix on a literal.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243