Movatterモバイル変換


[0]ホーム

URL:


Menu
×
Sign In
+1 Get Certified For Teachers Spaces Plus Get Certified For Teachers Spaces Plus
   ❮     
     ❯   

Java Tutorial

Java HOMEJava IntroJava Get StartedJava SyntaxJava OutputJava CommentsJava VariablesJava Data TypesJava Type CastingJava OperatorsJava StringsJava MathJava BooleansJava If...ElseJava SwitchJava While LoopJava For LoopJava Break/ContinueJava Arrays

Java Methods

Java MethodsJava Method ParametersJava Method OverloadingJava ScopeJava Recursion

Java Classes

Java OOPJava Classes/ObjectsJava Class AttributesJava Class MethodsJava ConstructorsJava this KeywordJava ModifiersJava EncapsulationJava Packages / APIJava InheritanceJava PolymorphismJava super KeywordJava Inner ClassesJava AbstractionJava InterfaceJava EnumsJava User InputJava Date

Java Errors

Java ErrorsJava DebuggingJava Exceptions

Java File Handling

Java FilesJava Create/Write FilesJava Read FilesJava Delete Files

Java Data Structures

Java Data StructuresJava CollectionsJava ListJava ArrayListJava LinkedListJava List SortingJava SetJava HashSetJava TreeSetJava LinkedHashSetJava MapJava HashMapJava TreeMapJava LinkedHashMapJava Iterator

Java Advanced

Java Wrapper ClassesJava GenericsJava AnnotationsJava RegExJava ThreadsJava LambdaJava Advanced Sorting

Java How To's

Add Two NumbersCount WordsReverse a StringSum of Array ElementsConvert String to ArraySort an ArrayFind Array AverageFind Smallest ElementArrayList LoopHashMap LoopLoop Through an EnumArea of RectangleEven or Odd NumberPositive or NegativeSquare RootRandom Number

Java Reference

Java ReferenceJava KeywordsJava String MethodsJava Math MethodsJava Output MethodsJava Arrays MethodsJava ArrayList MethodsJava LinkedList MethodsJava HashMap MethodsJava Scanner MethodsJava Iterator MethodsJava Errors & Exceptions

Java Examples

Java ExamplesJava CompilerJava ExercisesJava QuizJava ServerJava SyllabusJava Study PlanJava Certificate


JavaMethod Overloading


Method Overloading

With method overloading, multiple methods can have the same name with differentparameters:

Example

int myMethod(int x)float myMethod(float x)double myMethod(double x, double y)

Consider the following example, which has two methods that add numbers of different type:

Example

static int plusMethodInt(int x, int y) {  return x + y;}static double plusMethodDouble(double x, double y) {  return x + y;}public static void main(String[] args) {  int myNum1 = plusMethodInt(8, 5);  double myNum2 = plusMethodDouble(4.3, 6.26);  System.out.println("int: " + myNum1);  System.out.println("double: " + myNum2);}

Try it Yourself »

Instead of defining two methods that should do the same thing, it is better to overload one.

In the example below, we overload theplusMethod method to work for bothint anddouble:

Example

static int plusMethod(int x, int y) {  return x + y;}static double plusMethod(double x, double y) {  return x + y;}public static void main(String[] args) {  int myNum1 = plusMethod(8, 5);  double myNum2 = plusMethod(4.3, 6.26);  System.out.println("int: " + myNum1);  System.out.println("double: " + myNum2);}

Try it Yourself »

Note: Multiple methods can have the same name as long as the number and/or type of parameters are different.




 
Track your progress - it's free!
 

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted ourterms of use,cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.


[8]ページ先頭

©2009-2025 Movatter.jp