Solved

how to set the value of the Integer instance?

Posted on 2001-08-03
8
178 Views
Last Modified: 2010-03-31
how to set the value of the Integer instance? for example, how to make the following Update work ?


class m
{
  public static void Update(Integer i)
  {
   // how to update the value of i to some arbitrary value
   // say, 999, i = new Integer(999) won't work.
  }
}
0
Comment
Question by:eugeneng
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 3

Expert Comment

by:yuri1976
ID: 6347929
this won't work because you can't change the reference of the external value i. This is what you do when you say i = new Integer(999).
And as Integer is a immutable class (you can't change the contents), you can't change the value of i also. The only way to do such a action is to calculate the new value and return it.

public static Integer Update(Integer i) {
  // do your calculations on i
  // and return the result
  return new Integer(999);
}
0
 

Expert Comment

by:isond
ID: 6347934
You need to make i a class variable not a parameter.

try this

class mytest
{

  private Integer i=new Integer(5);

  public void Update()
  {
     this.i=new Integer(999);
  }

  public Integer getVal()
  {
     return this.i;
  }

}

Then call the class using code similar to the following.

public static void main(String args[])
{
      mytest m = new mytest();
      Integer i = m.getVal();
      System.out.println("i=" + i.toString());
      m.Update();
      i = m.getVal();
      System.out.println("i=" + i.toString());

   }
}

Hope this helps.

Darren.
0
 
LVL 9

Accepted Solution

by:
yongsing earned 10 total points
ID: 6347957
In Java, variables, whether they are primitive types or reference types, are passed by values. When a reference type variable is passed to a method, a copy of that reference is made available to the method. Whatever you do to that copy has no effect on the actual reference.

For example:

Integer a = new Integer(100);

'a' is now a reference to an Integer instance.

Update(a);

When you call Update(), a copy of 'a' is passed to that method. Inside Update(), this copy is assigned to a variable called 'i'. When you change the value of i (i = new Integer(999)), you are only changing the value of the copy of 'a', not the actual 'a'. Therefore, when the method returns, 'a' still have the same value, i.e. the same reference to the Integer instance.

What you can do is for Update to return a new Integer instead. Example:

public static Integer Update(Integer i)
{
   int i = i.intValue(); // get the original int value
   i += 2; // update to some other value
   return (new Integer(i)); // return the new value as a new instance of Integer
}

Now you can call the method as:

Integer a = new Integer(100);
a = Update(a); // update 'a'
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:eugeneng
ID: 6347972
so, is String an immutable class also ?
0
 

Author Comment

by:eugeneng
ID: 6347986
so, is String an immutable class also ?
0
 
LVL 9

Expert Comment

by:yongsing
ID: 6347987
Yes, String is an immutable class. You can't change its content.
0
 

Author Comment

by:eugeneng
ID: 6348022
so, is String an immutable class also ?
0
 

Author Comment

by:eugeneng
ID: 6348035
sorry about the double posting!!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question