Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
CrusaderSean
Jun 10, 2002
AwakenedDreamer
Alright, apparently I don't understand how primitives and objects are passed to methods in Java. I thought Java only passes values to methods. But if I pass in an object like array, the object's content will change. For example:

code:
public class SimpleClass {
	void scaleLine(int[] l, int s) {
		s = 40;
		l[0] *= s; l[1] *= s;
	}
	
	public static void main(String[] args) {
		int scale = 1;
		int[] line = new int[2];
		line[0] = 1; line[1] = 2;
		System.out.println("scale="+scale+"; Line="+line[0]+","+line[1]);
		
		SimpleClass sc = new SimpleClass();
		sc.scaleLine(line, scale);
		System.out.println("scale="+scale+"; Line="+line[0]+","+line[1]);
	}
}
notice the scaleLine method doesn't return anything, so I figured nothing will change. but the corresponding output is:
code:
scale=1; Line=1,2
scale=1; Line=40,80
so the primitive int scale did not change value, but the array int[] line did. Does this mean objects are passed in as reference by default? Or am I totally missing something here?

Adbot
ADBOT LOVES YOU

CrusaderSean
Jun 10, 2002
AwakenedDreamer

Twitchy posted:

Arrays are themselves Objects, and so are passed by reference. The array may contain primitives (ints, doubles etc), but the structure that holds them is an Object.

but if I change the scaleLine method to the following, the line array doesn't change... so if object parameters are really passed in as reference, shouldn't it point to the new int[] newLine? or is the following code just changing where the reference variable is pointing to and has no effect on the caller? :confused:
code:
	void scaleLine2(int[] l, int s) {
		s = 40;
		int[] newLine = new int[2];
		newLine[0] = -2; newLine[1] = -2;
		
		l = newLine;
	}
edit: ah, k. that makes sense. thanks Twitchy

CrusaderSean fucked around with this message at 00:39 on Apr 25, 2008

CrusaderSean
Jun 10, 2002
AwakenedDreamer

Twitchy posted:

Hah, I think this same question was just asked on the last page, and being a master of words made a mess off explaining it, but basically:

When you call scaleLine2, the REFERENCE (i.e. a place in memory where the object can be found) of l in the main method is assigned to a new variable called l. So when you say "l = newLine" you're basically saying "forget that last reference I gave you, use this one instead". So now your temporary variable l refers to the new int[] you made, where as l in the main method still refers to the same location as before.

So in terms of programming practice, which of the following is better?

void someMethod(Object obj...) { ... passed in object value may change ... }
or
Object someMethod(Object obj...) { ... explicitly return changed object ... }

I see people using the first one because they want to change multiple objects in a method and can't return multiple objects. But in reality it is not really "void" in the sense that inputs can change in value so it might be misleading. Maybe this is a non-issue and everyone knows how reference works in Java... I was just really confused the first time I saw someone do that.

CrusaderSean
Jun 10, 2002
AwakenedDreamer
I'm playing with java.util.Observer and Observable for MVC GUI design. I don't really understand how the listener/observers determine if the state has changed. I know you can run dataModel.hasChanged() to explicitly mark the data state as changed, but when is it necessary to do this? Does java automatically mark observable state as changed or do I have to do it manually?

I redid this simple temperature program here. So when the temperature is raised or lowered, the GUI will call setTemperature function in the model. The model's setTemp function will in turn call the notifyObserver function. The temperature model does not call hasChanged() at all and it works just fine. So Java must have determined temperature state has changed somehow...

Then I created my own example with a more complicated data structure. I'm using the same basic concept for observer and observable. I tried the following two variations:

case 1:
update state parameters, run notifyObserver => observer.update() doesn't run and nothing changes

case 2:
update state parameters AND set model.hasChanged(), run notifyObserver => will run observer.update() and GUI will reflect changes

So does this mean I need to set model.hasChanged() manually for more complicated data models?

CrusaderSean fucked around with this message at 03:25 on Apr 29, 2008

CrusaderSean
Jun 10, 2002
AwakenedDreamer

triplekungfu posted:

No, but is does call setChanged(), which means that any subsequent calls to hasChanged() will return true until clearChanged() is called.


Edit: To clarify, for now, you probably want to call setChanged before every call to notifyObservers.

Hmm.. how did I miss that setChanged() line.. thanks for the clarification.

edit: actually I was using setChanged(), I guess I'm illiterate that late in the night...

CrusaderSean fucked around with this message at 16:11 on Apr 29, 2008

CrusaderSean
Jun 10, 2002
AwakenedDreamer
I'm reading up on design patterns and I saw this interface declaration:
public interface DrawingView extends ImageObserver, DrawingChangeListener {...stuff...}
I thought java doesn't support multiple inheritance?... By the way, DrawingChangeListener is another interface. so interface extends another_interface doesn't really count as inheritance or what?

Adbot
ADBOT LOVES YOU

CrusaderSean
Jun 10, 2002
AwakenedDreamer

csammis posted:

It doesn't for classes, but an interface can extend (and a class can implement) multiple base interfaces.

Ah, k. That makes sense.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply