3

So I've been thinking (while reading this Java pdf)...

I know this may seem silly but why can't I do this in c++.

float &f = new float;

Isn't this saying the reference of f is the address of new float?

In Java, I see something like this

String s = new String("something")

String s is called a string reference.

Does the word 'reference' in Java have the same meaning as in C++?

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
purepureluck
  • 1,271
  • 2
  • 14
  • 16
  • 3
    In C++ terms, all Java variables that are declared with an object type are in fact *pointers* to the object, and there are *no* references in Java, just pointer passing (by value). (Think: can you implement a `swap` function?) – Kerrek SB Feb 07 '12 at 23:16
  • See: http://stackoverflow.com/a/5174827/14065 – Martin York Feb 07 '12 at 23:57
  • Have a look at this link, http://digitalneuron.in/blog/get/1 – Xinus Jun 17 '13 at 16:48

7 Answers7

14

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

  • Change which object it refers to
  • Check whether two references are equal or unequal
  • Send messages to the referenced object.

In C++, pointers have these same properties. As a result, the code you're looking for in C++ is something like

float* f = new float;

Which is perfectly 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's another string!");
System.out.println(myString.length());

would map to this C++ code:

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's another string!");
std::cout << myString->length() << std::endl;

delete myString; // No GC in C++!

Hope this helps!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • The best fit imho would be a pointer to a constant object, i.e. `const float* f` as assigning a new value to a memory address isn't allowed in java either. – Voo Feb 07 '12 at 23:32
  • @Voo- That pointer would mean that you can't change the `float` being pointed at, and in Java references can absolutely be used to change the object. Think about `ArrayList` references - if you couldn't change the `ArrayList`, you couldn't call `add`, for example. Or am I misinterpreting what you're saying? – templatetypedef Feb 07 '12 at 23:34
  • Yeah you should think about a constant pointer as in c, not c++ - i.e. ignore const methods. In that sense: Without the const `float* f` would allow us to do `*f = 1.0` which won't work in java. The extended const concept from c++ just doesn't map to java at all.. sadly as I think it's quite useful. – Voo Feb 07 '12 at 23:38
  • (In Java) Set a reference to NULL. – Martin York Feb 07 '12 at 23:54
  • If you use shared pointer you get the same functionality as Java. – Martin York Feb 07 '12 at 23:55
0

In java reference, you can refer values or methods more than one times. But in c++,you can refer only one value in a single reference variable. So in java reference variables are more likely pointers of c++.Moreover java runs garbage collector automatically, so there is no use of delete key which is used in c++ to release memory.

0

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

You can take a look at this post for more information about variable address locations in Java which is kind of meaningless.

user1084509
  • 1,832
  • 10
  • 33
  • 48
0

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

int foo = 0;

you can then have a pointer refer to the address of foo with:

int* bar = &foo;

This line of code says that the value of bar is foo's memory address. Java manipulates objects by reference and every object is a reference but it also collects garbage automatically. If you use the new keyword in C++ you need to clean up the memory yourself:

float* f = new float(0);  // Allocate memory
...                       
delete f;                 // Free memory
Foggzie
  • 9,691
  • 1
  • 31
  • 48
0

Yes, I agree with Joni, a c++ pointer is syntactically closer to a reference in Java.

The other important difference between a pointer and a reference in C++, a pointer can either point to a real object or it can be equal to null. 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.

larboyer
  • 119
  • 1
  • 3
0

To answer your question, for best practices only use the new or malloc operator to allocate space. Assuming that you are new to pointers, The big difference between pointers and references is that you must use an explicit operator-the * operator-to dereference a pointer, but you don't use an operator to dereference a reference.

Frantz Romain
  • 836
  • 1
  • 6
  • 14
0

Java parameters are close to C++ references. For instance;

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

You can also check this link out.

meandbobbymcgee
  • 341
  • 2
  • 5
  • 12