Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Other Software (https://www.askmehelpdesk.com/forumdisplay.php?f=394)
-   -   Java (https://www.askmehelpdesk.com/showthread.php?t=814452)

  • Jul 23, 2015, 11:06 PM
    9123456789
    Java
    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??
  • Jul 24, 2015, 08:02 AM
    CravenMorhead
    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.

  • All times are GMT -7. The time now is 02:05 AM.