- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Pass long parameter to an overloaded method in Java
Method overloading in a class contains multiple methods with the same name but the parameter list of the methods should not be the same. One of these methods can have a long parameter in their parameter list.
A program that demonstrates this is given as follows −
Example
class PrintValues { public void print(int val) { System.out.println("The int value is: " + val); } public void print(long val) { System.out.println("The long value is: " + val); } } public class Demo { public static void main(String[] args) { PrintValues obj = new PrintValues(); obj.print(15); obj.print(8L); } }
Output
The int value is: 15 The long value is: 8
Now let us understand the above program.
The PrintValues class is created with two methods print() in the implementation of method overloading. One of these takes a parameter of type int and the other takes a parameter of type long. A code snippet which demonstrates this is as follows:
class PrintValues { public void print(int val) { System.out.println("The int value is: " + val); } public void print(long val) { System.out.println("The long value is: " + val); } }
In the main() method, object obj of class PrintValues is created and the print() method is called two times with parameters 15 and 8L respectively where the former is an int value and the latter a long value. A code snippet which demonstrates this is as follows:
public class Demo { public static void main(String[] args) { PrintValues obj = new PrintValues(); obj.print(15); obj.print(8L); } }
- Related Articles
- How to pass a lambda expression as a method parameter in Java?
- How to pass a function as a parameter in Java
- How to pass a jQuery event as a parameter in a method?
- How to pass an object as a parameter in JavaScript function?
- How to pass an array as a URL parameter in Laravel?
- C# Program to pass Parameter to a Thread
- Overloaded method and ambiguity in C#
- Java Program to pass method call as arguments to another method
- If I change the return type, will the method gets overloaded in java?
- How to pass a 2D array as a parameter in C?
- How to pass import parameter values using SAP RFC class?
- How can I pass a parameter to a setTimeout() callback?
- Does Java support default parameter values for a method?
- Get the declared method by name and parameter type in Java
- Class declaration with a method that has a parameter in Java
