"Pointers" in Java

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2008
Posts: 401
Reputation: CoolGamer48 is on a distinguished road 
Solved Threads: 40
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Pro in Training

"Pointers" in Java

 
0
  #1
May 16th, 2008
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++):
  1. Student bill;
  2. bill = new Student();
  3. bill.GPA = 4.0;

is equivalent to this C++ code:
  1. Student* bill;
  2. bill = new Student;
  3. 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...".
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: "Pointers" in Java

 
0
  #2
May 17th, 2008
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.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 224
Reputation: bugmenot is an unknown quantity at this point 
Solved Threads: 31
bugmenot bugmenot is offline Offline
Posting Whiz in Training

Re: "Pointers" in Java

 
0
  #3
May 18th, 2008
Originally Posted by CoolGamer48 View Post
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.
.
Originally Posted by CoolGamer48 View Post
You cannot make it so that it is not a pointer.
You mean that you cannot have objects as values, then yes.

Originally Posted by CoolGamer48 View Post
And when you declare a variable with a simple data type, it is not a pointer
Yes, they are called primitive types.

Originally Posted by CoolGamer48 View Post
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.

Originally Posted by CoolGamer48 View Post
Like, this java code (sorry if there are minor syntax errors, as I'm used to C++):
  1. Student bill;
  2. bill = new Student();
  3. bill.GPA = 4.0;

is equivalent to this C++ code:
  1. Student* bill;
  2. bill = new Student;
  3. bill->GPA = 4.0;

Is that a correct "translation"?
Yes.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 4
Reputation: agdastidar is an unknown quantity at this point 
Solved Threads: 0
agdastidar agdastidar is offline Offline
Newbie Poster

Re: "Pointers" in Java

 
0
  #4
May 19th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: "Pointers" in Java

 
0
  #5
May 19th, 2008
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
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 4
Reputation: agdastidar is an unknown quantity at this point 
Solved Threads: 0
agdastidar agdastidar is offline Offline
Newbie Poster

Re: "Pointers" in Java

 
0
  #6
May 26th, 2008
  1. 1) it's reference, not refference
  2.  

Thanks for the spelling correction

  1. 2) you don't need pointers to work with filesystems
  2.  


Not in java but in C/C++. Pointers are often use in C/C++ to access files.

  1. 3) JVMs don't have to be written in C++ or C
  2.  

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.

  1. 4) C/C++ doesn't exist
  2.  

This sentence do not have any meaning!!!!! Clarify.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 351
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Solved Threads: 62
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz

Re: "Pointers" in Java

 
0
  #7
May 26th, 2008
Originally Posted by agdastidar View Post
  1. 4) C/C++ doesn't exist
  2.  

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.
If at first you don't succeed, keep on sucking until you do succeed.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: "Pointers" in Java

 
0
  #8
May 27th, 2008
Edward thinks wrong...
If you can't even correctly define your problem domain how are you ever going to figure out a correct solution?
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 401
Reputation: CoolGamer48 is on a distinguished road 
Solved Threads: 40
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Pro in Training

Re: "Pointers" in Java

 
0
  #9
May 27th, 2008
I always thought C/C++ meant C or C++...
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: "Pointers" in Java

 
0
  #10
May 28th, 2008
nope, it's some kids' idea that C and C++ are actually one and the same language.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC