Big wisdom with minor issues

Source: Internet
Author: User

When it comes to string comparison, we all know, but do you really know how to compare is the most correct?

Almost all people who do not know are compared using the following methods:

String str = nullif (str == null || str.equals("")) {}

This is actually the lowest efficiency. The correct method is:

String str = null;if (str == null || str.isEmpty()) {}

However, if the above method requires JDK 1.6 or above, you can also use:

String str = null;if (str == null || str.length() == 0) {}

In addition, str. Equals ("1") is different from "1". Equals (STR). If STR is null, a null pointer exception is reported, while the latter does not.

Although the above problems seem simple, they reflect the programming quality and attitude of a programmer. I have repeatedly stressed in my previous articles that a good piece of code doesn't mean that the implementation of functions is complete, it is how to be efficient, stable, and sound while implementing functions. For the same business, you may need to spend 1 s, but others only need 0.3 s to zoom in to the server. In the case of high load, other projects can run normally, you just got down.

No matter how good programmers are, they also grow from ignorance to knowledge, however, the reason why some people cannot grow up after a certain period of time is that they lack the strong perseverance to think about and never give up and the mentality to pursue their ideals.

The software itself represents automation, intelligence, and efficiency. The software is written in code, so the Code itself has these features. Some programmers seem to be hard-pressed to write code day and night, the actual root cause is that the quality of the Code is not high, and the features of the Code itself are lost. It turns into pure physical words, which repeatedly repeat, copy, and paste the code. Transform yourself from an artist to a worker ".

Therefore, to become a master of art, you must start from an early age and keep thinking deeply. You must have a perfect mentality that is not satisfied with the status quo.

Below, I have summarized some examples for your further experience:

1.

for (int i = 0; i < size() * 2; i++) {}
for (int i = 0, stop = size() * 2; i < stop; i++) {}

2.

if (birds.elementAt(i).isGrower()) ...if (birds.elementAt(i).isPullet()) ... 
Bird bird = birds.elementAt(i);if (bird.isGrower()) ...if (bird.isPullet()) ... 

3.

TestBean bean = new TestBean();if (bean instanceof TestBean) {}
TestBean bean = new TestBean();if (bean.getClass().equals(TestBean.class)) {}

4. Replace long if-else-If with switch

String s = "";for (int i = 0; i < n; i++) {s += "#" + i;}
StringBuilder sbuf = new StringBuilder();for (int i = 0; i < n; i++) {sbuf.append("#").append(i);}String s = sbuf.toString();

However

String s = "(" + x + ", " + y + ")";

Will automatically compile and use stringbuilder. append (""), so that the string can be spelled.

5. Get and set methods of object classes, plus final

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.