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"?

Recommended Answers

All 15 Replies

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.

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.

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?

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

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.

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. ;)

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?

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

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

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

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.

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.

Do pointers used in java?

If you read previous post you would have seen this answer

check the dates of when was posted. this is an ancient thread, that nobody was still following.

commented: Closed for ever ;) +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.