What is the difference between C structures and Java classes?

I'm new to Java but a little familiar with C. I wanted to know what differences exist between C structures and Java objects and call their methods? Or are they completely equivalent?

For example, the structure of a bicycle:

class BicycleDemo { public static void main(String[] args) { // Create two different Bicycle objects Bicycle bike1 = new Bicycle(); Bicycle bike2 = new Bicycle(); // Invoke methods on those objects bike1.changeCadence(50); bike1.speedUp(10); bike1.changeGear(2); bike1.printStates(); bike2.changeCadence(50); bike2.speedUp(10); bike2.changeGear(2); bike2.changeCadence(40); bike2.speedUp(10); bike2.changeGear(3); bike2.printStates(); } } 

Why am I asking because they look so similar! Thanks!

+7
source share
4 answers

If you leave the method overriding the image, you might think of Java classes and methods as C-style struct pairs and a set of functions that work with these struct s. For example, if you have a class like this:

  public class MyJavaClass { private int x; public int getX() { return x; } public int setX(int value) { x = value; } } 

It will be like writing C code in this regard:

 struct MyJavaClass { int x; }; int MyJavaClass_getX(struct MyJavaClass* this) { return this->x; } void MyJavaClass_setX(struct MyJavaClass* this, int value) { this->x = value; } 

The basic idea is that the method is similar to a function that takes a receiver object as an implicit parameter of "this". In C, you must explicitly pass the receiver as a parameter to the function, while in Java this is done implicitly through the syntax object.method() .

If you start introducing a method override, it becomes a little more complicated because the method you call on the object depends on the dynamic type of the object, and not on the static type. One way to simulate this is to use something called the vtable virtual table table or the so-called because of the C ++ virtual . The idea is that each object stores a pointer to a table of function pointers, one for each function that can be overridden, and when a method is called on an object, the corresponding function pointer is selected from the table and called. So, more correctly, the above Java object might look something like this:

 struct MyJavaClass_Vtable { void (*getX)(struct MyJavaClass* this); void (*setX)(struct MyJavaClass* this, int value); }; struct MyJavaClass { struct MyJavaClass_Vtable* vtable; int x; }; int MyJavaClass_getX(struct MyJavaClass* this) { return this->x; } void MyJavaClass_setX(struct MyJavaClass* this, int value) { this->x = value; } /* A global instance of the vtable for MyJavaClass */ struct MyJavaClass_Vtable MyJavaClassVtableInstance = { &MyJavaClass_getX, &MyJavaClass_setX }; 

Whenever you create an instance of MyJavaClass , you must configure it vtable as follows:

 struct MyJavaClass* mjc = malloc(sizeof *mjc); mjc->vtable = &MyJavaClassVtableInstance; 

Then, when calling such a function (in Java):

 myJavaClass.getX(); 

In C, it will look like

 myJavaClass->vtable->getX(myJavaClass); 

So, in a way, the Java class is just a structure with some extra meta information. Of course, for a programmer, it looks completely different: encapsulation, polymorphism, a more stringent type system, etc., But at the native code level, the regular C structure and Java class probably look very similar.

Hope this helps!

+13
source

C struct cannot have methods / functions in it. This is just a collection of different types of data. class can have both declared variables and methods.

+4
source

The Java class allows you to define fields, such as a C structure, but has the following additional functions:

  • As you state in your question, you can add methods to a class that can work with data.
  • You can declare both fields and private methods (and several other access parameters), which means that they can be accessed only by other methods of the class, and not the code outside. (It would be pointless to have this in structures, since there would be no access to private fields - this makes sense only when methods are allowed).
  • Classes can inherit other classes. Among other things, this allows you to pass an object of type B where type A was expected, as long as B inherits from A.

There is another subtle difference. In C, structures are value types, but in Java, classes are reference types. This means that when you copy a structure in C (for example, assign it to a variable or pass it as an argument), it copies all its fields, whereas in Java, when copying an object of a class, it simply copies the reference to the object (therefore, if you change your fields , you also modify the original object, not just the copy).

In C, you often use structure pointers to emulate Java reference behavior. In Java, you do not use pointers because class objects are already references.

+2
source

The main differences, IMHO are classes that allow

These are two very important features of OOP.

+1
source

All Articles