How do Java "pointers" work?

Let's say this is C ++ code:

void change(int& x){ x++; } 

or

 void change2(int* a){ *a++; } 

Both will change global x, right?

So how can I do something like this in java?
In particular, I want to point to a vector object

But since Java has no pointers, I'm not sure what to do.
From an internet search, I saw people say that Java does this in some other way, but I did not find any real example. Thanks help!

+6
source share
6 answers

In Java, you have object references instead of pointers. You cannot pass a primitive type by reference, but you can wrap a primitive type inside an object, and then pass a reference to that object.

Java provides an Integer type that wraps an int , however this type is immutable, so you cannot change its value after construction. However, you could use MutableInt from Apache Commons:

 void change(MutableInt x) { x.increment(); } 

A change to x will be visible to the caller.


In particular, I want to point to a Vector object

When you write Vector v = ...; , you assign a vector reference to the variable v . A link in Java is very similar to a pointer. Links are actually implemented using pointers.

Java uses a pass by value. When you pass a vector to a method, you are actually copying the link to that vector. It does not clone the vector itself. Therefore, passing a link in Java is very similar to passing a pointer to C ++.

+12
source

With Java, you cannot pass primitive types, such as int by reference, they are passed only by value.

The only thing you can do is find tricks to do this, because instead of Objects are passed by reference. Here are two examples.

Use an array with a single value, e.g.

 int[] value = new int[1]; value[0] = 2; // call a method obj.setValue(value); // and in setValue public void setValue(int[] value) { value[0] = 5; } 

Or the second approach uses a holder class:

 public class Holder<T> { public T value; public Holder(T value) { this.value = value; } } // then use it in this way Holder<Integer> h = new Holder<Integer>(2); obj.setValue(h); // and in setValue public void setValue(Holder<Integer> h) { h.value = 5; } 

In this case, I use the class classes of the class with generics, but you can also have a simple holder, just for the whole. For instance:

 public class IntHolder { public int value; public IntHolder(int value) { this.value = value; } } 
+4
source

Java always passes by value and global variables do not exist, as in the sense of C ++. Therefore, if you want to do the same as in C ++, you need to return a new value.

Thusly:

 public int change(int x) { return ++x; // or // return x + 1; } 

To check this:

 int x = 2; change(x); System.out.println(x); // returns 2 x = change(x); System.out.println(x); // returns 3 

Thus, it does not make sense to allow the method to be called change ; it is more intelligent along the lines of calculateThisInt .


Java does pass objects by value. But since Mark Byers mentions that the Integer class is immutable, and you can use the MutableInt from the Apache Commons library. To describe how this works, you can implement it yourself for your example:

 public class MyInt() { public int i; public void setInt(int i) { this.i = i; } public int getInt() { return this.i; } public int increment() { this.i++; } } 

You need to change your change function so that the MyInt object is specified as an MyInt :

 public void change(MyInt i) { i.increment(); } 

Using:

 MyInt x = new MyInt(); x.setInt(2); change(x); System.out.println(x.getInt); // returns 3 

In your case, you want to change the Vector object ...

 public void changeVector(Vector v) { // anything you do with 'v' will change it even // for the scope that called this method } // Usage: Vector v = new Vector(); changeVector(v); // v should be changed after calling change vector method 

Hope this all makes sense.

+1
source

While you cannot replace the object passed to the function, you can change its state by changing fields directly or calls. If you need something like a pointer to a primitive, wrap it in an object. To follow your code, you can do this:

 public class IntPointer { public int value; public IntPointer(int value) { this.value = value; } } 

Then in another place you could say:

 public static void change(IntPointer ipoint) { ipoint.value++; } public static void main(String[] args) { IntPointer a = new IntPointer(10); change(a); } 

It may seem a little uncomfortable, but it does not suit me as often as you think. I would be more likely to do something like this:

 public class ABPair { private int a = 0; private int b = 0; public static void changeA() { a++; } public static void changeB() { b++; } } 

So in another place I can say:

 public static void main(String[] args) { ABPair ab = new ABPair(); if (ACondition) { ab.changeA(); } } 

In other words, my data, as a rule, is already wrapped in some kind of object, and I tend to use the methods of data objects to notify of any changes.

0
source

Both will change global x, right?

So how can I do something like this in java? In particular, I want to specify a Vector object

The question is somewhat vague, but I got the impression that in the end you want a global Vector that you can store things?

Many ways to do this, but one of the easiest is to have a static field in the class with public static methods to access it. (Or just a public static field that is accessed directly, but it really won't be idiomatic in Java.)

 public class Foo { private static List<Integer> globalVector = new Vector<Integer>(); public static void add(int number){ globalVector.add(number); } // ... plus whatever other accessors to the global list that you need } 

Elsewhere in the code:

 Foo.add(23); // modifies the global vector 

(Btw, Vector is deprecated, and we will usually use an ArrayList in its place now. As Javadoc says, this has been modified to implement the List interface, which I also used in this example.)

0
source

Java supports what it calls "links." Links act just like pointers in C / C ++. They do not act like "links" work in these languages.

The main differences between a pointer in C and a link in Java:

You cannot perform pointer arithmetic in Java (that is, you cannot β€œadd” or β€œsubtract” from a Java link, you can only dereference it or compare it with another). You cannot attribute it to an incompatible type: Java is strictly safe, you cannot "re-interpret" bytes in memory as some other object. For some uses of pointers, this has no real effect (for example, linked lists work almost the same in both languages), for others, the difference is quite large (arrays in C are just bizarre arithmetic of pointers, in Java they work very differently).

Thus, Java links could be called "restricted pointers."

0
source

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


All Articles