Page 1 of 1

enthuware.ocajp.i.v7.2.946

Posted: Fri Mar 09, 2018 8:03 am
by ashishrai.kv

Code: Select all

public class TestClass{
    public static void main(String args[ ] ){
       int i = 1;
       int[] iArr = {1,2};
       incr(i) ;
       incr(iArr) ;
       System.out.println( "i = " + i + "  iArr[0] = " + iArr [ 0] ) ;
    }
    public static void incr(int   n) { n++ ; }
    public static void incr(int[ ] n ) { n [0]++ ; }
}
why i is not getting incremented?

and what are the scenarios on which a value doesn't get incremented, if you can share with a example?

Re: enthuware.ocajp.i.v7.2.946

Posted: Fri Mar 09, 2018 9:41 am
by admin
You need to read about pass by value semantics that Java uses for passing arguments . Here is a start:
https://dzone.com/articles/java-pass-by ... s-by-value

Re: enthuware.ocajp.i.v7.2.946

Posted: Mon Mar 12, 2018 2:06 am
by ashishrai.kv
Thank you!!