java如何使用指针,我如何在Java中使用指针?

本文探讨了Java中对象引用的概念,虽然Java没有像C++那样的指针,但引用可以类似地使用。文章通过示例展示了如何在Java中使用引用,包括空引用错误、别名问题、对象丢失以及类型转换的注意事项。同时,对比了C++中的指针操作,强调了Java中避免内存错误的重要性。
摘要由CSDN通过智能技术生成

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?

解决方案

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;

Dereferencing a null:

first.growl(); // ERROR, first is null.

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

Aliasing Problem:

third = new Tiger();

first = third;

Losing Cells:

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

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

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.

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.

What you probably meant to say was:

Tiger tony = null;

tony = third; // OK.

Improper Casting:

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.

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

}

Pointers in 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: as suggested in the comments one must note that C has pointer arithmetic. However, we do not have that in Java.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值