Java Programming Tutorial 80 - Passing by Value or Reference

preview_player
Показать описание


~~~~~~~~~~~~~~~ CONNECT ~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~ SUPPORT ME ~~~~~~~~~~~~~~

🅑 Bitcoin - 3HnF1SWTzo1dCU7RwFLhgk7SYiVfV37Pbq
🅔 Eth - 0x350139af84b60d075a3a0379716040b63f6D3853
Рекомендации по теме
Комментарии
Автор

Thank you, all other videos were at least 2 times as long as this and 2 times more unclear

Daniel_He
Автор

2:35 - Why did he use the setFirstName setter here? It's within the class, so he could have set firstName directly. x.firstName = "Changed". Is that good practise because the setFirstName method might contain some restrictions on how the name is allowed to be changed?

trumbaron
Автор

when i try to change the name to "changed", it prints the id

ollie
Автор

I changed it to changeS:

User Class:

public void changeS(int x){
x++;
}

Main Class:

int y = 4;
User.changeS(y);
System.out.println(y);

The error I get is: non-static method changeS (int) cannot be referenced from a static context

darkfire_
Автор

x-->some spot in memory
Casting new User() for the x allocates a new slot in memory for this variable. So x appeared equal to something different from the previous x value taken in as a method parameter
x--> somewhere else

Archeryring
Автор

I agree the terms are used confusingly. For Java I would say:
- primitive types (int, float, char) are passed by value
- objects (including arrays) are passed by reference

bradwk
Автор

Hey Caleb, This series is fantastic.
As I understand Integer is a wrapper class which wraps a primitive type int into an object. But if Integer type argument is passed to a method and increment that within that method, the original value is not changed. I'm bit confuse on that. Do you have any explanation

chanakafernando
Автор

Hi people, I have a question.

Why is this possible?


public class Main {
public static void main(String[] args) {
int something [] = {0, 1, 2, 3};
changeArray(something);
for(int i : something)
System.out.println(i);
}

static void changeArray(int[] array){
array[1] = 99;
}
}






But not this?


public class Main {
public static void main(String[] args) {
int something = 1;
change(something);

}

static void change(int someInteger){
someInteger = 99;
}
}


Something about passing by value or by reference, which I still don't completely understand.
I can understand why the second one is not possible, as it copies the value into parameter, but I don't get why it's possible with the first example, as it would seem (at least for me) that it's not too different from the second example, where the array gets passed by value instead of reference.


Sorry if the terms I'm using are wrong.

Jason-tpcb