Павел Иванов
Павел Иванов

Reputation: 1913

How is the plus operator works in case of calculating integer with object?

guys!

How is the operator + implemented and works in Java internally in case when we add integer with Integer object?

package ru.systemres.ru;

public class Main {
    public static void main(String[] args) {
        Integer iOb = new Integer(10);
        int a = iOb + 5;
        System.out.println(a);

    }
}

Is it overloaded? Can you show me some source code from jvm which works with it? Thanks!

Upvotes: 1

Views: 1146

Answers (3)

vijayraj34
vijayraj34

Reputation: 2415

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes.

During Unboxing (Object(Integer) --to--> Primitive(int))

Implicitly Integer.intValue() is called to return int value.

Please Refer: https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html

Upvotes: 2

Vasu
Vasu

Reputation: 22432

Is it overloaded? Can you show me some source code from jvm which works with it?

It is NOT overloaded, rather the Integer iOb object will be unboxed to int first and then added with 5 to the variable a.

I suggest you refer here for Autoboxing and Unboxing in Java.

Upvotes: 0

Mureinik
Mureinik

Reputation: 311478

iOb isn't just any old object - it's an Integer. When you use it in such a context, it's outboxed to an int, and then the calculation is performed.

Upvotes: 0

Related Questions