Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8158879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:45:13+00:00 2026-06-06T17:45:13+00:00

I am fairly new to Java, and recently I was reading some material about

  • 0

I am fairly new to Java, and recently I was reading some material about Java being pass-by-value. I’ve read over this question, and this blog before running a test myself.

Now, based on my reading and my quick test, I found that there are two ways that I can alter the variables contained within an object reference. Which of the below approaches is the better or safer approach? Are there any obvious issues with either approach?

Both of these print out “iArr[0] = 45”.

Approach 1:

public static void main(String args[] ){
   int[] iArr = {1};
   method(iArr) ;
   System.out.println( "iArr[0] = " + iArr [0] ) ;
}
public static void method(int[] n ) { 
    n [0] = 45 ;
}

Approach 2:

public static void main(String args[] )
{
   int[] iArr = {1};
   iArr = method(iArr) ;
   System.out.println( "iArr[0] = " + iArr [0] ) ;
}
public static int[] method(int[] n ) { 
    n [0] = 45 ;
    return n;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T17:45:15+00:00Added an answer on June 6, 2026 at 5:45 pm

    I find neither approach ideal as they both cause the same side-effects.

    That is, they are the same but the 2nd approach also returns the modified object: the 2nd approach still modifies the array object passed in! The re-assignment to iArr of the return value from #2 in the example code has no effect on the object modified! Remember that Java uses Call-By-Object-Sharing semantics (for reference types); the return value is unrelated to this behavior.

    I actually really dislike approach #2 because it “hides” this fact (I look at the signature and think “oh, I get a new array object!”), while approach #1 “does it’s dirty job”, but I can tell that quickly from the void return type. (In some advanced casing “chaining” can be useful; this is not one of them.)

    Here is a trivial version which does not cause side-effects: (I would suggest minimizing side-effects in general as it often makes code easier to reason about and debug.)

    public static void main(String args[] )
    {
       int[] iArr = {1};
       int[] newArr = method(iArr) ;
       System.out.println( "iArr[0] = " + iArr [0] ) ;
       // This is different here, but it would "be the same" in the 
       // 2nd-case example in the post.
       System.out.println( "newArr[0] = " + newArr [0] ) ;
    }
    public static int[] method(int[] n ) {
        // This is a silly stub method, n would be presumably used.
        int[] x = new int[1];
        x[0] = 45; // modifying a DIFFERENT object
        return x;  // returning the NEW object
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all, I'm fairly new to Java, so sorry if this question is
I'm relatively new to java (specifically swing) and have recently been making some fairly
Another question about my implementation. I am fairly new to java/android coming from a
I am fairly new to Java and in another Stack Overflow question about for
I have been working on Eclipse recently. I am fairly new to java programming,
I'm fairly new to Java and I've been having some difficulties with Swing. I'm
I'm fairly new to java. I'm reading up on it and learning as I
I'm a fairly new to Java and to programming, so excuse me if this
I'm fairly new in Android/Java programming, so some of the basic stuff is still
I'm fairly new to Java so please forgive me if this is a stupid

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.