Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to set the value of the Integer instance?

Posted on 2001-08-03
8
Medium Priority
?
186 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 40 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 

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

Independent Software Vendors: 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

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

688 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