Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the rules of java expression

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "what are the rules of java expressions?". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Notice the return value of the expression

When using the JDK library, we must carefully read the meaning of the method in JDK and its return value.

Some return values may indicate whether the operation was successful or not, while others may be the result of a method operation. Let's look at two common examples:

Public void deleteFileWrong () {File file= new File ("/ tmp/www.flydean.com.txt"); file.delete (); System.out.println ("File delete success!");} public void deleteFileRight () {File file= new File ("/ tmp/www.flydean.com.txt"); if (file.delete ()) {System.out.println ("File delete success!");}}

First, take a look at an example of file deletion. The delete method has a return value, so after calling the delete method, we must judge the return value to see if the deletion is successful.

Take another look at a common example of character substitution in String:

Public void stringReplaceWrong () {String url= "www.flydean.com"; url.replace ("www", "WWW"); System.out.println ("replaced url..." + url);} public void stringReplaceRight () {String url= "www.flydean.com"; url=url.replace ("www", "WWW"); System.out.println ("replaced url..." + url);}

We have to remember that String is immutable, so its replace method returns a replaced String, but the original String is the same, so we need to reassign the return value.

Pay attention to avoid NullPointerException

NullPointerException should be the most common runtime exception. How to avoid this anomaly?

What we need to do is to determine whether the object is empty or not when calling the object method.

After JDK8, we introduced the Stream operation:

Public void streamWrong (Collection collection) {collection.stream () .filter (obj- > obj.equals ("www.flydean.com");}

During the Stream operation, we need to pay attention to whether the element in the stream is empty.

Sometimes, we may think that the judgment is empty, but the condition judgment is not accurate, resulting in unknown exceptions. Take a look at the following example:

Public int countWrong (Collection collection, Object object) {int count=0; if (collection = = null) {return count;} for (Object element: collection) {if ((element = = null & & object== null) | | element.equals (object)) {count++;}} return count;}

This example is used to find out how many elements in collection are the same as object, and if both are empty, they are also marked as the same.

But the above example has a loophole that does not take into account element = = null and object! = null, which leads to the generation of NullPointerException.

We need to modify it like this:

Public int countRight (Collection collection, Object object) {int count=0; if (collection = = null) {return count;} for (Object element: collection) {if ((element = = null & & object== null) | | (element! = null & & element.equals (object) {count++;} return count } judgment of array equality

If we need to compare whether two arrays are equal, what we want to compare is whether the elements in the two arrays are equal.

We know that an array is a special Object, so array objects also have an equals method. Consider the following example:

Public boolean compareWrong () {int [] array1 = new int [10]; int [] array2 = new int [10]; return array1.equals (array2);}

The returned result is false, because the array directly uses the equals method defined in Object, so let's take a look at the definition of this method:

Public boolean equals (Object obj) {return (this = = obj);}

As you can see, this method compares whether the two addresses are equal. So we got the false result.

In fact, we can use the methods in the Arrays.equals utility class to compare the two arrays:

Public boolean compareRight () {int [] array1 = new int [10]; int [] array2 = new int [10]; return Arrays.equals (array1, array2);} comparison between wrapper classes of basic types

In java, we know that there are some basic types such as boolean, byte,char, short, int, and they will have corresponding encapsulation types: Boolean,Byte,Character,Short,Integer, etc.

We can directly assign the value of the underlying type to the encapsulation type, and the encapsulation type will convert itself.

Consider the following example:

Boolean boolA=true; Boolean boolB=true; System.out.println (boolA==boolB)

What is the result?

The answer is true. Why is the comparison of two different objects true?

Before answering this question, let's take a look at the comparison of strings:

String stringA= "www.flydean.com"; String stringB= "www.flydean.com"; System.out.println (stringA==stringB)

We should all know this, because String has a pool of string constants, and String objects built directly from string constants are actually the same object.

Similarly, for Boolean and Byte, it is the same object if built directly from the underlying class values.

For Character, if the value range is in\ u0000 to\ u007f, it belongs to the same object, and if beyond this range, it is a different object.

For Integer and Short, if the value is in the range of-128 and 127, it belongs to the same object, and if it is beyond this range, it is a different object.

Consider the following example:

Boolean boolA=true;Boolean boolC=new Boolean (true); System.out.println (boolA==boolC)

The output is false, because boolC uses the new keyword to build a new object.

Types in the collection do not match

Java collections can now store only specific types of objects by specifying types. Consider the following example:

This is the end of public void typeMismatch () {HashSet shortSet= new HashSet (); for (int item0);} "what are the rules of java expressions?" Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report