Are there, in any way, shape, or form, pointers in java like there are in C and C++?
If so, do I declare them the same way as in C++?
My 873 page book doesn't explain pointers.

Are there, in any way, shape, or form, pointers in java like there are in C and C++?
If so, do I declare them the same way as in C++?
My 873 page book doesn't explain pointers.

Technically, all reference-variables in Java are pointers.

The only differences are that you can't perform mathematical operations on them to change their addresses.


For example, consider the following--

public class DriverProgram_1{



	public static void main(String[] args){

		DriverProgram_1 dp = new DriverProgram_1();
        DriverProgram_1.Other o = dp.new Other(), o2 = dp.new Other(), o3 = null;


		System.out.println(o3 + "    " + o2);
		o3 = o2; // pointer o points to address of o2
		System.out.println(o3 + "    " + o2);

		o3.number = 5; // assigning value 5 to Other reference pointed to by o3

		System.out.println(o2.number); // printing out the result



		final DriverProgram_1.Other o4 = o; // acts as a reference declaration, i.e - DriverProgram_1::Other &o4 = *o;
		System.out.println(o4 + "  " + o);


		o4.number = 1;

		System.out.println(o.number);

	}

	public class Other{

		public int number = 0;
	}
}

And here's a C++ version--

#include <cstdlib>
#include <iostream>

using namespace std;

class DriverProgram_1{
      
	public:
           
           class Other{
		         public:
                        
                      int number;
                      Other() : number(0) {}
           };
};

int main(){
    
		DriverProgram_1 *dp = new DriverProgram_1;
        DriverProgram_1::Other *o = new DriverProgram_1::Other,
         *o2 = new DriverProgram_1::Other, *o3 = NULL;


		cout << o3 << "    " << o2 << endl;
		o3 = o2; // pointer o points to address of o2
		cout << o3 << "    " << o2 << endl;

		o3->number = 5; // assigning value 5 to Other reference pointed to by o3

		cout << o2->number << endl; // printing out the result



		DriverProgram_1::Other &o4 = *o; // acts as a reference declaration, i.e - DriverProgram_1::Other &o4 = *o;
		cout << &o4 << "  " << o << endl;


		o4.number = 1;

		cout << o->number << endl;    
    
    cin.get();
    return 0;
}
This article has been dead for over six months. Start a new discussion instead.