简体   繁体   English

如何实现基本指针

[英]How to implement a basic pointer

I know here is no pointer in Java. 我知道这里没有Java中的指针。 But how do I change a value in the calling scope? 但是如何更改调用范围中的值? For instance, I want to write a function that takes an integer num, set the integer to 0 if it's greater than 21, otherwise do nothing. 例如,我想写一个带整数num的函数,如果它大于21则将整数设置为0,否则什么都不做。 In the main, my code is as follow: 主要是,我的代码如下:

int a=34;
KillOver21(a);
System.out.print(a);

I expect an 0. 我期待0。

Java is pass by value , so a copy of the parameter a is sent to the method, so modification to a in the method will not affect the original argument a in main Java是按值传递的 ,因此参数a的副本被发送到方法,因此对方法中a修改不会影响main的原始参数a

The max you can do is return int from KillOver21(a) method 你可以做的最大值是从KillOver21(a)方法return int

int z = KillOver21(a); // This will return 0
System.out.print(z);

But you can achieve something like that with custom objects, say you have a class 但你可以用自定义对象实现类似的东西,比如你有一个类

class AHolder {

    public int a;

}

then you can expect AHolder instance to change 那么你可以期待AHolder实例改变

public static void main(String [] args) {
     AHolder a = new AHolder();
     a.a = 34;
     killOver21(a);
     System.out.println(a.a);
}

public static void killOver21(AHolder b) {
    if(b.a > 21) {
       b.a = 0;
    }
}

Since in the latter (even if its Pass by Value ) , the reference is copied and both reference point to same object . 由于在后者中(即使它的Pass by Value ), 复制 引用并且两个引用都指向同一个对象 So changes made inside the killOver21 method actually changes the object . 因此,在killOver21方法中进行的更改实际上会更改对象

在此输入图像描述

It is simply not possible, Java supports pass by value . 这根本不可能,Java支持传递值 int a 's value will be copied to the function. int a的值将被复制到函数中。

You could use Object instead of primitive where the reference value will be copied to your function by which you can get the actual object and modify it. 您可以使用Object而不是primitive,其中引用值将被复制到您的函数中,您可以通过该函数获取实际对象并对其进行修改。

Fundamentally impossible in Java, period. 在Java中,根本不可能。 int are immutable, and passed by value. int是不可变的,并按值传递。 You would need to create a mutable int type: 需要创建一个可变的int类型:

class MutableInt {
    private int value;

    public MutableInt(int value) { this.value = value; }

    public getValue() { return this.value; }
    public setValue(int value) { this.value = value; }
}

Then: 然后:

void KillOver21(MutableInt m) {
    if(m.getValue() > 21) { m.setValue(0); }
}

However, be aware the mutable types that represent concepts that are defined by their value rather than their identity are generally an extremely bad idea . 但是,请注意,表示由其值而非其身份定义的概念的可变类型通常是一个非常糟糕的主意 But, this is the only way to achieve what you're trying to achieve. 但是,这是实现您想要实现的目标的唯一途径。 Again, I caution you with the strongest words: what you're doing is a bad idea. 再一次,我用最强烈的话语提醒你:你所做的是一个主意。 You should find another way. 你应该找到另一种方式。

Doc, it hurts when I do this. 医生,我这样做会很痛。

Then don't do that! 那就不要那样做!

The simpliest way (quick&dirty) is to put value within an array 最简单的方法(快速和脏)是将值放在数组中

int holder[] = new int[]{ a};

KillOver21(holder)

System.out.printf( "value=[%d]", holder[0]  );

void KillOver21(int holder[] ) {
    holder[0] = 0;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM