• Post Reply Bookmark Topic Watch Topic
  • New Topic

Setting value of Integer object  RSS feed

 
Tom McCann
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK - this should be really easy. How do you set the value of an EXISTING Integer object? i.e. without using the constructor?

I want a function to return two integer values. Obviously I cant pass 2 ints to the function because parameters are passed-by-value.

To get around this I will pass 2 Integer objects. But I can't see any way to set the value apart from in the constructor which won't help me here.

There must be an easy way to do this.

Thanks for your help.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The java.lang.Integer by itself, once created, is inmutable. Maybe You can store the objects in an array or a collection. Some kind of mutable object that you can use to manipulate a reference after the method returns.

This works to me

[ October 11, 2006: Message edited by: Edwin Dalorzo ]
 
Tom McCann
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edwin,

Thanks very much for replying.

I did a bit more research on the net and I found out that my wider requirement, for Multiple Return Values (MRVs) from a function, is a highly contentious issue. Some programmers argue for, some against. In any case, Java doesn't support MRVs.

As you rightly suggest, the preferred way to return MRVs in Java is to create and return a custom object, setting the values in the called function, and then 'getting' those values after returning. This is obviously required for when the return values are of different types.

In my case though, I really wanted 2 integers to be returned so your example using an array is very useful.

Many Thanks,

Tom


Edwin - if you will forgive me for changing the subject to the contents of your tagline, evolution is NOT random. If it were then I wouldn't believe in it either. There are plenty of good books on the subject (try Richard Dawkins) or look up any number of articles on the web (try Wikipedia).

I really don't want to start a discussion on evolution, but to use your own analogy, remember that evolution gives CONSTANT feedback - i.e. the individual lives to breed or it dies. So, your analogy of bashing away at the keyboard would be more accurate if the computer gave you feedback after EVERY keystroke (i.e. if it told you "that's useful" or "that's nonsense"). Only then, and given enough time, could you generate an operating system (and you would need a very very long time).

By the way, some people have achieved similar results. Look up Genetic Algorithms on Wikipedia.

Anyway, thanks again for taking the time to respond. Much appreciated.

Tom
[ October 11, 2006: Message edited by: Tom McCann ]
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With very much pleasure, Tom.

Hey, you start typing and when you get it I'll believe you. And then we start talking about where did you get the matter for your keyboard.

Just kidding, let's not talk about this here. In fact I will change my lines right away.

Success and I will see you around, pal!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!