How to more efficiently use strings

xiaoxiao2021-03-06  63

After reading the article (please refer to the comparison of the objects explained by the author of the Java Weekly, September 12,

Since the string is told, the author will discuss with you, how to use the string efficiently.

You may have a question, the string is used, what is the efficient use string? we

Let's first determine one thing, the Java API indicates such a word: "Strings Are

Constant; Their Values ​​Cannot Be Changed After.

String Buffers Support Mutable strings. Because String Objects

Are Immutable The can be shad. ". Means the string is a constant, is it

Changed, that is, you do any modifications to a string object, like another string,

Write letters, etc., will create a new string object. We are a small test,

To prove this.

String str1 = new string ("hello");

String str2 = STR2;

Boolean B1 = STR1 == STR2;

Remember comparing between strings explained by the note by the pen? The result obtained by B1 is True, represents STR1

And STR2 reference to the same string object, forget or unclear readers, then go back to the weekly report

Content. If we now add Str2, "Java" strings:

STR2 = STR2 "Java";

Boolean B2 = STR1 == STR2;

You will find that the result of B2 is False, representing the string object referred to in STR1 and STR2 is not the same.

One! Now there are three string objects in the system, one is "Hello", the second is "

Java, the third is "Hellojava". Because in Java, the string has such an unparalleled

Change characteristics, so it will trigger how to effectively use the string.

Strings is the objects that can be used each application, often some of the places to do a lot of strings

Operation, especially the two strings, for example, we wrote a web page data from the Internet

The program, running a round ring, read a row with the bufferedreader object, then put each

The row is added together. Like this kind of intuition, very simple program, two lines are getting it, but JVM

What you do is. Suppose we write a one-course, to put a string of "a"

One hundred times, I really feel that we will write this:

String str = "";

For (int i = 0; i <100; i )

Str = "a";

But do you know how much garbage will be generated in the memory? There will be A, AA, AAA,

AAA ...., undoubtedly, although the above formula is simple, it is wasting a lot of memory, but also

The access to the bonists and objects will also spend a lot of time, we plus several lines of code to test

Time and memory of flowers:

Long StartTime = system.currenttimemillis ();

Long StartMem = runtime.getRuntime (). FreeMemory ();

For (int i = 0; i <1000; i )

Str = "a";

Long endmem = runtime.getRuntime (). FreeMemory (); system.out.println ("use memory:" (startMem - endmem);

Long endtime = system.currenttimemillis ();

System.out.println ("Use Time:" (endTime - starttime);

The above program runs on the author's PIII-800, with an average of 50 ms and 151256 bytes

Memory, time is ok, but the memory usage is very large. Then what we want to improve

How is more efficient? Because String objects are unmodified, it is often necessary in the program.

Such actions, so Java provides another category, specifically to process string operations

This category is called StringBuffer. StringBuffer category provides a lot of ways

Make strings, but such as additional, delete, insert, reverse, replacement, etc. We use

The Additional (Append) method provided by the StringBuffer category is also the same as the above example.

the result of:

StringBuffer SB = new stringbuffer ();

Long StartTime = system.currenttimemillis ();

Long StartMem = runtime.getRuntime (). FreeMemory ();

For (int i = 0; i <1000; i )

SB.Append ("a");

Long endmem = runtime.getRuntime (). FreeMemory ();

System.out.println ("use memory:" (startMem - endmem);

Long endtime = system.currenttimemillis ();

The modified program runs on the computer, with an average of 0 ms and 4854 Bytes memory.

0 ms? ! I will run a few times to get to 10 ms. I have to see how your computer CPU is currently loaded.

It is already fast to compare with the previous example, and the memory is also very useful.

. This is just a small example. If the strings are bigger, the differences between the two will be more obvious.

So the conclusion is that if you make a lot of modifications to the string in your program, please

StringBuffer category, it will show the efficiency of improving your program. As for StringBuffer

The method provided by the category details, please refer to the Java API.

转载请注明原文地址:https://www.9cbs.com/read-86325.html

New Post(0)