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

查看:236
本文介绍了我如何在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;

取消引用null:

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。

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 ...)是同样多的潜在错误并可能导致丢失它指向的对象。

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系统将抛出异常( OutOfMemoryError )当你调用new并且分配器不能分配所请求的单元格时。这是非常罕见的,通常是由于失控递归造成的。

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.

新手经常会出现以下错误。

Novices often make the following 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
    }
} 

UPDATE:如评论中所建议的那样,必须注意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天全站免登陆