Home > Java > javaTutorial > body text

How to understand how to use final to modify parameters in JAVA methods

坏嘻嘻
Release: 2018-09-15 15:09:59
Original
1850 people have browsed it

The content of this article is about how to understand the parameters in JAVA methods and use final to modify them. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

The reason why parameters in JAVA methods are modified with final

Many people say that the reason for using final to modify method parameters in JAVA is to prevent method parameters from being tampered with when calling. In fact, it is not This is the reason, but there may be ambiguities in understanding. Some people think that the actual value of the variable where the statement is called will not be modified. Another understanding is that it cannot be modified only within the calling method.

In fact, the first understanding is wrong. For basic types, it has the same effect whether it is modified with final or not at the place of call, such as the following code:

publi cstatic void main(String hh[])
      {
            int i = 1;
            System.out.println(i);
            checkInt(i);
            System.out.println(i);
      }
      public static void checkInt(final int i)
      {
            //do something
      }
Copy after login


You set the parameters in the checkInt() method to final and non-final effects It's the same for the calling place.

However, it is the same for reference types. Whether it is modified or not, the reference address will not be changed, but the attribute value of the reference variable can be changed. As follows:

  publicstaticvoid main(String hh[])
      {
            LoginInfo login = new LoginInfo();
            login.setPassword("1235");
            login.setUserName("mygod");
            System.out.println("username:"+login.getUserName()+",password:"+login.getPassword());
            checkLoginInfo(login);
            System.out.println("username:"+login.getUserName()+",password:"+login.getPassword());
      }
     
      publicstaticvoid checkLoginInfo(final LoginInfo login)
      {
            login.setUserName("yun");
      }
Copy after login


##For the second statement , it looks like this, I gave this parameter, you can only use the value of this parameter, you cannot modify it, it is the same for basic types and reference types, as follows:

//如果不是final 的话,我可以在checkInt方法内部把i的值改变(有意或无意的,
      //虽然不会改变实际调用处的值),特别是无意的,可能会引用一些难以发现的BUG
      publicstaticvoid checkInt(int i)
      {
            i = 200;//这样是可以的,不会编译出错的
            //do something
      }
 
      //如果是final 的话,我可以在checkInt方法内部就没办法把i的值改变
      //可以完全避免上面的问题
      publicstaticvoid checkInt(finalint i)
      {
            i = 200;//这样是不可以的,会编译出错的
            //do something
      }
 
      //final 的引用类型方法参数
      publicstaticvoid checkLoginInfo(final LoginInfo login)
      {
            login = new LoginInfo();//error,编译不过去
            //do something
      }
      //非final的引用类型方法参数
      publicstaticvoid checkLoginInfo(LoginInfo login)
      {
            //没有任何问题,但是肯定不符合此参数存在的初衷
            login = new LoginInfo();
            //do something
      }
Copy after login

The above is the detailed content of How to understand how to use final to modify parameters in JAVA methods. For more information, please follow other related articles on the PHP Chinese website!

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