r/javahelp • u/chillip135 • Jul 16 '17
Can someone explain this to me?
public class hoho{
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);
}
}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 I got up to Java and tyler like tv...but now I'm stuck with the 3rd part... I don't get how it's TV and donnie like rugby...? as well as the other two below
0
u/NotPennysUsername Jul 16 '17
The "hamburger" method takes three strings as arguments and prints out "string2 and strin3 like string1."
For the first two, the strings "java," "tyler," and "tv" were used as arguments for the method hamburger, and they were referenced using the variable name assigned to them: x, y, and z. When the other three method calls are made, they input some of the strings using references to the variables x, y, z, rugby, or java AND they also take some of the arguments as raw strings without a variable reference (those are the ones with double quotes around them). So when the method call looks like:
hamburger("rugby", z, java);The two variables that are referenced are z and java, and the strings they reference ("tv" and "donnie" respectively) are printed. But, since "rugby" is passed in as a string, it doesn't refer to the variable rugby, rather the string "rugby." So it results in: tv and donnie like rugby
1
u/throwaway_for_cause Jul 16 '17
Sidebar
Do not reply with complete solutions
Even more so when OP can't be bothered to ask a proper question, nor to format their code. We do not support carelessness/laziness. OP skould follow the rules as was pointed out in the mod's comment and once he does that - and only then he should be helped.
By giving OP the complete solution you have not helped their learning. You supported only laziness.
1
u/NotPennysUsername Jul 16 '17
Well, I only explained one reason why the third method call is different from the first two, and I left the final two for OP.
By giving OP the complete solution you have not helped their learning. You supported only laziness.
I totally get the desire to discourage giving complete solutions, but I thought what I was doing was clarifying an understandably confusing issue: namely, passing in strings vs references to strings as arguments. I don't necessarily know if that's what OP was having trouble with, but I took a guess...apparently that's a downvote-able offense? If you (or anyone else) has any recommendations for how I should edit the post, I'd be happy to consider them.
1
u/chillip135 Jul 16 '17
Thank you for clarifying. It totally makes sense now. I was staring at it....writing it out and still couldn't figure out why the output was different than what I was getting. I struggled for over an hour so I decided to put on reddit. Thank you for a sincere response.
•
u/desrtfx Out of Coffee error - System halted Jul 16 '17
Please read and follow our Posting Guidelines
You need to be way more elaborate in asking questions. What exactly do you have problems with?
Please format your code. Instructions how to do so are in the sidebar
Work through the code with pencil and paper. You are the computer, you execute every step. Trace all the variables.
The best way to understand an algorithm is to carry it out on paper as if you were the computer. Line by line explanations are not beneficial for your learning.