Javase Getting Started learning 27:java Common class string Class (top)

Source: Internet
Author: User

A String class

Strings are widely used in Java programming, where strings belong to objects, and Java provides a string class to create and manipulate strings. In the program Open

Strings are everywhere in the hair, such as the user name, password, etc. that are used when the user logs in. In fact, in the previous chapters we have made

Use a string, such as "Hello World", which we output in the console.

In Java, strings are treated as objects of type string. The String class is located in the Java.lang package. By default, the package is imported automatically

All the programs.

The Java.lang.String class represents an immutable sequence of characters, and "XXXX" is an object of that class.

Two Creating a string

The common construction methods of the String class are:

1) string (string original): Creates a copy of the string object as original;

2) string (char[] value): Creates a string object with a character array;

3) string (char[] Value,int offset,int count): Creates a string on the count character sequence starting with the offset entry with a single character array

Like

The simple way to create a string is as follows:

<span style= "FONT-SIZE:18PX;" >string greeting = "Hello world!"; </span>

When you encounter a string constant in your code, the value here is "Hello world!", which the compiler uses to create a string object. Create a character

String Object greeting= "Hello world!"; , the Java runtime (the running JVM) will hold this "IMOOC" Hello world!; In the string pool, find out whether to save

A string "Hello world!" is created in the pool if it does not exist in the same string object as the content, otherwise it is not added in the pool.

As with other objects, you can use keywords and construction methods to create a string object. There are 11 ways to construct the string class, which provide different

parameter to initialize the string and create the object using the New keyword, a new object will be created (in the heap or stack area). This is the direct

Creates a string object. For example, provide a character array parameter:

<span style= "FONT-SIZE:18PX;" >public class Test{public static void Main (string[] args) {//Create character array char[] Helloarray = {' h ', ' e ', ' l ', ' l ', ' o ', '. '};/ /Use construction method to create string                hellostring = new string (helloarray);                  System.out.println (hellostring);}} </span>

The results of the above example compilation run as follows:


Note: The string class is immutable, so once you create a string object, its value cannot be changed. After a String object is created, it cannot be

is modified, is immutable, so-called modification is actually creating a new object, pointing to a different memory space. If you need to make a lot of changes to the string,

Then you should choose to use the StringBuffer class or the StringBuilder class.

three string lengths

The method used to get information about an object is called an accessor method. An accessor method of the string class is the length () method, which returns a String object

Contains the number of characters.

After the following code executes, the Len variable equals 17:

<span style= "FONT-SIZE:18PX;" >public class Test{public static void Main (string[] args) {String palindrome = "Dot saw I was Tod";                int len = Palindrome.length ();                System.out.println ("String Length is:" + len);}} </span>

The results of the above example compilation run as follows:


Four connection string

The string class provides a way to concatenate two strings:

(1) String1.concat (string2);

Returns a new string for the string2 connection string1. You can also use the concat () method on string constants, such as:

<span style= "FONT-SIZE:18PX;" > "My name is". Concat ("Zara");</span>

The results are as follows:

<span style= "FONT-SIZE:18PX;" >my name is zara</span>

(2) It is more commonly used to concatenate strings using the ' + ' operator, such as:

<span style= "FONT-SIZE:18PX;" > "Hello," + "world" + "!" </span>

The results are as follows:

<span style= "FONT-SIZE:18PX;" > "Hello, world!" </span>

Here is an example:

<span style= "FONT-SIZE:18PX;" >public class Test{public static void Main (string[] args) {String string1 = "Saw I was";                     System.out.println ("Dot" + string1 + "Tod");}} </span>
The results of the above example compilation run as follows:

<span style= "FONT-SIZE:18PX;" >dot saw I was tod</span>

Five Create a formatted string

We know that output formatted numbers can use the printf () and format () methods. The string class uses the static method format () to return a string object without

is a PrintStream object. The static method of the String class, format (), can be used to create a reusable format string, not just for one print output at a time.

As shown below:

<span style= "FONT-SIZE:18PX;"  >system.out.printf ("The value of the float variable is" +                  "%f and while the value of the" +                  "variable are %d, and the string "+                  " is%s ", Floatvar, Intvar, Stringvar);</span>

You can also write this:

<span style= "FONT-SIZE:18PX;" >string Fs;fs = String.Format ("The value of the float variable is" +                   "%f, while the value of the" +                   " Variable is%d, and the string "+                   " is%s ", Floatvar, Intvar, stringvar); SYSTEM.OUT.PRINTLN (FS);</span>

Six string method

The following are the methods supported by the string class, in more detail, see the Java API documentation:







Javase Getting Started learning 27:java Common class string Class (top)

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.