
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
method overloading and type promotion in Java
Method overloading helps to create multiple methods with the same name to do similar action on a different type of parameters.
We can use type promotion in case variables are of similar type. Type promotion automatically promotes the lower range value to higher range value. For example, byte variable can be assigned to an int variable. Here byte variable will be type promoted to int. In case, we want to add two numbers which can be byte, short or int, we can use a single method. See the example below −
Example
public class Tester { public static void main(String args[]) { Tester tester = new Tester(); byte a = 1, b= 2; short c = 1, d = 2; int e = 1, f = 2; System.out.println(tester.add(a, b)); System.out.println(tester.add(c, d)); System.out.println(tester.add(e, f)); } public int add(int a, int b) { return a + b; } }
Output
3 3 3
- Related Questions & Answers
- Method overloading in Java
- Method Overloading and null error in Java
- Method Overloading and Ambiguity in Varargs in Java
- Using Method Overloading in Java
- Method overloading with autoboxing and widening in Java.
- Function overloading and return type in C++
- What is method overloading in Java?
- Method overloading v/s method overriding in Java
- What is the difference between method overloading and method hiding in Java?
- What is the difference between method overloading and method overriding in Java?
- Why is method overloading not possible by changing the return type of the method only in java?
- Java Numeric Promotion in Conditional Expression
- Constructor overloading in Java
- Overloading in java programming
- What are the restrictions placed on method overloading in java?
Advertisements