我怎样才能在Java中使用指针?

我知道Java没有指针,但是我听说可以用指针创buildJava程序,而这可以由less数几个java专家来完成。 这是真的吗?

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

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. 

你可以通过首先确保没有进一步需要旧值或者赋值另一个指针来保证安全。

 first = second; second = third; //OK 

请注意,以其他方式给出第二个值(NULL,new …)与潜在错误一样多,可能会导致丢失指向的对象。

当您调用new时,Java系统将抛出exception( OutOfMemoryError ),并且分配器无法分配请求的单元。 这是非常罕见的,通常是由失控recursion造成的。

请注意,从语言的angular度来看,将对象放弃到垃圾收集器根本不是错误。 这只是程序员需要注意的事情。 同一个variables可以在不同的时间指向不同的对象,当没有指针引用它们时,旧的值将被回收。 但是,如果程序的逻辑需要至less保持一个对象的引用,那么会导致错误。

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

 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中的指针:

 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 } } 

更新:如评论中所build议的,必须注意到C具有指针算术。 但是,我们没有在Java中。

Java确实有指针。 任何时候你用Java创build一个对象,实际上是创build一个指向对象的指针; 这个指针可以被设置为一个不同的对象或为null ,并且原始对象将仍然存在(等待垃圾回收)。

在Java中不能做的是指针算术。 您不能取消引用特定的内存地址或增加指针。

如果你真的想要低级别的话,唯一的办法就是使用Java Native Interface ; 即使如此,低级部分也必须用C或C ++来完成。

由于Java没有指针数据types,所以不可能在Java中使用指针。 即使是less数专家也无法在java中使用指针。

另请参阅Java语言环境中的最后一点

Java没有像C这样的指针,但是它确实允许你在被variables“引用”的堆上创build新的对象。 缺less指针就是阻止Java程序非法引用内存位置,同时也会使垃圾收集由Java虚拟机自动执行。

您可以使用不安全类的地址和指针。 然而顾名思义,这些方法是不安全的,通常是一个坏主意。 不正确的用法会导致你的JVM随机死亡(实际上,在C / C ++中错误地使用指针也是一样的)

虽然你可能习惯了指针,并认为你需要它们(因为你不知道如何编写任何其他的方式),你会发现你没有,你会更好。

Java中有指针,但不能像C ++或C那样操作它们。当你传递一个对象时,你传递的是一个指向该对象的指针,但与C ++中的指针不同。 该对象不能被解除引用。 如果你使用它的本地访问器来设置它的值,它将会改变,因为Java通过指针知道它的内存位置。 但是指针是不可改变的。 当您尝试将指针设置为一个新的位置时,您最终会得到一个与另一个名称相同的新本地对象。 原始对象不变。 这里是一个简短的程序来展示差异。

 import java.util.*; import java.lang.*; import java.io.*; class Ideone { public static void main(String[] args) throws java.lang.Exception { System.out.println("Expected # = 0 1 2 2 1"); Cat c = new Cat(); c.setClaws(0); System.out.println("Initial value is " + c.getClaws()); // prints 0 obviously clawsAreOne(c); System.out.println("Accessor changes value to " + c.getClaws()); // prints 1 because the value 'referenced' by the 'pointer' is changed using an accessor. makeNewCat(c); System.out.println("Final value is " + c.getClaws()); // prints 1 because the pointer is not changed to 'kitten'; that would be a reference pass. } public static void clawsAreOne(Cat kitty) { kitty.setClaws(1); } public static void makeNewCat(Cat kitty) { Cat kitten = new Cat(); kitten.setClaws(2); kitty = kitten; System.out.println("Value in makeNewCat scope of kitten " + kitten.getClaws()); //Prints 2. the value pointed to by 'kitten' is 2 System.out.println("Value in makeNewcat scope of kitty " + kitty.getClaws()); //Prints 2. The local copy is being used within the scope of this method. } } class Cat { private int claws; public void setClaws(int i) { claws = i; } public int getClaws() { return claws; } } 

这可以在Ideone.com上运行。

java中的所有对象都通过引用复制传递给函数,除了基本types。

实际上,这意味着您要将指针的副本发送给原始对象,而不是该对象本身的副本。

如果你想要一个例子来理解这个,请留下评论。

不是真的,不是。

Java没有指针。 如果你真的想要,你可以尝试通过构build像reflection这样的东西来模拟它们,但是它会带来所有复杂的指针,而没有任何好处

Java没有指针,因为它不需要它们。 你希望从这个问题中得到什么样的答案?即:内心深处你是否希望你能用它来做某件事,或者这只是一种好奇?

从Godfrey Nolan的名为Decompiling Android的书

安全性决定了Java中不使用指针,所以黑客无法摆脱应用程序和操作系统。 没有指针意味着其他的东西 – 在这种情况下,JVM —-必须关心分配和释放内存。 内存泄漏也应该成为过去,或者说这个理论是如此。 一些使用C和C ++编写的应用程序,因为程序员没有注意在合适的时间释放不需要的内存而臭名昭着的内存就像筛子一样臭名昭着 – 而不是任何人阅读这些内容都会犯这样的罪过。 垃圾收集也应该使程序员更有效率,用更less的时间来debugging内存问题。

Java引用types与C指针不同,因为在Java中不能有指向指针的指针。

从技术上讲,所有的Java对象都是指针。 所有的原始types都是值。 没有办法手动控制这些指针。 Java只是在内部使用pass-by-reference。

正如其他人所说,简短的回答是“不”。

当然,你可以编写使用Java指针的JNI代码。 根据你想要完成的事情,也许这会让你在某个地方,也许不会。

你总是可以通过创build一个数组和处理索引到数组来模拟指针。 再一次,取决于你想要完成什么,这可能会或可能不会有用。

你也可以有文字的指针。 你必须自己实现它们。 这对于专家是非常基本的)。 使用一个int / object / long / byte的数组,你有基本的指针实现。 现在任何int值都可以是指向数组int []的指针。 你可以增加指针,你可以减less指针,你可以乘以指针。 你确实有指针algorithm! 这是实现1000个int属性类的唯一方法,并且具有适用于所有属性的generics方法。 你也可以使用byte []数组来代替int []

不过,我希望Java会让你通过引用传递字面值。 沿线的东西

//(* telling you it is a pointer) public void myMethod(int* intValue);

所有java对象都是指针,因为保存地址的variables称为指针和对象保持地址。因此对象是指针variables。