Java – Best Practices for passing parameters

Suppose I have an application class with about 10 fields In other utility classes, I have one of the following two methods Which of these two works best?

public printUsernameAndGroup(String username,String group){
    sout(username);
    sout(group);
}

public printUsernameAndGroup(Application application){
    sout(application.username);
    sout(application.group);
}

Solution

With regard to performance, two of them are the same If you realize your function

public printUsernameAndGroup(String username,String group){
    sout(username);
    sout(group);
}

Before calling the function, you will access the user name and group properties of the object When you realize it

public printUsernameAndGroup(Application application){
    sout(application.username);
    sout(application.group);
}

Instead of copying all 10 fields and accessing its two properties again, you just pass a reference copy of the object

However, if you consider ease of use, the second one will be more user - friendly

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>