Methods with parameters in Java

Can anyone explain why this works.

I have 2 classes in Eclipse. A class called "Car" contains the following code.

public class Car { public void printOut(String variable1){ System.out.println("Hello " +variable1); } } 

and another class, where my "main" one, is called the "house", the code inside it

 import java.util.Scanner; class House { public static void main(String args[]){ Scanner input = new Scanner(System.in); Car carObject = new Car(); System.out.println("Enter name here: "); String variable2 = input.nextLine(); carObject.printOut(variable2); } } 

When I run the code, it works, it writes "Enter the name here", and when I cross it out, it continues to say the name "Hello" "

My question is: do 'variable1' and 'variable2' have something to do with eachother, except that they both belong to the String class.

because I'm confused why the code compiles correctly.

It seems to me that variable 1 has no correlation with variable2, although they are both String classes, they are not similar to each other, and variable1 is not used in the "House" class at all, but it still knows to compile everything that I entered . As if "variable1" is being replaced by "variable2", and every variable2 contains is being printed.

+7
source share
11 answers

Defining a method in the Car class is a kind of prototype when using it. Have you ever taught black box math functions? You enter a number and get the result. So you enter 3, if the function f (x) = Xx2, the output will be 6. Before calling the var2 method, it is completely different from var1. However, the var2 method is passed and replaces all var1 that you use in the method. Do not worry, I did not understand this when I started Java

+3
source

They are only related because you pass variable2 to your Car.printOut method. Imagine this analogy for a moment: you know how to do math homework on a team. You are an object called FacelessVoid , and you have a method called doHomework . doHomework accepts a single parameter of type Work . In the real world, I would have a box of work, and I would drop the box of work on your desk. No matter what I write on the box, it doesn’t matter to you, but the contents of the box are dumped onto your desk, where you can call it whatever you want.

This is exactly what happens in your code: you have a line called variable2 , and its contents are "dumped" to variable1 inside printOut . Note, however, that the string does not actually get dumped into your method, the link is copied. So this is the same object, it is simply called something else.

+3
source

The reference in variable2 will simply be called copied to variable2. Variable2 is passed as an argument to the method. In addition, there is no relationship. Variable2 is local only to main, variable1 is local only to printOut. think of variable1 as a function parameter, expecting a value from the calling method. This means that you can pass any line to printOut.

For example carObject.printOut("Toyota") As you can see, I do not pass the variable to printOut, but a string encoded string

+1
source

variable2 is a link for a string somewhere in memory. The value in variable2 is used by Java to find out where the string is. For example:

 variable2 memory location 1100 [1100] ------> "hello" 

variable1 is also a string reference somewhere in memory. When you call the function, the value of variable2 (in this case 1100) is placed in variable1.

 variable1 memory location 1100 [1100] ------> "hello" 

Thus, the function can find a string that refers to variables2 using variable1, since the location of the string is contained in both. Or in another way, they refer to the same line.

+1
source

The value of Variable2 will be moved to variable1 when the method is called

0
source

They are exactly the same as you pass variable2 to the printOut () method. variable1 is just the name for the method parameter.

0
source

Variable names from the calling or called party are not affected. You may have a variable parameter called orange , and with another helicopter it will do the same. The important thing is that they must be of the same type (here String ).

The contents of the variable of the caller are passed to the method regardless of the name of both variables.

0
source

variable1 is not really a variable, it is the name of the variable that will be passed to the method. You will need this name because you use it in your method for some operations. Therefore, when you pass variable2 , which is now a variable, you see it in a method called variable1 . Hope this helps.

0
source

In Java, parameters are always passed by value. If you however pass the object, then it points to the original link to the object, and you can change the original object.

0
source

there is no relativity v1 and v2. When you call the car.printOut () method from the Hous class, it pushes v2 to the stack as a value. car.printOut () pops this value of variable v1 from the stack. Therefore, you can use any name.

Also, when you change the value of v1 to printOut (), v2 does not change.

0
source

Others gave good answers, but to get the correct vocabulary, variable1 is called a formal parameter , while variable2 is the actual parameter, also called the "argument" passed to the printOut method. It would be easier to understand that you are just playing with names when it comes to passing arguments. At the gut level, he simply plays with data locations, rather than referencing material by name.

0
source

All Articles