The Artima Developer Community
Sponsored Link

Java Answers Forum
Help With Using Java Pointers/Reference Variables

3 replies on 1 page. Most recent reply: Jun 10, 2005 8:38 AM by ziyuan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
andy

Posts: 6
Nickname: chant9
Registered: Mar, 2005

Help With Using Java Pointers/Reference Variables Posted: Mar 29, 2005 11:28 AM
Reply to this message Reply
Advertisement
Hi im trying to implement a type of pointer but i cant work out how to do it. I found some code that seemed to suggest that it was something like below, but it doesnt work.

int x =10;
int y = new int(x);

Then when altering y, x would also be altered.

If anyone knows how this is done please let me know.

Thanks


Justin

Posts: 1
Nickname: zallambo
Registered: Jun, 2005

Re: Help With Using Java Pointers/Reference Variables Posted: Jun 9, 2005 6:12 PM
Reply to this message Reply
Someone correct me if I'm wrong, but there is absolutely no way to create a pointer to any primitive data type or function in Java. One can create an Integer object, and assign it a value, but it doesn't work exactly like an int, and cannot reference an int. I am switching to C++ on a current programming project because of Java's lack of pointers.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Help With Using Java Pointers/Reference Variables Posted: Jun 9, 2005 11:00 PM
Reply to this message Reply
i = 14 would change i, not the content of the space in memory where the value of i is located. Therefore it would be against the rules of object oriented programming.
C++ does SUPPORT object oriented programming, but it's not an object oriented language


There are 2 solutions for this problem.

1. The simple way: use a array with length 1.
a = new int[]{10};
b = a;
altering a[0] will also change b[0]
This solution works, but it's not the way you should do it.

2. The object oriented way: create a class storing a int value
public class MyInteger{
  public MyInteger() {
    this(0);
  }
  public MyInteger(int val) {
    iVal = val;
  }
  public int get() {
    return iVal;
  }
  public void set(int newValue) {
    iVal = newValue;
  }
  private int iVal;
}


That would be the way it should be done if you really want to program following the rules of object oriented languages.


Try programming in SmallTalk. SmallTalk absolutely offers no way to alter a variable from the outside. You can only tell the variable to alter itself in a defined way.
This language really is for hard core programmers.

ziyuan

Posts: 2
Nickname: ziyuan
Registered: Jun, 2005

Re: Help With Using Java Pointers/Reference Variables Posted: Jun 10, 2005 8:38 AM
Reply to this message Reply
Yes! Java has not pointer,but you can use a mechanism similar pointer(called "reference itself of class").

You can refer to the book that named "data structure in Java"(may be).

Now,i get you example:

class Node{
private int data;
private Node next; // reference Node class itself

Note(int d){
/* method body */
}
}

Flat View: This topic has 3 replies on 1 page
Topic: Applet Problems.. Previous Topic   Next Topic Topic: Can't run Java Jackcess

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use