The Artima Developer Community
Sponsored Link

Java Answers Forum
passing array in java by value-result?

3 replies on 1 page. Most recent reply: Nov 2, 2004 2:39 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
ajay pappan

Posts: 1
Nickname: ajaypap
Registered: Nov, 2004

passing array in java by value-result? Posted: Nov 1, 2004 10:58 AM
Reply to this message Reply
Advertisement
what is the result if array is passed by value-result

class ParameterExample
{

static int i = 0;
static int[] a = {7, 11, 13};


static void P(int x, int y)
{
y = 1;
System.out.println(x);
i = 2;
System.out.println(x);
System.out.println(y);
}

public static void main(String[] args)
{
i = 0;
P(a, i);
P(i, a);
}
}


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: passing array in java by value-result? Posted: Nov 2, 2004 2:07 AM
Reply to this message Reply
How can this work?
a is int[], you can't use it as int.

In your casse, you would need

static void P(int x, int[] y) {...}
and
static void P(int[] x, int y) {...}


What are you trying to do?

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: passing array in java by value-result? Posted: Nov 2, 2004 2:35 PM
Reply to this message Reply
For it to work, you'd probably have to have Object paramters to P() and box up the int into an Integer object, then do silly type checking stuff in P(). Assuming your goal is to have interchangeable paramers...

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: passing array in java by value-result? Posted: Nov 2, 2004 2:39 PM
Reply to this message Reply
By the way, I should have added if that (swappable parameters) is your goal, you should be aware that it would be a pretty anti-idiomatic thing to do in Java. It might be less offensive in a dynamically typed language like Python or Ruby, but even in those languages, many people would find the practice annoying and confusing. For certain, you'll make no friends in the Java community with this kind of behavior (pun intended).

Flat View: This topic has 3 replies on 1 page
Topic: JCreator Pro Previous Topic   Next Topic Topic: searching array using recursion

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use