010-003-012

Type Widening and Overload Resolution

Medium

Problem Description

Type Widening and Overload Resolution

In this problem, you will create a program that implements three overloaded process() methods (accepting int, long, and double), reads byte, short, int, and long values from standard input, calls the methods with each, and displays how Java's type widening automatically selects the appropriate overloaded method for each call to standard output.

Learning Objective: Understand how type widening automatically selects overloaded methods

Overview

When no exact match method exists, Java uses type widening to convert argument types to wider types and calls the matching method.

Input Format

<byte value> (-128 to 127)
<short value> (-32768 to 32767)
<int value>
<long value>

Specifications

  • process(int value): displays "int: " + value
  • process(long value): displays "long: " + value
  • process(double value): displays "double: " + value
  • Call with byte, short, int, and long values read from Scanner

Output Format

byte -> int: <byte value>
short -> int: <short value>
int -> int: <int value>
long -> long: <long value>

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?

Sign Up