Solved

how to set the value of the Integer instance?

Posted on 2001-08-03
8
173 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
 

Author Comment

by:eugeneng
ID: 6347972
so, is String an immutable class also ?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
array11 challenge 16 52
wordlen challenge 3 49
wordmultiple challenge 12 92
Groovy:unable to resolve class error 2 32
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now