• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

when to use arguments and parameters? and what is return exactly?

 
Greenhorn
Posts: 17
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Farmers ;)

I'm only a beginner in the wonderful world of Java and I need some help please.

First problem, I don't really understand the whole concept of ( arguments and parameters ), I don't know how to use them and I don't even know when I should use them !
So I would really appreciate it if someone could give me an idea on when do we need to have arguments, and how can we use them ? syntax or anything ?

Second, this (return) thing is also a little annoying for me at this point, I mean why use the command return when I can just use ( variable = value or formula ) ? anyone care to explain ?

I'm so sorry that this had to be my first post, I just really wanna learn all about Java. And excuse my English if there were any mistakes or what so ever, English is not my native language =)

Thanks,
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sarah Mitchell wrote:First problem, I don't really understand the whole concept of ( arguments and parameters )


They're basically the same thing; although the word 'argument' is often used to mean the value of a parameter, rather than the parameter itself.

I don't know how to use them and I don't even know when I should use them !


Lots!

So I would really appreciate it if someone could give me an idea on when do we need to have arguments, and how can we use them ? syntax or anything ?

Second, this (return) thing is also a little annoying for me at this point, I mean why use the command return when I can just use ( variable = value or formula ) ? anyone care to explain ?


Both things are linked. Remember your maths? Absolutely bursting with functions, right? Like the sine of an angle: sin(θ).
Well, in Java terms, θ is the parameter, and the result of the function is what you return.

So, if you were going to write a Java method to do it, you might write something like:

I'm so sorry that this had to be my first post, I just really wanna learn all about Java. And excuse my English if there were any mistakes or what so ever, English is not my native language =)


No probs. Programming is not easy (and it doesn't get any easier either ).

Winston
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, did you study these, or their equivalents in some other tutorial or book? If not, you should, and if you did, what parts didn't you understand?

http://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html
http://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html

As for this:

why use the command return when I can just use ( variable = value or formula )


Two points:
1) If you're talking inside the method called, then sometimes its result is to just set the value of a member variable. However that's a different scenario. When you do that, you're changing the state of an object, and you want that state to persist as long as that object exists, unless somebody comes along and changes it again. For instance, in a Student object, I might store a currentGPA value. That stays with the student until something comes along to change it. Returning a value, on the other hand, a way of providing the result of some operation or querying the state of the object without actually changing it. For instance, a Person object might have a private Date birthDate member variable. If I want to know that person's age in seconds at a particular instant in time, I might call person.getAgeInSeconds(some time). The method will then use the birthDate and the given time to calculate the age. It wouldn't make sense to update the state of the Person object for that, as a person's age in seconds as of some arbitrary instant is not an attribute of that person.

2) When you do variable = forumula, if that "formula" is, for instance, x = Math.sin(y), we couldn't do that unless the sin() method returned a value.
 
Sarah Mitchell
Greenhorn
Posts: 17
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:
So, if you were going to write a Java method to do it, you might write something like:

Winston



Ok, so if I made such a code, should I assign the formula's outcome to the variable 'result' by using the equal sign '=' ? I mean something like this :


and in this case should we also use return as well ?
or it's only enough to use return ? but then how will it know in which variable to store the outcome if I have more than one variable ?






Thanks Winston, and sorry for all the questions
 
Sarah Mitchell
Greenhorn
Posts: 17
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:Also, did you study these, or their equivalents in some other tutorial or book? If not, you should, and if you did, what parts didn't you understand?

http://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html
http://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html

As for this:

why use the command return when I can just use ( variable = value or formula )


Two points:
1) If you're talking inside the method called, then sometimes its result is to just set the value of a member variable. However that's a different scenario. When you do that, you're changing the state of an object, and you want that state to persist as long as that object exists, unless somebody comes along and changes it again. For instance, in a Student object, I might store a currentGPA value. That stays with the student until something comes along to change it. Returning a value, on the other hand, a way of providing the result of some operation or querying the state of the object without actually changing it. For instance, a Person object might have a private Date birthDate member variable. If I want to know that person's age in seconds at a particular instant in time, I might call person.getAgeInSeconds(some time). The method will then use the birthDate and the given time to calculate the age. It wouldn't make sense to update the state of the Person object for that, as a person's age in seconds as of some arbitrary instant is not an attribute of that person.

2) When you do variable = forumula, if that "formula" is, for instance, x = Math.sin(y), we couldn't do that unless the sin() method returned a value.



