Home > Java > javaTutorial > A brief analysis of the problem of Integer parameter passing method in Java

A brief analysis of the problem of Integer parameter passing method in Java

高洛峰
Release: 2017-01-16 16:12:54
Original
1614 people have browsed it

Java itself is a value-passing call, and the address value is passed to the object. Reassigning the address value is equivalent to repointing and will not affect the outer layer.
And the Integer object here also has special characteristics. In fact, the implementation may be similar to

class Integer{
final int value; //一旦赋值,就不能改变。
}
Copy after login

This shows up: the address value passed when calling cannot change the outer + object itself and cannot change. As a result, this value cannot be changed


There are many solutions
1. The Java style is to use the return value for a single value. return i; assign i=foo(); outside; use arrays or objects for multiple values.
2. Pass your own encapsulation class. class MutableInteger{ int value;}
3. Pass the special AtomicInteger atomic integer object

    public static void main(String[] 参数) {     
  AtomicInteger i=new AtomicInteger(40);
  i.intValue();
  System.out.println(i);
 }
    public static void change(AtomicInteger i) {
     i.set(55);
}
Copy after login

You can also change the value after passing it.
Recommended solution 1, try to avoid

Change Please pay attention to the PHP Chinese website for more related articles on the problem of Integer parameter passing in Java!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template