I am trying to convert the C++ programs into Java programs but I am not able to get the syntax of pointers in java programming language. What should I do?
Your question is very vague because you haven't specified what you need the pointers for. If you need pointer arithmetic, the closest thing in Java is the Unsafe
API, which is proprietary and not part of the official JDK API.
If you need to just pass pointers around and dereference them, no syntax for that is needed in Java—actually you must remove the syntax from C++ code to get the equivalent in Java:
ptr->method()
converts to ptr.method()
*ptr
becomes just ptr
&obj
becomes just obj
.
This is because Java exclusively uses pointers to refer to objects, so the syntax simply assumes them.
Firstly, there's no pointers (actually, everything, except primitives, is a pointer, but you want to change passed variable, ain't you?) in Java, so you should think of how you can implement the same functionality without pointers.
If you can't succeed in this, you can make your methods accept something, which holds reference for another variable, for example, AtomicReference<T>
( AtomicInteger
/ AtomicLong
for, correspondingly, int
and long
).
It would be something like:
void changeMyString(AtomicReference<String> ref) {
ref.set("Hello, World!");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.