Well, they actually give us like 12 small books for this course, they had this idea that by dividing it to small many books it's better than just giving us one big book, but actually it's more confusing now. So I got some e-books and I started reading here and there, I learned some very basic stuff but whenever I have a question I can't ask it in class, the proff always goes like "Don't rush into things, we're still not quite there yet", so it's really no use.

Thanks for the links tho, it seems like a very good and easy tutorial.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Careful with books; some books are very good, others not so good. We have some book reviews which we hope will help you.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think of calling a method as asking a question. "hey...What time is it?" or "What is the sine of 27.43 degrees"? or "what is the result set when you query THIS database with THAT sql query?"

Note that each and every question can only return one answer. I can't say "What time is it and what is Fred's dog's name?" and expect to get back "8:30 a.m. and Mamie". One question, one answer.

Now, that answer can be a rather complicated single thing...the aforementioned result set may contain hundreds or thousands of records, but they are all contained in one object (which may then have methods for accessing those records).

The reason you write methods rather than just 'in-lining' the calculation everywhere is that the methods can actually be rather complicated. You may have a method that connects to the database, passes the query, gets the results, formats them, and a bunch of other things. You don't want to have the same basic 200 lines of code written over and over again. Plus, if you write it in a single method, you can test that method extensively. Once you know it works, you KNOW it works, and you don't have to worry about it anymore. If you re-wrote they method by hand a bunch of times, you increase the changes of introducing an error somewhere.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sarah Mitchell wrote:Ok, so if I made such a code, should I assign the formula's outcome to the variable 'result' by using the equal sign '=' ? I mean something like this :


Pretty much; although getting the answer may take more than just one line.

and in this case should we also use return as well ?


Yes. Any method that is defined to return a value other than void must contain at least one return statement.

but then how will it know in which variable to store the outcome if I have more than one variable ?


Because you tell it which one to return. Despite all the advances of the last 50 years, computers are still basically stupid - they do exactly what they're told.

HIH

Winston
 
Sarah Mitchell
Greenhorn
Posts: 17
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

Careful with books; some books are very good, others not so good. We have some book reviews which we hope will help you.



Unfortunately, the book is not really our choice since it's mandatory for our class. But thank you so much for the link, it's gonna com in handy when I plan to get a 'real' book, lol.


fred rosenberger wrote:I think of calling a method as asking a question. "hey...What time is it?" or "What is the sine of 27.43 degrees"? or "what is the result set when you query THIS database with THAT sql query?"

Note that each and every question can only return one answer. I can't say "What time is it and what is Fred's dog's name?" and expect to get back "8:30 a.m. and Mamie". One question, one answer.

Now, that answer can be a rather complicated single thing...the aforementioned result set may contain hundreds or thousands of records, but they are all contained in one object (which may then have methods for accessing those records).

The reason you write methods rather than just 'in-lining' the calculation everywhere is that the methods can actually be rather complicated. You may have a method that connects to the database, passes the query, gets the results, formats them, and a bunch of other things. You don't want to have the same basic 200 lines of code written over and over again. Plus, if you write it in a single method, you can test that method extensively. Once you know it works, you KNOW it works, and you don't have to worry about it anymore. If you re-wrote they method by hand a bunch of times, you increase the changes of introducing an error somewhere.



Got your point, thanks for explaining .


Winston Gutkowski wrote:

Sarah Mitchell wrote:Ok, so if I made such a code, should I assign the formula's outcome to the variable 'result' by using the equal sign '=' ? I mean something like this :


Pretty much; although getting the answer may take more than just one line.

and in this case should we also use return as well ?


Yes. Any method that is defined to return a value other than void must contain at least one return statement.

but then how will it know in which variable to store the outcome if I have more than one variable ?


Because you tell it which one to return. Despite all the advances of the last 50 years, computers are still basically stupid - they do exactly what they're told.

HIH

Winston



OMG, I totally forgot about what I learned in the very beginning. the void methods doesn't return any value but other types does, and that's where we need to use arguments !
uffff, I kinda hate myself for forgetting this. If it wasn't for you I would be still lost about this a little bit. Again thank you so much Winston.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sarah Mitchell wrote:
OMG, I totally forgot about what I learned in the very beginning. the void methods doesn't return any value but other types does, and that's where we need to use arguments !



Partly right. A void method doesn't return any value, that is true. However,r eturning a value and taking arguments are totally independent of each other.



uffff, I kinda hate myself for forgetting this.



Don't stress out about it. It's a new way of thinking, and like anything else new that we learn, it takes practice for things to become automatic.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote: . . . Partly right. A void method doesn't return any value, that is true. However,r eturning a value and taking arguments are totally independent of each other.

