Java. lang Package for API tutorial

Source: Internet
Author: User

API tutorial. java. lang Package
1. Original encapsulation class
Char char
Int integer

Integer num = new INTEGER (5); int num2 = num. intvalue; // 1 integer num = integer. valueof (2); // used to convert the original value to its corresponding encapsulation class; whether charater isletter () is the letter iswhitespace () is a space or line break

2 stringbuffer class (by default, the space reserved by the stringbuffer class is 16 characters). When + is used, the stringbuffer is automatically generated.
Indicates the string that can be modified.

Public class stringbuf {protected stringbuf () {} public static void main (string [] ARGs) {stringbuffer Buf = new stringbuffer ("Java"); Buf. append ("guide verl/"); // Add a Buf. append (3); // Add int Index = 5; Buf. insert (index, "student"); // The characters after the index is inserted move to the back of Index = 23; Buf. setcharat (index ,'. '); // change the character int start = 24; int end = 25; Buf. replace (START, end, "4"); // Replace the string S = Buf. tostring (); // the conversion between stirngbuf and string is system. out. println (s );}}

Result: Java student guide verl.4

Example 2

public class Test {    public static void stringReplace (String text) {      text = text.replace('j' , 'i');     }       public static void bufferReplace (StringBuffer text) {      text = text.append("C");     }        public static void main (String args[]) {        String textString = new String ("java");        StringBuffer textBuffer = new StringBuffer ("java");            stringReplace (textString);        bufferReplace (textBuffer);            System.out.println (textString + textBuffer);      }    }

The answer is javajavac.
This is the string parameter passed and is immutable ).

In the seventh line of the question, text = text. append ("C"), the append method will change the value in text.
The text and textbuffer in main point to the same object, so the corresponding output is javac.
The value of string will never change!
String A = "A"; // assume that a points to the address 0x0001,
A = "B"; // After the negative value is re-displayed, A points to the address 0x0002, but the "A" stored in the address 0x0001 still exists,

But it is no longer what a points.
On the surface, the string type object changes the value, but the fact is that it cannot change the value, but can only change the point

The stringbuffer is different. It directly changes the reserved value in the address.

Example 3

Stringbuffer S1 = new stringbuffer ("A"); stringbuffer S2 = new stringbuffer ("A"); If (s1.equals (S2) // Why is false {system. out. print ("yes");} string S11 = new string ("A"); string S21 = new string ("A"); If (s11.equals (S21 )) // Why is it true {system. out. print ("yes ");}

The equals method is not redefined in the stringbuffer class, so this method comes from the object class,

The equals method in the object class is used to compare the addresses, so it is equal to false.
The equals method is redefined in the string class, and the value is compared, not the address. Therefore

Is true.

3 class
Instance. getclass () is the object obtained by the instance.
Getname () Get the Object Name
Instance. getsuperclass () is obtained by the instance.

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.