What is the difference between a java link and a c ++ link

So, I was thinking (reading this Java file) ...

I know this may seem silly, but why can't I do it in C ++.

float &f = new float; 

Does this not mean that the reference to f is the address of the new float?

In Java, I see something like this

 String s = new String("something") 

String s is called a string reference.

Does the word β€œlink” in Java have the same meaning as in C ++?

+3
source share
7 answers

Java links are much closer to C ++ pointers than to C ++ links. In Java, you can do the following with a link:

  • Change which object it belongs to
  • Check if two links are equal or not equal:
  • Send messages to the specified object.

In C ++, pointers have the same properties. As a result, the code you are looking for in C ++ is similar to

 float* f = new float; 

This is completely legal. For a better comparison, this Java code:

 String myString = new String("This is a string!"); // Normally I wouldn't allocate a string here, but just for the parallel structure we will. System.out.println(myString.length()); /* Reassign myString to point to a different string object. */ myString = new String("Here another string!"); System.out.println(myString.length()); 

will display this code in C ++:

 std::string* myString = new std::string("This is a string"); std::cout << myString->length() << std::endl; delete myString; // No GC in C++! /* Reassign myString to point to a different string object. */ myString = new std::string("Here another string!"); std::cout << myString->length() << std::endl; delete myString; // No GC in C++! 

Hope this helps!

+14
source

Not in Java, when you declare String s, s does not contain the address of the new String, but its value, which in this case is something.

You can see this post for more information on variable address locations in Java, which is kind of pointless.

0
source

In C ++, you use pointers, so if you have:

 int foo = 0; 

you can point to the address of foo with:

 int* bar = &foo; 

This line of code indicates that the value bar is the memory address of foo. Java manipulates objects by reference, and each object is a link, but also automatically collects garbage. If you use the new keyword in C ++, you need to clear the memory yourself:

 float* f = new float(0); // Allocate memory ... delete f; // Free memory 
0
source

Yes, I agree with Joni, the C ++ pointer is syntactically closer to the link in Java.

Another important difference between a pointer and a link in C ++, a pointer can either point to a real object, or it can be zero. A reference in C ++, by definition, always points to a real object or variable. You must assign it to something real when you declare it.

0
source

To answer your question, for best practice, use only the new or malloc operator to allocate space. Assuming you're new to pointers. The big difference between pointers and links is that you must use the explicit operator - the * operator - to dereference a pointer, but you do not use the operator to dereference a link.

0
source

Java options are close to C ++ links. For instance;

  • When you pass an object, you are actually passing a reference value. Therefore, all objects are links in Java.
  • Java, on the other hand, passes methods by value.

You can also check this link.

0
source

In a java link, you can reference values ​​or methods more than once. But in C ++ you can only refer to one value in one reference variable. Thus, in java reference variables, pointers to C ++ are more likely. In addition, java automatically starts the garbage collector, so there is no need to use the delete key, which is used in C ++ to release memory.

0
source

Source: https://habr.com/ru/post/1436389/


All Articles