12

I have a code like this

int a,b;
switch(whatever){
    case 1:
      lots_of_lines_dealing_with_variable_a;
    case 2:
      same_lines_but_dealing_with_variable_b;
}

I thought of doing:

int a,b;
pointer_to_int p;
switch(whatever){
    case 1:
      p=a;
    case 2:
      p=b;
}
lots_of_lines_dealing_with_pointer_p;

It would reduce the code to about half the lines, but Java doesn't allow pointers to integers. So, is there any way to approach this?

Edit: The homework is much bigger than just this method. I need to Create a class called "DoubleList" which contains two linked lists in a single Vector. The integers I talk about are the pointers to the start of the Lists, which I need to move to other positions of the Lists when adding or removing elements to the Lists

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
bluehallu
  • 10,205
  • 9
  • 44
  • 61

7 Answers7

19

Maybe I'm missing something, but looks easily solvable to me:

int a,b;

switch(whatever) {
  case 1:
    a = manipulateValue(a);
    break;
  case 2:
    b = manipulateValue(b);
    break;
}

int manipulateValue(int v) {
  // lots of lines dealing with variable v
  return v;
}

If you don't need to modify the variables, then you can leave out the return-value (just use void) and the assignment.

If nothing else needs to call the method, then it should be private (it's a general principle: give as little access as possible, as much as necessary).

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • Well, so that's a another solution for my example, my fault for not thinking in a best one, but couldn't it be done my way? I mean, isn't there anything like a pointer to an Integer in Java? – bluehallu Feb 28 '11 at 16:40
  • @Hallucynogenyc: there's no such thing as a pointer to an `int` in Java. Pointers only exist to reference types (i.e. not for primitive types) in which case they are usually called references (Warning: they don't act like C references, 'though!). And the standard wrapper for `int` is immutable, so having a reference to it doesn't really help. You could use an `AtomicInteger` which is mutable. You'd be kind-of "abusing" it and your code would look a lot less neat (because assignment needs a `set()` call instead of a simple `=`). – Joachim Sauer Feb 28 '11 at 16:44
  • The real situation in my case is that the int I need to deal with is a class variable that simulates a pointer, it's homework and it needs to be in Java. I don't need the value only, those lines are directly modifying that variable. I can't use a method because it would work to read the value, but there are lines that need to modify that integer. Better now? – bluehallu Feb 28 '11 at 16:48
  • In production code, I'd say: use a method and assign the return value to the same variable you pass in (just as I do in my example). For homework, you could write your own mutable `int` wrapper class and pass (and manipulate) that. Or maybe the homework is just for you to learn that there's no direct way to do it ;-) – Joachim Sauer Feb 28 '11 at 16:50
  • The homework is much bigger than just this method haha. I need to Create a class called "DoubleList" which contains two linked lists in a single Vector. The integers I talk about are the pointers to the start of the Lists, which I need to move to other positions of the Lists when adding or removing elements to the Lists. I hope it's all clear now xd – bluehallu Feb 28 '11 at 17:14
  • @hallucynogenyc You could keep Node references, `Node startA; Node startB;` - they act similar to pointers as @joachim's first comment. – corsiKa Feb 28 '11 at 17:50
  • @Hallucynogenyc: Please add your information added here to the question, it is much easier to find there. (There is an edit link for a reason.) – Paŭlo Ebermann Feb 28 '11 at 19:13
6

You could try using boxing.

public class IntBoxer {
    public IntBoxer() {
    }
    public IntBoxer(int startValue) {
        this.value = startValue;
    }
    public int value;
}
IntBoxer a = new IntBoxer();
IntBoxer b = new IntBoxer();
IntBoxer p;
Switch(whatever){
    case 1:
      p=a;
      break;
    case 2:
      p=b;
      break;
}
lots_of_lines_dealing_with_pointer_p.value;
Teo Klestrup Röijezon
  • 5,097
  • 3
  • 29
  • 37
  • 1
    Why would you box using your own class rather than using the `Integer` class? – ubadub Dec 28 '17 at 08:09
  • 3
    @ubadub Integer (and the other built-in primitive box classes) are immutable. That's good when you just want to be able to use it in generics, but not if you want to be able to mutate the value from multiple places. That said, there is also the `AtomicInteger` class, which can be shared safely between threads (but is slower since it has to ensure the correct multi-threaded behaviour). – Teo Klestrup Röijezon Jan 01 '18 at 16:36
