n++ and n[0]++

Oracle Certified Foundations Associate Java Certification Questions and Discussion
1Z0-811

Moderator: admin

Post Reply
hin1129
Posts: 9
Joined: Fri Apr 14, 2023 2:20 pm
Contact:

n++ and n[0]++

Post by hin1129 »

Code: Select all

public class TestClass {     
  public static void main(String args[ ] )     {        
    int i = 1;        
    int[] iArr = {1};        
    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 the answer is 1 and 2?
why int i is unchanged but int[] iArr is changed?
iArr [0] should have a default value of 0, how did it get to 2?
is there anything to do with the static keyword in the methods?
Last edited by admin on Thu May 04, 2023 1:32 am, edited 1 time in total.

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

Re: n++ and n[0]++

Post by admin »

Have you read about how method parameters are passed? Have you read about pass by value / pass by reference concept? Once you go through that, it will be clear to you.
Which book are you following?
If you like our products and services, please help us by posting your review here.

hin1129
Posts: 9
Joined: Fri Apr 14, 2023 2:20 pm
Contact:

Re: n++ and n[0]++

Post by hin1129 »

OCFA Java Foundations Exam Fundamentals 1Z0-811: Study guide for Oracle Certified Foundations Associate, Java Certification (Kindle Edition)
which topic/page should i go over for this question and the Static and non-static questions?

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

Re: n++ and n[0]++

Post by admin »

You may go through section 11.3 "Passing object references and primitive values into methods".
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 18 guests