where does the s come from
where does the s come from? doesn't it need to be identified first?
Under discussion
Comments (7)
- Popular
- New
- Old
You must be signed in to leave a comment
Gellert Varga
22 June 2021, 21:37

+1
hidden #10760823
30 June 2021, 21:35
Forgive me for being slow, but I still can't wrap my head around why the lines of code are Passed on ton be associated with String S? Is there any other way you can try to explain to me? Thank you in advance!
0
Gellert Varga
1 July 2021, 10:32
I am referring to the code line numbers in the image attached above.
The code to execute should always be written in the main() method - between lines 3 and 7.
The print() method ( between line 9 and 13) does nothing by itself! It will only work/execute if it is called somewhere in the program.
Now this call is in main(), line 5.
In the header of the print() method (line 9), we see in parentheses that this method expects a (String) parameter. This means that the only way to call this method is to send it a String. If you don't send it anything, or send something else instead of a String: Error.
So, line 5 calls the print() method to execute it and at the same time sends it a String: "Java is easy to learn!".
The method on line 9 receives this sent string. You don't need to declare it, because in the method's parameter - i.e. in the parentheses - it says "String s", so we know that inside the method this received string is called variable s. If you want to print the received string to the console in this method, you have to print this s variable.
The meaning of this s variable is only used inside the print() method. As soon as the program has executed the method and exits to resume its original activity, it immediately forgets this s variable as if it never existed!
I hope it helped.
+5
Guadalupe Gagnon
1 July 2021, 13:47
A way that I had it described to me when I was struggling to understand is that methods are like telephone numbers. In the United States we have 7 digit local telephone numbers, which can be thought of like methods. To reach any person I want all I need to do is to dial their 7 digits. To reach any method I want I just need to type in the method name.
Now, you typically don't just call someone and stand there idly, not saying anything; you would call someone to have a conversation with them. You pass them information, they give you information back. This is also how methods behave; when you call a method you pass it a message. The method receives that message, processes it, and sends back a response.
Now the rules of methods:
- methods MUST have a return type, a name, and parameters:
- the code within the method needs to be surrounded by curly brackets:
- when calling a method you call it by name and pass arguments to it that match EXACTLY the parameters. Here are some examples using abstract code:
In each of these examples the method parameters changed, so to call the method the arguments had to change.
You may hear "parameter" and "argument" used interchangeably, but the correct usage:
- parameters are part of the method declaration, inside the parentheses
- arguements are what are passed to methods when calling them +1
Paloma
22 June 2021, 19:12
dont get it :(
0
Gellert Varga
22 June 2021, 21:38
See above, on the attached pic.
0
ron_villela
22 June 2021, 01:20
In print("Java is easy to learn") you are passing a String to the print() method below. The print() method is taking in the String and assigning the variable 's'. Once in the print() method it uses the 's' instead of the whole string that was passed (i.e. "Java is easy to learn").....gl
+1