954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

"Pointers" in Java

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++):

Student bill;
bill = new Student();
bill.GPA = 4.0;


is equivalent to this C++ code:

Student* bill;
bill = new Student;
bill->GPA = 4.0;


Is that a correct "translation"?

CoolGamer48
Posting Pro in Training
401 posts since Jan 2008
Reputation Points: 77
Solved Threads: 40
 

no. Java has no pointers as such.
Everything that's not a primitive is a reference.
While these act in some ways similarly to C++ pointers, they're more like C++ references.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 
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.


Yes, they are called "reference types". The references point to objects.
.You cannot make it so that it is not 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
Yes, they are called primitive types.and nothing you can do will make it 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++):

Student bill;
bill = new Student();
bill.GPA = 4.0;


is equivalent to this C++ code:

Student* bill;
bill = new Student;
bill->GPA = 4.0;


Is that a correct "translation"?
Yes.

bugmenot
Posting Whiz in Training
225 posts since Nov 2006
Reputation Points: 53
Solved Threads: 34
 

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?

agdastidar
Newbie Poster
4 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 

1) it's reference, not refference
2) you don't need pointers to work with filesystems
3) JVMs don't have to be written in C++ or C
4) C/C++ doesn't exist

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 
1) it's reference, not refference


Thanks for the spelling correction

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.

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.

4) C/C++ doesn't exist


This sentence do not have any meaning!!!!! Clarify.

agdastidar
Newbie Poster
4 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 
4) C/C++ doesn't exist

This sentence do not have any meaning!!!!! Clarify.


The logic is that C is a programming language and C++ is a programming language, but C/C++ is a misunderstanding of the difference between C and C++. Edward thinks this is just the language snobs being snobbish, and there are enough legitimate meanings of C/C++ to let it go. ;)

Radical Edward
Posting Pro
545 posts since May 2008
Reputation Points: 361
Solved Threads: 97
 

Edward thinks wrong... :D
If you can't even correctly define your problem domain how are you ever going to figure out a correct solution?

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

I always thought C/C++ meant C or C++...

CoolGamer48
Posting Pro in Training
401 posts since Jan 2008
Reputation Points: 77
Solved Threads: 40
 

nope, it's some kids' idea that C and C++ are actually one and the same language.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 
I always thought C/C++ meant C or C++...


C and C++ are completely different language but "C/C++" always means "C or C++" just like "C++/Java" stands for "C++ or Java" :-)

agdastidar
Newbie Poster
4 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 

C/C++ would be C divided by C++ using integer division.
It would yield nothing, as C++ is (based on) a superset of C, therefore the floating point result (intermediate, to be truncated) will be less than 1.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

Although java doesn't explicitly use pointer, it is fully pointer oriented.
Without pointer java doesn't work.In java, 'this' pointer is used explicitly to access the data member associate with the object.

kokila_mca
Newbie Poster
1 post since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

Do pointers used in java?

venkatesh182
Newbie Poster
1 post since Feb 2011
Reputation Points: 7
Solved Threads: 0
 
Do pointers used in java?


If you read previous post you would have seen this answer

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You