How to Write efficient Android code and Android code

Source: Internet
Author: User

How to Write efficient Android code and Android code

Time is a very valuable thing. Writing as efficient Android code as possible can save you a lot of time, it gives you time to pick up girls, spend time with girlfriends, spend time with friends, eat, entertain, and go to bed! After all, no one wants to work overtime all night. Then, they are still confused: Where is the time ?! The following describes how to improve efficiency and write efficient Android code! I can see this. I have a smile. I can see it!

There are two basic principles for judging how a system is unreasonable:

1. Do not do unnecessary things.

Ii. Try to save memory usage as much as possible.

Below are some common optimization suggestions:

1. Avoid creating objects as much as possible)

Because object creation does not have no cost. If you allocate an object in a user interface loop, You have to forcibly recycle the memory, then, the user experience will experience a slight "snoring" phenomenon.

When extracting a string from the original input data, try to return a substring from the original string instead of creating a copy.

2. Use Native Methods)

When processing strings, do not hesitate to use as many methods as possible, such as String. indexOf () and String. lastIndexOf. These methods are implemented using C/C ++, which is 10-times faster than doing the same thing in a java loop.

3. Use Safe. ijiami to check your code.

After the Android code is complete and the Android App is developed, Safe. ijiami can be used to detect vulnerabilities in the Code. Once the code is completed in one minute, you will not be delayed in the next batch of code. The most important thing is that it will also propose solutions based on your vulnerabilities, saving you the trouble of pulling code one by one!

4. Cache Field Lookups

Accessing object properties is much slower than accessing local variables. You should not write your code like this:

For (int I = 0; I <this. mCount; I ++)

DumpItem (this. mItems [I]);

It should be written as follows:

Int count = this. mCount;

Item [] items = this. mItems;

For (int I = 0; I <count; I ++)

DumpItems (items [I]);

(We directly use "this" to indicate that these are its member variables)

5. Declare Final Constants

Let's take a look at the Declaration at the top of the following class:

Static int intVal = 42;

Static String strVal = "Hello, world !";

When a class is used for the first time, the compiler will call a class initialization method, which saves 42 to the variable intVal and extracts a reference for strVal In the String constant table of the class file, when these values are referenced later, the attribute is called directly.

We can use the keyword "final" to improve the Code:

Static final int intVal = 42;

Static final String strVal = "Hello, world !";

This class will not call the class initialization method, because these constants are directly written into the class file static attribute initialization, which is directly processed by the virtual machine. Code Access to intVal will use 42 of the Integer type, and access to strVal will use a relatively saved "String constant" to replace an attribute call.

Declaring a class or method as "final" does not bring any execution advantages. It can be optimized. For example, if the compiler knows that a Get method cannot be overloaded by the quilt class, it sets this function to Inline.

6. Avoid floating point usage

Embedded processors generally do not support floating point processing. Therefore, all "float" and "double" operations are performed through software, some basic floating point operations take milliseconds.

Likewise, even integers, some chips only have multiplication without division. In these cases, the division and modulo operations of integers are implemented by software.

7. Avoid listing Avoid Enums

The list type is very easy to use. When considering the size and speed, the price is very high. For example:

Public class Foo {

Public enum Shrubbery {GROUND, crawler, HANGING}

}

This will be converted into a 900-Byte class file (Foo $ Shrubbery. class ). During the first use, class initialization requires the exclusive permission to call a method to describe each of the listed items. Each object must have its own static space, it is stored in an array (a static array called "$ VALUE ). It's a lot of code and data, just for three integers.

Shrubbery shrub = Shrubbery. GROUND;

This will cause a call to a static property. If GROUND is a static Final variable, the compiler will regard it as a constant embedded in the code.

 


How to compile Android code in eclipse

Eclipse does not support visual programming, that is, there are no buttons, labels, menus, and other components that can be dragged.
If you understand the java syntax, You need to define the size, position, and other attributes of the buttons in java.
However, if you want to click the button to trigger the event, you must use the listener to implement it,
The listener can listen on the keyboard, mouse, and other operations, and then process the corresponding events.
We recommend that you take a look at the Swing programming in JAVA.

How to compile Android code in eclipse

Eclipse does not support visual programming, that is, there are no buttons, labels, menus, and other components that can be dragged.
If you understand the java syntax, You need to define the size, position, and other attributes of the buttons in java.
However, if you want to click the button to trigger the event, you must use the listener to implement it,
The listener can listen on the keyboard, mouse, and other operations, and then process the corresponding events.
We recommend that you take a look at the Swing programming in JAVA.

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.