Solved

how to set the value of the Integer instance?

Posted on 2001-08-03
8
175 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
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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
stackato and cloud 4 85
SHA2 certs for IIS AND Java? 2 91
tomcat startup error 5 65
hibernate jars 4 30
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

778 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