JAVA Foundation--java API Common Objects 11

Source: Internet
Author: User

Tag: Offset image demonstrates its bytes target style using up to

One, String type 1. The String class introduces

The next day you learned about constants in Java:

Categories of constants:

Numeric constants: Integers, decimals (floating-point numbers)

Character constants: Data referenced with single quotes

String constants: Data referenced with double quotes

Boolean constant: TRUE or False

NULL constant: null

Most of the constants above can be used to open up space using the 8 basic types provided in Java to store these constants.

String constants do not belong to data in 8 basic types. And it belongs to reference type data (class type data) in Java.

String data is described in Java using the string class.

String data it belongs to a class of data in Java, and we can also think of it as a class of things. Since it is a category, it can be encapsulated and described using a class in Java.

Later we learn the goals and focus:

It is no longer how to describe a class (not to define a class). Instead, learn how to use the classes that are available in others (JDK). You need to know what these classes are, and what common methods are used in it to solve any problems.

When learning new classes later, learn the way:

The point is to use your own code to test the methods in these classes and get your own conclusions.

2.String Class Introduction

           

      Stringclass represents a string. All string literals (such as) in a Java program "abc" are implemented as instances of this class.

In Java, any data that is referenced with double quotes is an object of string.

        

Strings are constants, and their values cannot be changed after they are created. Because the String object is immutable, it can be shared.

Although the data quoted using double quotation marks is a string and an instance object of string, the character data in this object must be finished, it is already a fixed data, can not be modified, and it is stored in the method area of the string constant pool.

      

      

3.String Class Construction method

The string class provides a number of construction methods that can convert different forms of data into string objects.

    

1 /*2 * Demonstrates how to construct a string class3  */4  Public classStringconstructordemo {5      Public Static voidMain (string[] args) {6         7 method5 ();8     }9     /*Ten * Face Test One      */ A      Public Static voidMethod5 () { -          -String s = "abc"; theString s2 =NewString ("ABC"); -         /* - * The difference between S and S2?  - * s It points to the method area in the constant pool in the ABC string + * s2 It points to the object of its own new in the heap, while the address maintained in this object in the heap - * The ABC string in the constant pool pointed to +          *  A * String s = "abc"; only one object in memory at * String s2 = new String ("abc"); There are 2 objects in memory -          */ - System.out.println (s); - System.out.println (S2); -System.out.println (s = =S2); -     } in     /* - * Demo to convert an array of type int into a string to      */ +      Public Static voidmethod4 () { -          the         int[] arr = {23456,23457,23458}; *String s =NewString (arr, 0, 3); $ System.out.println (s);Panax Notoginseng     } -     /* the * String (byte[] bytes, int offset, int length) + * byte[] Bytes: Specifies the byte array that needs to be turned into a string A * int offset: Starts the data transfer from the subscript of the byte array the * int Length: How much data will need to be transferred from offset onwards +      */ -      Public Static voidmethod3 () { $          $         byte[] B = {97,98,99,100,65,49}; -          -String s =NewString (b, 4, 4 ); the System.out.println (s); -     }Wuyi     /* the * Demonstrates a parameterized constructor in the string class -      */ Wu      Public Static voidmethod2 () { -          About         //creating a byte array $         byte[] B = {97,98,99,100,65,49}; -          -         //converts all data in a byte array into a string -String s =NewString (b); A          + System.out.println (s); the     } -     /* $ * How to construct empty parameters the * String () the      */ the      Public Static voidmethod1 () { the          -         /* in * Use the construction method of the string class to get the string object the * A String object created using the construction method of an empty argument, which is in the heap the * And there is no character data in this string object About          */ theString s =NewString (); the          the     } +}
4. Common methods of String Class A. Stitching strings

After guessing a feature, continue to guess its method parameters and return value types

Parameters: There are at least 2 of them. The actual value requires a parameter

Return value type: string, new string after concatenation

      

      

B. Locate the subscript according to the character

      

      

      

      

C. Find characters based on subscript

      

D. Length of the string

      

      

E. Getting a substring in a string

        

     

1 /*2 * Demonstrates the interception method in the string class3  */4  Public classStringMethodDemo2 {5      Public Static voidMain (string[] args) {6         7String s = "Study hard, day up";8         /*9 * SUBSTRING (int beginindex)Ten * Intercept substrings starting at the Beginindex position in the specified string, until the end One * SUBSTRING (int beginindex, int endIndex) A * from the beginindex position in the specified string to the end of Endindex (does not contain characters on the endindex position) -          */ -String ss = s.substring (2); the SYSTEM.OUT.PRINTLN (ss); -String SS2 = s.substring (0, 5); - System.out.println (SS2); -     } +}
F. String-to-array

      

      

      

1 //string into byte array2      Public Static voidmethod2 () {3String s = "ABDEFG";4         //byte array5         byte[] bs =s.getbytes ();6         7          for(inti = 0; i < bs.length; i++) {8 System.out.println (Bs[i]);9         }Ten     } One     //string to character array A      Public Static voidmethod1 () { -String s = "ABCEFG"; -         //Turn character Array the         Char[] cs =S.tochararray (); -          for(inti = 0; i < cs.length; i++) { - System.out.println (Cs[i]); -         } +}

G. Other methods of judgement

    

JAVA Foundation--java API Common Objects 11

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.