PDA

View Full Version : Java


9123456789
Jul 23, 2015, 11:06 PM
public class A {
public static void main(String[] args) {
int a= 20;
short b = 99;
short[] Num = new short[10];


Num[0] =b;


decrease1(a);


decrease2(Num);


System.out.println(Num[0]);
System.out.printf("a=%d, b=%d, Num[0]=%d",a,b,Num[0]);
}


static void decrease1(int num) {


num--;


}


static void decrease2(short[] num) {


num[0]--;


}
}




o/p is a=20 b=99 Num[0]=98

How num[00]=98??

CravenMorhead
Jul 24, 2015, 08:02 AM
The answer here is scope and how each of the variables were passed in; pointer, reference, and value. When decrease1 is called a local copy of the variable was made and that is used internally in the function. When decrease2 is called, you're passing in an array. This is a point that points to a series of shorts. You're actually working on the array that was passed in.

look this up in your textbook it will explain it better.