. . .

At least you gave her the simple version and didn’t confuse her with really dubious classifications of methods!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:At least you gave her the simple version and didn’t confuse her with really dubious classifications of methods!



Do you now, or did you at some point, work for the government or the military?


 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote: . . . Do you now, or did you at some point, work for the government or the military? . . .

I would have to take legal advice before answering that question
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe we should bring this thread back to its official topic.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Maybe we should bring this thread back to its official topic.



I think that ball is in Sarah's court now. We haven't heard form her in a while, but as far as I can tell, all her questions have been answered.
 
Sarah Mitchell
Greenhorn
Posts: 17
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Maybe we should bring this thread back to its official topic.




Jeff Verdegan wrote:

Campbell Ritchie wrote:Maybe we should bring this thread back to its official topic.



I think that ball is in Sarah's court now. We haven't heard form her in a while, but as far as I can tell, all her questions have been answered.



I'm still here, I was reading everything my eyes catch in the last tow days, but honestly this is still complicated, when I try to link everything together I get totally lost !
For example I was taking a look at some code :



and I got confused by the following :



I mean what is this public Account() ? it's not a class header of course, it's not variables deceleration, it's not a method(at least I don't think so), so what is it ?
As far as I know it's only about assigning some values to the variables, but in this case why did they use ' this. ' ? what is that exactly ? why did we have to use it in this case? why didn't they just say holder = ""; or balance = 0.0; ? and what other cases it must be used in ?

Winston, fred, Jeff and Campbell. Thank you all for helping me and I'm so sorry if I'm asking a lot of questions that you guys know for sure it will be answered after reading some tutorials, but the fact is after taking a look at some websites including Oracle website, every tutorial and every website has it's own approach, and reading from different resources is confusing, so when I asked my tutor he advised us to stick to the class books at least for now. We submitted a request to the course coordinator to see if there is any chance to change the current books with something a little bit more organized and better explained but since she didn't replay until now I'm not very optimistic about it, so I'm trying to read from the books and that's where I need some help from the virtual world. It's been only 3 weeks for me with Java!
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That’s nicely-written code.

That’s a constructor that you quoted. You will have to go back to the tutorial.
A constructor sets up initial values for the fields when you create an object. I would suggest you should get rid of that constructor, because you don’t want to set up the values as blanks. I would suggest you replace it with a constructor which takes parameters, like this:-A real-life bank account would not have a setBalance method, otherwise you could program something like this:-So give our setBalance method private access, or get rid of it altogether and change your deposit method to this . . . and your debit method would need similar changes. Similarly you might change the account holder’s name (on marriage, for example), but you wouldn’t change the number, so you might be able to get rid of the setNumber method.
I would suggest you add a toString method. Read about to String() here. A possible example looks like thisYou won’t see the use of any of those until you try it out.For that to work you require the Account class with my constructor and an overridden toString() method.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sarah Mitchell wrote:I mean what is this public Account() ? it's not a class header of course, it's not variables deceleration, it's not a method(at least I don't think so), so what is it ?


As Campbell said, it's a constructor (you may also sometimes see the abbreviation 'ctor' used by wannabes), and it's the method that gets called when you write.
Account myAccount = new Account();

As far as I know it's only about assigning some values to the variables, but in this case why did they use ' this. ' ? what is that exactly ? why did we have to use it in this case? why didn't they just say holder = ""; or balance = 0.0; ?


They could have done, but people often put it in constructors to highlight the difference between values passed as parameters and the object's own fields (as in Campbell's example constructor) and some are so used to it that they forget that its not needed all the time.

and what other cases it must be used in ?


Basically, any time that you need an instance of the current object. For example, the String class has a method called concat() which adds a new String to the end of the current one. Well, if the new String is null, or empty, there's nothing to add, so the method might as well just return the current object, viz:(BTW, don't assume that the actual method looks anything like the above; it's just for illustration)

HIH

Winston
 
Sarah Mitchell
Greenhorn
Posts: 17
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell, Winston & Jeff,

From the bottom of my heart THANK YOU. Being humbled, patient, dealing with positive attitude and feeling obligated to help, this is what makes projects like this really valuable. And as individuals, I don't know what to say about you guys, you are really great and very nice people. Again thank you, and I wish I can repay you sometime.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sarah Mitchell wrote:From the bottom of my heart THANK YOU.


You're very welcome, and the best repayment will be for you to become a good programmer. Good luck with that.

Winston
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’re welcome
 
Of course, I found a very beautiful couch. Definitely. And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic