Results 1 to 11 of 11
  1. #1
    Victor323 is offline Member
    Join Date
    Sep 2014
    Posts
    2
    Rep Power
    0

    Default Help me Understand

    public class ParameterMystery {
    public static void main(String[] args) {
    String x = "java";
    String y = "tyler";
    String z = "tv";
    String rugby = "hamburger";
    String java = "donnie";

    hamburger(x, y, z);
    hamburger(z, x, y);
    hamburger("rugby", z, java);
    hamburger(y, rugby, "x");
    hamburger(y, y, "java");

    }

    public static void hamburger(String y, String z, String x) {
    System.out.println(z + " and " + x + " like " + y);
    }

    }


    The output is :
    tyler and tv like java
    java and tyler like tv
    tv and donnie like rugby
    hamburger and x like tyler
    tyler and java like tyler





    So my homework task was to find the output, which i did ( pretty easy when you can just plug the formula into a compiler), but i dont understand WHY the output is what it is. Please help me understand how i got this output. Thank you all in advance.
    Last edited by Victor323; 09-27-2014 at 06:14 AM.

  2. #2
    Victor323 is offline Member
    Join Date
    Sep 2014
    Posts
    2
    Rep Power
    0

    Default Re: Help me Understand

    Please keep in mind that this is an intro class, and the extent of my knowledge with Java is veeeery limited.

  3. #3
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    168
    Rep Power
    7

    Default Re: Help me Understand

    Could you put the code into the CODE tags, so we can see it better? If you go into advanced mode and press the CODE tags button, that will put whatever you put between them in a more readable format. Thanks

  4. #4
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    272
    Rep Power
    7

    Default Re: Help me Understand

    Your confusion may pertain to the order of arguments in method hamburger. The first argument is printed at the end, the second in first, and the third in the middle of the sentence.

    It actually is quite confusing due to the order, better to put them in order.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Help me Understand

    Quote Originally Posted by superhaNds View Post
    It actually is quite confusing due to the order...
    That's probably the point. Usually these types of problems are given on a test and the student has no time to
    change much of the code or even compile and run it. It is an exercise in understanding how like named variables
    are mapped to different positions in a method call. May require working out on paper.

    Regards,
    Jim
    Last edited by jim829; 09-28-2014 at 07:49 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Help me Understand

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Help me Understand

    First thing to help the understanding of the code is to rename the variables, x,y,z are poor names.
    For example try these:
    Java Code:
    public static void hamburger(String firstStr, String secondStr, String thirdStr) {
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Help me Understand

    Does it help to understand that the names of the variables are irrelevant. Only the positions of the variables
    in the method call and method signature are important.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Help me Understand

    Sorry, the names are important so that you can easily see what the code is doing when it uses the args' values. When you see firstStr you know it is the first arg. What do you think when you see z? Yes you can trace it back with each arg, but with proper names, the chance of making a mistake is lower.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Help me Understand

    Actually, I was talking about the fact that z in the method call is not necessarily related to to z in the method signature. It is
    only related to the variable in the same position.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    dcarter's Avatar
    dcarter is offline Student
    Join Date
    Sep 2014
    Location
    NC
    Posts
    14
    Rep Power
    0

    Default Re: Help me Understand

    Java Code:
    public class ParameterMystery { 
    public static void main(String[] args) {
    String x = "java";
    String y = "tyler";
    String z = "tv";
    String rugby = "hamburger";
    String java = "donnie";
    
    hamburger(x, y, z);
    hamburger(z, x, y);
    hamburger("rugby", z, java);
    hamburger(y, rugby, "x");
    hamburger(y, y, "java");
    
    }
    
    public static void hamburger(String y, String z, String x) {
    System.out.println(z + " and " + x + " like " + y);
    }
    
    }

    The output is :
    tyler and tv like java
    java and tyler like tv
    tv and donnie like rugby
    hamburger and x like tyler
    tyler and java like tyler


    Okay, so you have the public method hamburger on line 17 and are passing it three string parameters. Your method signature is hamburger(string y, string z, string x). The names of the three strings is irrelevant, they could be anything. What matters is that they are three strings. You first call the hamburger method on line 9 when you pass it three strings called x, y, and z. Again, names are irrelevant. What your program does is see three strings and matches them up with the three strings in the corresponding method (a method named "hamburger" with three strings as parameters). Those strings are plugged into the method in the order they appear. String x becomes string y, string y becomes string z, and string z becomes string x. The method then plugs those strings into the output in the method body on line 18, resulting in "tyler and tv like java".

    It may be helpful to see a visual:


Similar Threads

  1. I want to understand this...
    By WHM2 in forum New To Java
    Replies: 6
    Last Post: 01-14-2014, 09:51 PM
  2. I cant understand OOP
    By eng_hyzoom in forum New To Java
    Replies: 4
    Last Post: 11-19-2010, 04:10 PM
  3. GUI help. Don t understand
    By s0meb0dy in forum AWT / Swing
    Replies: 2
    Last Post: 10-27-2010, 09:40 PM
  4. Trying to understand
    By ladykrimson in forum New To Java
    Replies: 20
    Last Post: 10-12-2010, 11:10 PM
  5. I don´t understand
    By Manikyr in forum New To Java
    Replies: 6
    Last Post: 02-22-2009, 11:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •