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 值,或者为另一个指针指定 second 值。
first = second;
second = third; //OK
请注意,以其他方式(NULL、 new...)给 second 赋值同样是一个潜在的错误,可能导致丢失它所指向的对象。
当您调用 new 并且分配器无法分配请求的单元格时,Java 系统将抛出异常(OutOfMemoryError)。这种情况非常少见,通常是由于失控的递归造成的。
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
}
}
你也可以有文字的指针。你必须自己实现它们。对于专家来说,这是相当基本的;)。使用 int/object/long/byte 数组,您就掌握了实现指针的基本知识。现在任何 int 值都可以是指向该数组 int []的指针。你可以递增指针,你可以递减指针,你可以乘以指针。您确实有指针算术!
这是实现1000个 int 属性类并具有应用于所有属性的泛型方法的唯一方法。您还可以使用 byte []数组代替 int []
然而我确实希望 Java 能够让你通过引用传递文字值
//(* 告诉你它是一个指针)
Public void myMethod (int * intValue) ;
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;
}
}
public class Main
{
public static void func ( int ptr1[] , int ptr2[] )
{
int temp;
temp = ptr1[0];
ptr1[0] = ptr2[0];
ptr2[0] = temp;
}
public static void main(String[] args) {
int x = 5; int y = 8;
// in c language
// int *p = &x; int *q = &y;
// in Java
int p[] = new int[1];
int q[] = new int[1];
p[0] = x; // same as pointer
q[0] = y; // same as pointer
func( p , q ); // passing address ( refrence )
System.out.println( " After Swap " );
System.out.println( "x = " + p[0] + " " + "y = " + q[0] );
}
}