-2

How can I change two values concurrently. I am writing a code for b-tree in java. But i am unable to modify two variables at a time. Like in C , we do so by using pointers. But how to do so in java? I hope my question is understandable.

provokoe
  • 178
  • 2
  • 10
  • 5
    `Like in C , we do so by using pointers`: how? do you mean if you update a pointer value, all other ponters pointing to same address are also updated? – Azodious Jan 16 '12 at 10:19
  • 2
    The JVM will only process one instruction at a time. You can't modify two variables concurrently. Unless you're talking about using threads? – Jivings Jan 16 '12 at 10:20
  • 1
    Some sample (pseudo-)code and/or a more specific example would be useful. – Anthony Grist Jan 16 '12 at 10:21
  • 1
    ya we can modify two or more variables using pointers. In b-tree, one node breaks up to form two. Its easy to implement this function in c, but in java, lack of pointers makes it a tough question. Hope the people familiar with b-tree can understand my problem – provokoe Jan 16 '12 at 10:26
  • 1
    All non-primitive variables in Java are references (another name for a pointer) to an object. It should work the same way as with pointers in C. Without seeing any code, it's hard to guess what you mean. – JB Nizet Jan 16 '12 at 10:27
  • Or I can ask the same question in different wordings. How can I achieve those features in java which i can get in c using pointers? but this will be broad question to cover up here – provokoe Jan 16 '12 at 10:29
  • @Partha: do you read the comments? Java variables which reference objects **are** pointers. Show us your code. – JB Nizet Jan 16 '12 at 11:03
  • +1 to JB Nizet. Pointers and references are two ways to say the same thing: a reference to an object/value in memory. – Peter Bratton Jan 16 '12 at 13:44
  • -2 to all of you. If you can use references everywhere then what is the use of pointers. Think before answering – provokoe Jan 19 '12 at 05:50
  • what ever we can do by pointers can be done by references but it needs more line of code i feel. – Trying May 11 '13 at 23:59

3 Answers3

1

If one of them is an integer you could use an AtomicStampedReference. See here for a definition.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
0

This will help you writing b-tree code.

nearest substitute for a function pointer in Java

You can also use cloning.

Community
  • 1
  • 1
Muhammad Imran Tariq
  • 22,654
  • 47
  • 125
  • 190
0

I'm not sure I completely understand your question, but setting an object reference in Java is atomic. You can use that to change a set of multiple values at a time, e.g.:

class BTreeState {
    int foo;
    long bar;
    String whatever;
}

//here you change foo, bar, whatever all at once
state = new State(1, 2L, "something");
Kosta
  • 812
  • 1
  • 7
  • 13