Some small ideas of Java

Source: Internet
Author: User
Tags object object

Tag: operator ima failed oid otherwise string UIL ret integer

1. Replace complex If...else (If+return) with If+return

public staticvoidtest1 (String str) {if("1". Equals (str)) {System.out.println ("The input parameter is 1"); return; }        if("2". Equals (str)) {System.out.println ("The input parameter is 2"); return; }        if("3". Equals (str)) {System.out.println ("The input parameter is 3"); return; } System.out.println ("You are not typing 123" "); // ... You can write a deal that doesn't meet the above conditions.}

2. Minimize the repetition of variables (forloop usage )

To define a concept, the invocation of a method, even if there is only one sentence in the method, is also consumed, including creating a stack frame, protecting the site when invoking a method, recovering the site when the method is called, and so on. So for example, the following actions:

 for (int i = 0; i < list.size (); i++) {...}

Suggested substitutions are:

 for (intint length = list.size (); i < length; i++) {...}

In this way, when the list.size () is very large, it reduces a lot of consumption

Add:

Non-null validation with the first method:

 for (int i = 0; list!=null && i < list.size (); i++) {...}

3. Do not constantly create object references within the loop

For example:

 for (int i = 1; I <= count; i++) {    new  Object ();    }

This practice causes memory to count as the object reference exists, and if count is large, it consumes memory and is recommended instead:

NULL ;  for (int i = 0; I <= count; i++) {    new  Object ();}

In this case, only one copy of the object object is referenced in memory, and each time new object () is used, the object reference points to a different object, but only one copy in memory, which saves memory space considerably.

4, try to use HashMap, ArrayList,StringBuilder, unless the thread safety needs, it is not recommended to use Hashtable, Vector, StringBuffer, The latter three result in performance overhead due to the use of synchronization mechanisms

5. String variable and string constant equals when the string constant is written in front

String str = "123"; if (str!=null && "123". Equals (str)) {    ...}

This is done primarily to avoid null pointer exceptions

6. Non-null judgment of any data prior to use ( including object, List)

        list<employeeexam> employeeinexams = exam.getemployeeinexams ();        List<Employeeexam> employeeoutexams = exam.getemployeeoutexams ();         Boolean false ;         int in_size, out_size;         null ? Employeeinexams.size (): 0;         null ? Employeeoutexams.size (): 0;         = In_size + out_size;

7.if...else ... Statement with conditional operator

return true false;
result = Examservice.deleteexambyid (examid)? "Delete succeeded!": "Delete failed!";

Some small ideas of Java

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.