Solved

how to set the value of the Integer instance?

Posted on 2001-08-03
8
182 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

628 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