如何在 Java 中使用指针? [英] How can I use pointers in Java?

查看:57
本文介绍了如何在 Java 中使用指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 Java 没有指针,但我听说 Java 程序可以用指针创建,而且这可以由少数 Java 专家来完成.是真的吗?

I know Java doesn't have pointers, but I heard that Java programs can be created with pointers and that this can be done by the few who are experts in java. Is it true?

推荐答案

Java 中的所有对象都是引用,您可以像使用指针一样使用它们.

All objects in Java are references and you can use them like pointers.

abstract class Animal
{...
}

class Lion extends Animal
{...
}

class Tiger extends Animal
{   
public Tiger() {...}
public void growl(){...}
}

Tiger first = null;
Tiger second = new Tiger();
Tiger third;

取消引用空值:

first.growl();  // ERROR, first is null.    
third.growl(); // ERROR, third has not been initialized.

混叠问题:

third = new Tiger();
first = third;

丢失细胞:

second = third; // Possible ERROR. The old value of second is lost.    

您可以通过首先确保不再需要旧的 second 值或将 second 的值分配给另一个指针来确保安全.

You can make this safe by first assuring that there is no further need of the old value of second or assigning another pointer the value of second.

first = second;
second = third; //OK

请注意,以其他方式(NULL、new...)给 second 一个值同样是一个潜在的错误,可能会导致丢失它指向的对象.

Note that giving second a value in other ways (NULL, new...) is just as much a potential error and may result in losing the object that it points to.

Java 系统在调用 new 时会抛出异常(OutOfMemoryError),并且分配器无法分配请求的单元格.这是非常罕见的,通常是由失控的递归引起的.

The Java system will throw an exception (OutOfMemoryError) when you call new and the allocator cannot allocate the requested cell. This is very rare and usually results from run-away recursion.

请注意,从语言的角度来看,将对象丢弃到垃圾收集器根本不是错误.这只是程序员需要注意的事情.同一个变量可以在不同的时间指向不同的对象,当没有指针引用它们时,旧值将被回收.但是如果程序的逻辑要求至少维护一个对象的引用,就会导致错误.

Note that, from a language point of view, abandoning objects to the garbage collector are not errors at all. It is just something that the programmer needs to be aware of. The same variable can point to different objects at different times and old values will be reclaimed when no pointer references them. But if the logic of the program requires maintaining at least one reference to the object, It will cause an error.

新手经常犯以下错误.

Tiger tony = new Tiger();
tony = third; // Error, the new object allocated above is reclaimed. 

你可能想说的是:

Tiger tony = null;
tony = third; // OK.

错误的铸造:

Lion leo = new Lion();
Tiger tony = (Tiger)leo; // Always illegal and caught by compiler. 

Animal whatever = new Lion(); // Legal.
Tiger tony = (Tiger)whatever; // Illegal, just as in previous example.
Lion leo = (Lion)whatever; // Legal, object whatever really is a Lion.

<小时>

C 中的指针:


Pointers in C:

void main() {   
    int*    x;  // Allocate the pointers x and y
    int*    y;  // (but not the pointees)

    x = malloc(sizeof(int));    // Allocate an int pointee,
                                // and set x to point to it

    *x = 42;    // Dereference x to store 42 in its pointee

    *y = 13;    // CRASH -- y does not have a pointee yet

    y = x;      // Pointer assignment sets y to point to x's pointee

    *y = 13;    // Dereference y to store 13 in its (shared) pointee
}

Java 中的指针:

class IntObj {
    public int value;
}

public class Binky() {
    public static void main(String[] args) {
        IntObj  x;  // Allocate the pointers x and y
        IntObj  y;  // (but not the IntObj pointees)

        x = new IntObj();   // Allocate an IntObj pointee
                            // and set x to point to it

        x.value = 42;   // Dereference x to store 42 in its pointee

        y.value = 13;   // CRASH -- y does not have a pointee yet

        y = x;  // Pointer assignment sets y to point to x's pointee

        y.value = 13;   // Deference y to store 13 in its (shared) pointee
    }
} 

更新: 正如评论中所建议的那样,必须注意 C 具有指针算法.但是,我们在 Java 中没有.

UPDATE: as suggested in the comments one must note that C has pointer arithmetic. However, we do not have that in Java.

这篇关于如何在 Java 中使用指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