| | |
"Pointers" in Java
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Hey,
I'm a C++ programmer, but I'm beginning to learn Java, and from what I see so far the two languages are very similar. The main difference I hear is that C++ has pointers and Java doesn't, but from my understanding Java does have pointers in a sense, the user is just not aware of it.
From what I've read in my book so far, I am under the impression that when you declare a variable of a complex data type (not sure if that's the right word, I mean like a self-defined class vs. an int) it is automatically a pointer. You cannot make it so that it is not a pointer. And when you declare a variable with a simple data type, it is not a pointer, and nothing you can do will make it a pointer? Is this assumption correct?
Like, this java code (sorry if there are minor syntax errors, as I'm used to C++):
is equivalent to this C++ code:
Is that a correct "translation"?
I'm a C++ programmer, but I'm beginning to learn Java, and from what I see so far the two languages are very similar. The main difference I hear is that C++ has pointers and Java doesn't, but from my understanding Java does have pointers in a sense, the user is just not aware of it.
From what I've read in my book so far, I am under the impression that when you declare a variable of a complex data type (not sure if that's the right word, I mean like a self-defined class vs. an int) it is automatically a pointer. You cannot make it so that it is not a pointer. And when you declare a variable with a simple data type, it is not a pointer, and nothing you can do will make it a pointer? Is this assumption correct?
Like, this java code (sorry if there are minor syntax errors, as I'm used to C++):
java Syntax (Toggle Plain Text)
Student bill; bill = new Student(); bill.GPA = 4.0;
is equivalent to this C++ code:
C++ Syntax (Toggle Plain Text)
Student* bill; bill = new Student; bill->GPA = 4.0;
Is that a correct "translation"?
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
•
•
Join Date: Nov 2006
Posts: 224
Reputation:
Solved Threads: 31
•
•
•
•
I am under the impression that when you declare a variable of a complex data type (not sure if that's the right word, I mean like a self-defined class vs. an int) it is automatically a pointer.
.
You mean that you cannot have objects as values, then yes.
•
•
•
•
And when you declare a variable with a simple data type, it is not a pointer
Basically. There is no way to "take the address" of a variable. But you can copy the value of the variable into a field of a wrapper object, which then can be pointed to by a reference.
•
•
•
•
Like, this java code (sorry if there are minor syntax errors, as I'm used to C++):
java Syntax (Toggle Plain Text)
Student bill; bill = new Student(); bill.GPA = 4.0;
is equivalent to this C++ code:
C++ Syntax (Toggle Plain Text)
Student* bill; bill = new Student; bill->GPA = 4.0;
Is that a correct "translation"?
•
•
Join Date: May 2008
Posts: 4
Reputation:
Solved Threads: 0
First you must think whether a 'Refference' and a 'Pointer' are equivalent. Centainly not. And Java always use refference, not pointer (at least from programmer's point of view).
But I am not sure what happens inside JVM when it executes a class file. Since JVM is mainly written in C/C++, it is quite possible inside JVM somewhere (or in many places) pointer is used. Otherwise how can JVM adapter can access a file/directory or many things else?
But I am not sure what happens inside JVM when it executes a class file. Since JVM is mainly written in C/C++, it is quite possible inside JVM somewhere (or in many places) pointer is used. Otherwise how can JVM adapter can access a file/directory or many things else?
•
•
Join Date: May 2008
Posts: 4
Reputation:
Solved Threads: 0
Java Syntax (Toggle Plain Text)
1) it's reference, not refference
Thanks for the spelling correction
Java Syntax (Toggle Plain Text)
2) you don't need pointers to work with filesystems
Not in java but in C/C++. Pointers are often use in C/C++ to access files.
Java Syntax (Toggle Plain Text)
3) JVMs don't have to be written in C++ or C
Most Java Virtual Machines are written in C++, but at least one is written almost entirely in Java (Jalapeno from IBM). However other languages could also be used.
Java Syntax (Toggle Plain Text)
4) C/C++ doesn't exist
This sentence do not have any meaning!!!!! Clarify.
•
•
•
•
Java Syntax (Toggle Plain Text)
4) C/C++ doesn't exist
This sentence do not have any meaning!!!!! Clarify.
If at first you don't succeed, keep on sucking until you do succeed.
![]() |
Other Threads in the Java Forum
- Previous Thread: Floating Dock Window for Windows
- Next Thread: Random Acces Helppppppp
| Thread Tools | Search this Thread |
-xlint android api applet application array arrays automation bi binary blackberry block bluetooth chat class classes client code compile compiler component database developmenthelp draw eclipse error event exception fractal freeze game gameprogramming givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javac javaprojects jetbrains jni jpanel jtable julia learningresources lego linux list login loop loops mac map method methods mobile netbeans newbie notdisplaying number online oracle page print problem program programming project qt recursion scanner screen server set singleton size sms sort sql string swing system template textfields threads time title tree tutorial-sample update variablebinding windows working xor







