enthuware.ocajp.i.v7.2.946

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ashishrai.kv
Posts: 33
Joined: Tue Jan 09, 2018 2:12 am
Contact:

enthuware.ocajp.i.v7.2.946

Post 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?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: enthuware.ocajp.i.v7.2.946

Post 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

ashishrai.kv
Posts: 33
Joined: Tue Jan 09, 2018 2:12 am
Contact:

Re: enthuware.ocajp.i.v7.2.946

Post by ashishrai.kv »

Thank you!!

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests