• Post Reply Bookmark Topic Watch Topic
  • New Topic

A quite silly question about method and its number of parameters in argument.  RSS feed

 
Matthew Park
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again, I should first let you know I am a totally beginner.

I tried to write my own class to test my thought but my programming ability is too short to enable it.

Now I will explain I was studying about predefined Java method and found something interesting.

In java.util.Calendar class, there is a set() method which is for setting the Calendar object to a specific date and time.

We can use it like this:



Here's my question.
Does this mean two set() methods - one using three parameters(I mean, maybe inputs? I'm not sure what should I call them yet.) and the other using two parameters - were predefined?
How can this be possible - two methods holding same method name in common while their number of inputs are different?
Could somebody explain about how this works please?
Well, I told you it might be a QUITE SILLY question.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's called method overloading.

Oh, and you can read the source of the Calendar class to see what's there; look in the file src.zip in your JDK install folder.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Park wrote:Well, I told you it might be a QUITE SILLY question.

Never apologise for asking questions, no matter how "SILLY" you might think they are.

As they say on Jeopardy: it's only easy if you KNOW the answer. And one of history's greatest thinkers (Socrates) created an entire learning system based around "silly" questions.

Winston
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In some programming languages, a method is entirely identified by its name. In those languages you couldn't have multiple methods taking different arguments (though some do allow optional arguments, so it can look as if they do).

But Java isn't like that. In Java a method is identified by its "signature", which is the name and the types of the parameters. So the method set(int, int) and the method set(int, int, int) are completely separate methods. As would be set(String, Object) if it existed. As Darryl says, this is called "method overloading".

You can have overloaded methods doing completely different things - as I said, they are different methods. But that's usually a bad idea because it will just confuse people. More commonly, they are intended to do roughly the same thing, but with different inputs.

Here's another example you've probably already used: System.out.println(x). That works regardless of whether x is an int, a double, a boolean, a String etc. If you look at the PrintStream class (which is the type of System.out) you'll find that there are lots of different versions of println() declared, for every possible type it might be given.
 
Matthew Park
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Matthew Park wrote:Well, I told you it might be a QUITE SILLY question.

Never apologise for asking questions, no matter how "SILLY" you might think they are.

As they say on Jeopardy: it's only easy if you KNOW the answer. And one of history's greatest thinkers (Socrates) created an entire learning system based around "silly" questions.

Winston


You may be right, Winston. Thank you for teaching me important lesson.
Enjoy the rest of your day!
 
Matthew Park
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:It's called method overloading.

Oh, and you can read the source of the Calendar class to see what's there; look in the file src.zip in your JDK install folder.


I looked in the file you mentioned.
I think it's a little bit tough for me to read it since I opened it with wordpad
Anyway, You'd been great help Darryl. Thank you very much!
 
Matthew Park
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:In some programming languages, a method is entirely identified by its name. In those languages you couldn't have multiple methods taking different arguments (though some do allow optional arguments, so it can look as if they do).

But Java isn't like that. In Java a method is identified by its "signature", which is the name and the types of the parameters. So the method set(int, int) and the method set(int, int, int) are completely separate methods. As would be set(String, Object) if it existed. As Darryl says, this is called "method overloading".

You can have overloaded methods doing completely different things - as I said, they are different methods. But that's usually a bad idea because it will just confuse people. More commonly, they are intended to do roughly the same thing, but with different inputs.

Here's another example you've probably already used: System.out.println(x). That works regardless of whether x is an int, a double, a boolean, a String etc. If you look at the PrintStream class (which is the type of System.out) you'll find that there are lots of different versions of println() declared, for every possible type it might be given.


Wow, this is the answer I wanted.
Thank you for your kind explanation.
Now I can understand what method overloading is.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Park wrote:I think it's a little bit tough for me to read it since I opened it with wordpad

Wordpad is a simple word processor. It is not suited for editing source code. Instead of Wordpad, use a good text editor with syntax highlighting such as Notepad++.
 
Matthew Park
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
Matthew Park wrote:I think it's a little bit tough for me to read it since I opened it with wordpad

Wordpad is a simple word processor. It is not suited for editing source code. Instead of Wordpad, use a good text editor with syntax highlighting such as Notepad++.

What a cool application! Thank you Jesper.
Notepad++ is a text editor I dreamed of.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!