在Java中为返回变量赋值

时间:2013-01-30 21:54:02

标签: java reference

这可能是一个基本问题,但我不确定使用哪些关键字进行搜索。

是否可以在Java中为返回变量赋值,如下所示:

static int a[] = new int[2];

static int f(int i) {
    return a[i];
}

static void main() {
    f(1) = 0; // <-- this
}

在C / C ++中,我可以返回一个指针并稍后为其赋值。由于Java使用引用,我希望上面的代码可以工作。我在这里错过了一些重要的Java概念吗?

8 个答案:

答案 0 :(得分:5)

即使它是引用类型,方法调用 变量

然而,这样的事情就可以了:

static MyClass func() {
   return new MyClass();
}

public static void main(String[] args) {
   func().setAttr(null); // change attributes
}

答案 1 :(得分:0)

这是不可能的(请参阅其他注释),但您可以创建自己的MutableInteger类,允许修改其值:

public class MutableInteger {
    public int value;
    public MutableInteger(int value) {
        this.value = value;
    }
    public MutableInteger() {
        this(0);
    }
}

static MutableInteger a[] = new MutableInteger[2];
static {
    for (int i=0; i<a.length; i++) {
        a[i] = new MutableInteger();
    }
}

static MutableInteger f(int i) {
    return a[i];
}

static void main() {
    f(1).value = 0; // <-- this
}

答案 2 :(得分:0)

没有。在Java中,只有原始类型(其行为与在C ++中的行为大致相同)和引用类型。引用类型的行为类似于C ++指针和C ++引用之间的混合 - 当您使用.时,它们会自动解除引用,但=始终会更改引用指向的内容,而不是引用的对象的值。< / p>

答案 3 :(得分:0)

这不起作用。也许它有助于理解“机器”级别上发生的事情。 在方法f内,表达式a[i]被评估为int值。返回此int值,而不与与该值关联的数组进行任何连接。从该方法返回后,您在Java虚拟机的操作数堆栈上具有int值(将其视为中间值)并且可以对其执行某些操作。您可以删除它(如果您在一行中编写f(1);就会发生这种情况),您可以将其分配给变量(int x = f(1)),这就是它。

答案 4 :(得分:0)

一种方法是将数组转换为MutableInt s(或AtomicInteger s)的数组,并返回对该数组的i元素的引用:

static AtomicInteger a[] = new AtomicInteger[2];
static {
    for (int i = 0; i < a.length; ++i) {
        a[i] = new AtomicInteger(0);
    }
}

static AtomicInteger f(int i) {
    return a[i];
}

public static void main(String[] args) {
    f(1).set(0);
}

答案 5 :(得分:0)

这将是一个编译错误,因为Assignment Operator要求左侧是Expression NameField Access ExpressionArray Access Expression,而{ {1}}是Method Invocation Expression

除了编译错误,这不是Java的工作方式。在Java中,所有内容都是按值传递的 - 特别是在此特定示例中,因为返回类型为f(1)

int

如果我们添加一个中间变量,它会变得更清晰:

static /*RETURN TYPE IS PRIMITIVE VALUE:*/ int f(int i) {
    return a[i];
}

答案 6 :(得分:0)

OP的问题是

  

是否可以在Java

中为返回变量赋值

非常简单......(注意我指定10来显示证据,因为初始化的int数组中的所有值都是0)...

static int a[] = new int[2];

static int f(int[] p, int index, int val) {
    return p[index] = val;
}

static void main() {        
    // f(a, 1, 0); use 10 (below) so we can show proof
    f(a, 1, 10);
    // proof
    System.out.println(a[1]);
}

另一个例子......愚蠢,但有效...

static int a[] = new int[2];

static int[] f(int[] p) {
    return p;
}

static void main() {
    f(a)[1] = 10;
}

又一个......

static int a[] = new int[2];

static void f(int i, int val) {
    a[i] = val;
    return;
}

static void main() {
    f(1, 10);
}

答案 7 :(得分:0)

您可能对lambdas如何应用于此问题感兴趣,但这仅适用于Java 8预览。

假设在标准库中有一个这样定义的接口:

interface Block<T> {
    void accept(T v);
}

(有人建议将其添加到java.util.functions,只增加了一点复杂性......)

现在我们可以像这样编写你的例子:

// Declare an array
int[] a = new int[5];

// Capture an "assignment reference" to element 2 
Block<Integer> elem2 = v -> a[2] = v;

// Some time later, we want to store a new value in that element:
elem2.accept(360);

因此,您的函数将返回Block<Integer>,之后您可以调用accept为其提供存储值。