1

I was glancing through this function getLocationOnScreen()

void getLocationOnScreen (int[] outLocation)

Computes the coordinates of this view on the screen. The argument must be an array of two integers. After the method returns, the array contains the x and y location in that order.

What I don't understand here is that if I am passing an array something like this,

int position[] = new int[2];

view.getLocationOnScreen(position);

The function being return type being void how does my array contains those values without the function returning it ?

Note: This question is not a duplicate of the below as it explains the working of the 2 methods.

getLocationOnScreen() vs getLocationInWindow()

Community
  • 1
  • 1
oldcode
  • 1,669
  • 3
  • 22
  • 41
  • I think you should learn the difference between _procedure_ and _function_ in programming language first. – Anggrayudi H Jan 11 '17 at 05:59
  • @AnggrayudiH That is a pass by value function so I was wondering how is it possible ? – oldcode Jan 11 '17 at 06:05
  • the method simply edits your array. So it does not have to return something. It just edits the argument. – Vladyslav Matviienko Jan 11 '17 at 06:11
  • @VladMatvienko I am only passing the array as there is only pass by value in Java an no pass by reference so how is Android able to edit – oldcode Jan 11 '17 at 06:12
  • you are passing a reference to array, not an array. Everything in Java is reference. – Vladyslav Matviienko Jan 11 '17 at 06:13
  • @VladMatvienko [Got it](http://stackoverflow.com/questions/12757841/are-arrays-passed-by-value-or-passed-by-reference-in-java) – oldcode Jan 11 '17 at 06:14
  • Possible duplicate of [Is Java "pass-by-reference" or "pass-by-value"?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – oldcode Feb 02 '17 at 05:14

2 Answers2

0

The primitive type will pass variable by copy it value ,so the value will not change after the method finished.But as you see ,you created an array type using keyword new ( new int[2]),so the this variable is an object type, when you passed param to the method it would be reference of the array.

In Java the primitive type pass by value while object type by reference and you can not chose to pass address like C using operation &

pf0214
  • 181
  • 8
0

It's about the reference an object from memory. You are sending your object adress which means the adress referencing your object. And the function using this adress of your object. With that way the function can change your object's value but can not change the adress. Here, this idea coming from C language

And usage in java:

Java works exactly like C. You can assign a pointer, pass the pointer to a method, follow the pointer in the method and change the data that was pointed to. However, you cannot change where that pointer points

Community
  • 1
  • 1
Eren Utku
  • 1,731
  • 1
  • 18
  • 27