BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Method Overloading with Autoboxing and Widening in Java

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

In this article, you will learn how Auto-Boxing and Auto-Widening works in method overloading in Java.

Auto-boxing: Automatic conversion of primitive data types to the object of their corresponding wrapper classes is known as auto-boxing. For example: conversion from int to Integer, long to Long, double to Double etc. are example of auto-boxing.

Widening: When data is automatically casted from a primitive data type to a larger size primitive data type then it is called widening or auto-widening. For example: automatic casting of int to long, long to float, float to double are all examples of auto-widening.

Rules for auto-boxing and widening in Java

In the above examples, we saw how auto-boxing and widening happens in a java program. Let’s list down the rules to understand when auto-boxing happens and when widening happens:

  • If you are calling a method while passing primitive data type as an argument, compiler checks whether a method with same primitive data type is available or not.
  • If no method exists then it searches for a method with primitive data type that take bigger size than the passed primitive data type. For example, if passed argument is of int type then it looks for method with long, double type etc.
  • If there is no scope for auto-widening means if there are no suitable primitive data type methods are available then it looks for auto-boxing conversions.
  • While doing auto-boxing, compiler checks if the corresponding wrapper class type method exists, if it exists then it invokes that method.
  • If no corresponding wrapper class type method present then compiler throws compile time error.

These rules can be represented in a digram like this:
Auto-boxing and auto-widening cast diagram

Auto-Boxing in Method Overloading

In the following example, we have a method myMethod() that accepts Integer wrapper class data type, there is another variation of this method that accepts Double wrapper class type. We are calling the method by passing the primitive int data type number and a primitive double type.

As you can see in the output that primitive int data type call, invoked the method with Integer type and primitive double type method call invoked the method with Double type. This confirms that in this example, auto-boxing is happening.

public class JavaExample {
  //Method that accept Integer wrapper class data type
  static void myMethod(Integer num){
    System.out.println("Integer Wrapper class data type");
    System.out.println("Passed number argument: "+num);
  }

  static void myMethod(Double num){
    System.out.println("Double Wrapper class data type");
    System.out.println("Passed number argument: "+num);
  }
  public static void main(String[] args) {
    //primitive integer data type
    int n = 100;
    myMethod(n);
    //primitive double data type
    double bigN = 1000000;
    myMethod(bigN);
  }
}

Output:
Autoboxing in method overloading

Auto-Widening in Method Overloading

In this example, one method accepts Integer type and another accept primitive double type. The call with the primitive int argument invokes the double primitive parameter method, instead of Integer type method. This confirms that in this example, auto-widening is happening. We discussed this in the beginning of this article under Rules section that compiler first looks for widening opportunities, if there are none present then it looks for auto-boxing opportunities.

public class JavaExample {
  static void myMethod(Integer num){
    System.out.println("Integer Wrapper class type");
  }

  static void myMethod(double num){
    System.out.println("double primitive type");
  }
  public static void main(String[] args) {
    //primitive int type
    int n = 100;
    myMethod(n);
  }
}

Output:

double primitive type

Compile time error when auto-boxing and widening not possible

In this example, widening not possible as no method exists that accepts larger primitive type. There is no scope for auto-boxing as no method present with Integer wrapper class type.

As discussed in rules, a primitive data type can only be casted to its corresponding wrapper class through auto-boxing. In this case, the passed argument is of int type, which means it can only be casted to Integer type through auto-boxing, which doesn’t exist in this code thus compiler throws an error.

public class JavaExample {
  static void myMethod(Long num){
    System.out.println("Long Wrapper class type");
  }

  static void myMethod(Double num){
    System.out.println("Double Wrapper class type");
  }
  public static void main(String[] args) {
    //primitive int type
    int n = 100;
    myMethod(n);
  }
}

Output:
Auto-boxing and Widening compile time error

Example: Both auto-boxing and widening occurs

public class JavaExample {
  static void myMethod(long num){
    System.out.println("long primitive type");
  }

  static void myMethod(Double num){
    System.out.println("Double Wrapper class type");
  }
  public static void main(String[] args) {
    //This will invoke the method with long primitive data type
    //Widening is happening here
    int num = 100;
    myMethod(num);

    //This will invoke the method with Double wrapper class type
    //auto-boxing is happening here
    double dnum = 1000000000;
    myMethod(dnum);
  }
}

Output:

long primitive type
Double Wrapper class type

Recommended Posts

  • Java Autoboxing and Unboxing with examples
  • Method Overloading in Java
  • Method Overloading vs Overriding in Java
  • Data Types in Java
  • Wrapper Class in Java
❮ Java Programming

Top Related Articles:

  1. Java Integer byteValue() Method
  2. Java Annotations tutorial with examples
  3. Java StringBuffer setLength() Method
  4. Java String to int Conversion
  5. Operators in Java With Examples

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap