• Post Reply Bookmark Topic Watch Topic
  • New Topic

Head First Java Question  RSS feed

 
Thorall Anderson
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey All,

I'm working on a question in Head First Java 2nd edition.  I need help understanding the answer. Question is on page 88, Chapter 4 "Be the Compiler, A".

The question asks to check the following code for correctness, and state what output would be if correct. 

Now, I have the answer (it's in the book, the code compiles as is). However I'm hung up on how a part of it works.  Here is the code:


Output is '42 84'

What I don't understand is how we get the output 84.  How does arg *2 =84?

Wouldn't we need to declare a value for arg under the main in order for arg to know what's it's original value is? 

I guess I'm just not understanding in what way arg is able to plug into where the .go method is called and be given the value of orig(42). To make this work?  Hope so worded that question well enough to explain my confusion.

Please help!
Thanks so much
 
Liutauras Vilda
Sheriff
Posts: 4921
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A warm welcome to the Ranch, Thorall Anderson.

What I don't understand is how we get the output 84.  How does arg *2 =84?

Wouldn't we need to declare a value for arg under the main in order for arg to know what's it's original value is? 

I think you slightly don't get only a small fraction of information, so you get all that misunderstanding.
Well, what you would expect in order to get 84 is 42 * 2. This is indeed what it is. Now how come that arg is 42. You do have declared value of origin (=42) within main method. Now, as an implementer of method go(), you do not necessarily know, how the caller going to name a variable, which is about to be passed into method go(). So, the language designers made it so you could name your parameter within go() method in the way you want. What *IS* important is its data type and number of parameters you let to pass in.
What is important, is to know it is String data type, and the name is just a label, so the code author and presumably other readers of the code would have enough information to understand what it represents.

Probably another example would give you a better idea (ignore illegal code, i'm shortening):
Imagine if parameter I'd need to name cents or dollars, that means for each possible variable I'd need to create separate method which matches passed argument name and parameter name.

  • Arguments are those which you are passing
  • Parameters are those which you are accepting


  • Sometimes argument name and parameter name matches, sometimes not.
     
    Liutauras Vilda
    Sheriff
    Posts: 4921
    334
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Also look up on google about method signature, this is all about that if we talk in correct terminology. Method signature defines contract, what caller of the method need to comply with.
     
    Joseph Mokenela
    Ranch Hand
    Posts: 71
    Eclipse IDE Hibernate Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well said Liutauras Vilda,

    I would however want to add one point,

    Since we are working with primitives, the value of orig remains unchanged (Pass by value). This is because the copy of the variable was send to the go method.
    This would have had a different outcome if orig was an object.(Pass by reference)
     
    Dave Tolls
    Ranch Foreman
    Posts: 3061
    37
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It would make no difference if orig was a reference, as Java is not pass by reference.
    If you change the value of the parameter reference variable inside the method it would still not be reflected in the value of orig, in exactly the same way that the change to the int is not reflected.
     
    Liutauras Vilda
    Sheriff
    Posts: 4921
    334
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Joseph Mokenela wrote:This would have had a different outcome if orig was an object.(Pass by reference)

    As Dave already mentioned to you, that's not quite right, at least not in full in case you meant what I think you might meant.

    You can change objects state using its reference (this is what you meant maybe, but in such case that is different topic), but you cannot re-assign original reference itself (about what we are talking here).
     
    Joseph Mokenela
    Ranch Hand
    Posts: 71
    Eclipse IDE Hibernate Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I slightly deviated from the original context of the question. I just wanted to highlight the scenario where we had passed the object reference. Not entirely in the same structure of the question though.

    Apologies for the confusion.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!