Java is easy to learn! Java is easy to learn! Java is easy to learn! Java is easy to learn! Java is object oriented ... ... ... is it because the method is calling the first string as the first argument then printing 4 times then grabbing the next argument printing 4 times then grabbing the last argument and printing 4 times or am I off base?
Why does it display four of the same string in a row?
You must be signed in to leave a comment
27 October 2020, 14:59
Methods were first devised as a way to put duplicate code that is used in multiple locations of a full project in one location and using an alias to execute that bit of code. Lets say you had 10 lines of code that retrieved data from the keyboard, did something with that data(being abstract here), then output a confirmation message. Now lets say that over 500 lines of code of the full project, this bit of code is used, in entirety, in 5 different locations. You could replace those 10 lines, in their 5 locations, with a method stub such as:and then move the 10 lines into one method. Whenever a method stub in code is reached, the processing moves to inside the method. In effect, you would save 40 lines of code in this example while making your overall project easier to read. On the method declaration you can use arguments inside the parentheses as ways to pass variable information to the method to use in its code. So when a method is declared such as: ... print(String s) The part underlined in parentheses is the method argument (or arguments as there can be as many as needed). When calling a method in code you MUST pass it the correct argument(s). You couldn't use this method like this: correct calls would be: So the conditions in this task say the method should print the arg 4 times. So whatever arg you pass to the method in code will be printed 4 times (as you have noticed). They are very powerful and extrememly important to understand. In OOP they are the primary way for classes to communicate with each other.
27 October 2020, 15:04
Just an FYI, you have been using methods the entire time. System.out.println() is a method as a way to pass messages from your code to the output screen. (if you haven't already) Soon you will learn how to use methods to send and receive messages to the keyboard using a BufferedReader or a Scanner. Java is only 50 keywords while 99% of it is just using methods. Everytime you see parantheses in code, that is a method
27 October 2020, 21:07
Interesting, thank you very much for the explanation.