4

Yes, it's called "a method":

private int lots_of_lines_dealing_with_value(int x)
{
  .
  .
  .
  return new_value;
}


int a,b;
Switch(whatever){
    case 1:
      p=lots_of_lines_dealing_with_value(a);
      break;
    case 2:
      p=lots_of_lines_dealing_with_value(b);
      break;
}
unwind
  • 391,730
  • 64
  • 469
  • 606
1

Use the Integer class instead of an int.

To give you an example using the code you have:

Integer a,b;
Integer p;
switch(whatever){
    case 1:
      p=a;
    case 2:
      p=b;
}
David Weiser
  • 5,190
  • 4
  • 28
  • 35
1

Why can't you just do:

int a,b;

Switch(whatever){
    case 1:
      f(a);
      break;
    case 2:
      f(b);
      break;
    default:
      // ???
}
PhilDin
  • 2,802
  • 4
  • 23
  • 38
1

Java doesn't have pointers, it has references.

A pointer is a variable that contains a memory address. Typically one dereferences the pointer to obtain the memory needed for some operation.

A reference is an index into a memory management table. Typically the memory management table is protected from direct access. In Java's case the reference can't be manipulated manually, as any read of the variable will result in returning the referenced object.

This has a lot of implications, but is necessary for decent automatic garbage collection. Garbage collection sometimes involves moving objects in memory to create larger areas of free space (for needed objects that cannot fit into the current memory holes). If Java exposed a pointer, then after memory compaction, you might have the "old" address of the memory.

By using references, your reference is guaranteed to stay the same, even if the actual location of memory moves around. Internal to the JVM is a reference to pointer table, but you will never get to see it from a running program; because, if you ever touched it, it would mess up the automatic memory management.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • 2
    More specifically, Java has object references. It does not have primitive type references. – Ted Hopp Feb 28 '11 at 16:38
  • If Java has no pointers, why does it have a `NullPointerException`? ;-) But seriously, even the [JLS (§4.3.1)](http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.3.1) calls says "The reference values (often just *references*) are pointers to these objects [..]". – Joachim Sauer Feb 28 '11 at 16:45
  • Because someone screwed up the name of a class. References are like pointers in someways, but unlike them in others - including unfortunately the way you want to use them. – DJClayworth Feb 28 '11 at 18:30
  • I would say, **Java has *only* pointers** (to objects, not to variables or arbitrary memory), and no references (since references in C++ are something quite a bit different). (The primitives are a bit strange in this respect, but even they could conceptionally be treated like pointers (to immutable objects).) As a difference to C, there is no pointer arithmetic. – Paŭlo Ebermann Feb 28 '11 at 19:11
  • @Paŭlo: The JLS uses both "pointer" and "reference" (or "reference value"). And really, those two terms are pretty much interchangeable. Just because C/C++ has chosen to let "reference" mean something different and add some contraint/meaning to that term doesn't make it so. Java pointers/references are not C pointers and they are not C references. They are Java pointers. – Joachim Sauer Feb 28 '11 at 20:39
  • @Joachim: Yeah, the exact word does not really matter. I still think Javas pointers/references by behaviour are more similar to C/C++ pointers than references, but this does not really matter for this question. The important thing here is that there are only pointers/references to (Java) objects, not to variables or memory locations. – Paŭlo Ebermann Feb 28 '11 at 20:49
  • Java pointers are nothing like C/C++ pointers because this particular operation is basic to a C/C++ pointer, yet 100% denied to a Java pointer: `char* pointer = pointer++;` Nor can you do `pointer = pointer + 4;` C/C++ style pointers accept this as they store memory location values, and both the starting and ending values of a basic mathematical manipulation is valid. Java's references have nothing to do with the location in memory, so manipulating them in any way other than type-checked assignment is meaningless, dangerous, and not allowed. – Edwin Buck Mar 01 '11 at 05:58
0

Well, for this scenario:

boolean useA;
int a, b;

if(useA)
      lots_of_lines_dealing_with_variable_a;
else
      same_lines_but_dealing_with_variable_b;

you can use:

boolean useA;
int a, b;

int value = useA ? a : b;

// lines_dealing_with_value

if(useA)
    a = value;
else
    b = value;

But really. Methods!

Eric
  • 95,302
  • 53
  • 242
  • 374