Java function parameter validity check

Source: Internet
Author: User


This work is licensed using the "knowledge sharing signature"-"non-commercial use"-2.5 mainland China License Agreement in the same way.

 

In Java programs, methods and attributes are the most basic members of a class. The following questions are related to the method:

What is the first step to implement a method?

Speaking of this, I suddenly remembered that two artists, Hou Yaowen and Zhao Lirong, had such a line in the sketch "Mother of Hero:

Director: What is the first thing you do after you get up? You can just say it.
As soon as I heard it, I laughed.
Aunt: Go to the toilet.

Here, I want to make a director and change the line above:

Novice programming: What is the first step in implementing a method in the class? You can just say it.
As a programmer, I laughed.
Programming veteran (coming to the ears of a newbie, quietly): checks the parameter validity.

Aunt's answer is definitely not what the director wants to hear, and the answers from programmers are probably not what the novice wants. The reason is very simple and has nothing to do with the subject. However, the importance of parameter validity check in method implementation is never the same as getting up and going to the toilet in the morning. The reason is only acceptable. Here we will discuss how to check the parameter validity in Java.

Speaking of the parameter validity check, you have to mention the runtime exception. Java has two types of exceptions: general exceptions and runtime exceptions. Exceptions inherited from exception are called general exceptions, and exceptions inherited from runtimeexception are called runtime exceptions. The biggest difference from general exceptions is that,Function callers do not have to handle runtime exceptions thrown by the implementers.However, an exception throw is usually considered an error.If the caller does not need to handle runtime exceptions, it means that the caller must avoid running exceptions thrown by the implementer as much as possible.For example:
Public static Boolean jsonobject (Object obj1, object obj2) {<br/> return obj1.equals (obj2); <br/>}

The above code will throw nullpointerexception when obj1 is null. To avoid this, the correct syntax should be:

Public static Boolean jsonobject (Object obj1, object obj2) {<br/> return (obj1! = NULL & obj1.equals (obj2); <br/>}

It can be seen that an exception during running is an invisible constraint on the function caller. This constraint meets the parameter validity check requirements, that is, the correct parameter must be provided when calling a method, otherwise, the system will crash. For example, we use the mass of an object to calculate its gravity value. The implementation method is as follows:

Public class test {<br/> Public static void main (string [] ARGs) {<br/> system. out. println (getgravity (1.0); <br/>}</P> <p> Public static double getgravity (double mass) {<br/> return mass * 9.82; <br/>}< br/>}

 

We know that the gravity of the next 1.0 kg objects is 9.82 Newton but the mass is negative? Physical knowledge tells us that there are no objects with a mass of 0 or less than 0 in the world. How can we improve the gravity calculation method?

The first step in gravity calculation is to check the parameter validity of quality. The Java SDK provides many runtime exceptions. The exception related to the parameter validity check is illegalargumentexception. The code implementation can be as follows:

Public static double getgravity (double mass) {<br/> // check the parameter validity <br/> If (mass <= 0) {<br/> throw new illegalargumentexception ("invalid quality:" + mass); <br/>}</P> <p> return mass * 9.82; <br/>}

 

When we call such a method to calculate gravity, we should try to pass valid quality values as much as possible. When we pass a negative value to this method, the system will exit by throwing an exception. For example:
Public class test {<br/> Public static void main (string [] ARGs) {</P> <p> system. out. println (getgravity (1.0); // valid quality <br/> // we should try to avoid the following calls <br/> // system. out. println (getgravity (-1.0); // invalid Quality <br/>}</P> <p> Public static double getgravity (double mass) {</P> <p> // parameter validity check <br/> If (mass <= 0) {<br/> throw new illegalargumentexception ("invalid quality: "+ mass); <br/>}</P> <p> return mass * 9.82; <br/>}< br/>}

The parameter validity check should be used as common knowledge, especially the special parameter validity check in Java. The above is the most basic usage, of course we can also use third-party libraries (such as http://commons.apache.org/lang/ "href =" http://commons.apache.org/lang/ "target =" _ blank "> Apache commons Lang library validate class) to simplify the parameter validity check process.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.