JAVA 28th (Common Object API)-String class

Source: Internet
Author: User

JAVA 28th (Common Object API)-String class

Multithreading has come to an end. You need to back up common object APIs !!!

Developed in the future, the most common object is text, or string.

String type

A string is a special object.

The string cannot be changed once it is initialized.


I. Features

Public class Main {public static void main (String [] args) {Demo1 (); System. out. println ("--------------"); Demo2 ();}/* demonstrate the first definition method of a string and specify the characteristics of the String constant pool */private static void Demo1 () {String s1 = "asd"; s1 = "sd"; // s1 is only a String reference, and the String "asd" itself remains unchanged String s2 = new String ("sdf "); system. out. println (s1); s2 = "sd"; System. out. println (s2 = s1); // the address that s1 and s2 actually point to. // sd is stored in the buffer zone (String constant pool) // if not, it is created, do not create}/* demonstrate the second definition of the String, */public static void Demo2 () {String s = "abc "; // create an object String s1 = new String ("abc") in the String constant pool; // the object is not created in the String constant pool, create two objects in heap memory // One is s1 and the other is abc System. out. println (s = s1); // false. The compare address value is different from that of System. out. println (s. equals (s1 )); // true/* The equals in the String class rewrites the equals in the Object and creates the String class * self-determining whether the String Object is the same * that is, comparing the String content */System. out. println ("s =" + s); System. out. println ("s1 =" + s1 );}}

Ii. Constructor

Public class Main {public static void main (String [] args) {StringconstructorDemo1 (); charDemo () ;}public static void StringconstructorDemo1 () {String s = new String (); // equivalent to String s = ""; the address value is different. It is not equivalent to String s = nullbyte [] sub = {97,66}; String s1 = new String (sub); System. out. println ("s1 =" + s1);} public static void charDemo () {char [] ch = {'A', 'B', 'C', 'D ', 'E', 'F'}; String string = new String (ch); // convert the array into a String, which can only be byte and charSystem. out. println ("string =" + string); String string1 = new String (ch,); // The System is truncated from the 0-angle mark. out. println ("string1 =" + string1); // The remaining methods of the String class, check the manual}

Not complete ....



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.