简体   繁体   English

Java中的C指针等效

[英]C Pointer Equivalence in Java

I wanted to know what the Java translation of this block of C code would be. 我想知道这个C代码块的Java翻译是什么。

 Person * father = malloc(sizeof(Person));
 Marriage * marriage = malloc(sizeof(Marriage));
 (* marriage).male = father;

I'm guessing I use reference. 我猜我使用参考。 So instead of making a variable father point to Person I just make a father object for the class Person like this: 因此,我没有为Person类创建变量父亲,而是为Person类创建了父亲对象,如下所示:

Person father = new Person();

I might be wrong on this. 我对此可能是错的。

How would the line (* marriage).male = father; 怎么行(* marriage).male = father; work? 工作?

I'm quite new to C and I am just trying to get a better understanding of how it works. 我对C还是很陌生,我只是想更好地了解C的工作原理。

That would be a mere marriage.male = father; 那简直就是marriage.male = father; in Java. 在Java中。

(*marriage).male is equivalent to marriage->male in C. marriage is a pointer to a memory location, similar to a reference to an object created with new in Java. (*marriage).male相当于C中的(*marriage).male marriage->malemarriage是指向内存位置的指针,类似于对用Java中new创建的对象的引用。

Also, note that while in a certain sense those can be associated with Java constructs, they are certainly no "equivalents." 另外,请注意,尽管从某种意义上讲它们可以与Java构造相关联,但它们肯定不是“等效物”。 Java has a garbage collector, for one. Java有一个垃圾收集器。 C doesn't. C没有。 Java is interpreted. Java被解释。 C isn't. C不是。 There's a lot of subtle differences, so I would never say that anything in C is anywhere equivalent to any Java. 有很多细微的差异,所以我永远不会说C中的任何东西都等同于任何Java。


Instead of learning C by equating it with Java, read a book explicitly about C. There are articles like "C for Java programmers" that might be a better entrance for you. 与其通过将Java与Java等同来学习C,还不如直接阅读一本有关C的书。有些像“ C for Java程序员”之类的文章可能对您来说更好。 However, don't really equate Java with C at all, that just leads to bad stuff. 但是,根本不将Java与C完全等同,只会导致糟糕的事情。

We can't work with pointers in Java, but, we can translate that code in C to Java: 我们不能使用Java中的指针,但是可以将C中的代码转换为Java:

    Person father = new Person();
    Marriage marriage = new Marriage();
    marriage.male = father;

(* marriage).male = father; (*婚姻)。男=父亲; is the same that marriage->male = father like cadaniluk said. 就像卡达尼尔鲁克所说的,婚姻->男=父亲是一样的。 This operator (* pointer) get the pointer's value, in that case it is a Marriage Object. 该运算符(*指针)获取指针的值,在这种情况下,它是一个婚姻对象。 If you would like to understand better about pointers in C, read this article: Pointers in C 如果您想更好地了解C中的指针,请阅读本文: C中的指针

C is a structured but not object-oriented language. C是一种结构化但非面向对象的语言。 There are basically two mechanisms to realize something like objects: data structures called Structs on one side and Pointer on the other side. 基本上有两种机制可以实现类似对象的功能:一方面是称为Structs的数据结构,另一方面是Pointer Pointer are like variables, but made to reference a memory address of a given field. 指针就像变量一样,但是使其引用给定字段的内存地址。

struct Person {
    char name[50];
};

struct Marriage {
    struct Person *father; /* fields of type 'pointer to a struct person' */
    struct Person *mother; 
} marriage;

int main() {
    struct Person father = {"Steve"};
    marriage.father = &father; /* marriage.father references the memory address of father */
    ...
}

In contrast, Java is an object-oriented language where eg an object A references another object B , without the need for that special pointer type , because these "pointing" happens implicit. 相反,Java是一种面向对象的语言,其中,例如,对象A 引用另一个对象B ,而无需那种特殊的指针类型 ,因为这些“指向”是隐式发生的。 So your code example in Java would be: 因此,您在Java中的代码示例为:

Person father = new Person(); // acts like struct Person
Marriage marriage = new Marriage(); // acts like struct Marriage
marriage.male = father; // marriage.male references ("points to") father memory address

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM