Java Study Notes (1)

Source: Internet
Author: User

[Before] I have graduated for nearly six months, and the trial period is almost over. I look forward to the coming days. In financial institutions, the most painful thing is that most systems are outsourced, so less code is written. In the past six months, only one month of code is written, then I am doing some complicated things every day, hoping to have the opportunity to write code and improve the technology. Some time ago I did some development, and recently I am about to celebrate the new year. This is a relatively long time. So I went back and looked at the book, consolidated my basic knowledge, and made some notes, look at your own thinking, the main purpose is to accumulate for yourself, so it is a very basic, very basic knowledge, please choose on your own. If you see that this is coming, I wish you a happy New Year. [Knowledge Summary] definition of the String class: String is a special class in java, because it can define variables directly for a data type, as follows: string str = "hello"; String str = new String ("hello"); two different definitions of String: String str = "hello "; // you can think of str as a char *. str points to the string "hello" in the data segment ". String str = new String ("hello"); // It is a new object. The heap space is allocated with a piece of memory and the object is put in it. The difference between the two is that str points to this object: string str 1 = "hello"; String str 2 = "hello"; // when str2 is defined, the system first checks whether "hello" exists in the data segment ", if so, str2 points directly to this "hello", which is system optimization. That is to say, "hello" will not be stored separately in the data segment. str1 and str2 point to the same data segment, that is, the Data Segment addresses represented by str1 and str2 are the same. // Correct the error. The first floor below indicates the error. For more information, see the first floor. String str 3 = new String ("hello"); String str 4 = new String ("hello"); // str4 is a new object, it is stored separately in the heap space. That is to say, the addresses of str3 and str4 are different, but the storage content is the same. You can run the following code: Code: copy the code String s1 = "hello"; String s2 = "hello"; System. out. println (s1 = s2); String s3 = new String ("hello"); String s4 = new String ("hello"); System. out. println (s3 = s4); System. out. println (s3.equals (s4); copy the code running result: truefalsetrue: String str1 = "hello"; str1 = str1.substring (0, 3) + "p! "; // First, substring (0, 3) indicates extracting the first to third letters of the string. // The immutable meaning that "hello" is always stored in the place where "hello" is stored. It will never change unless the system recovers automatically. For str1 to extract substrings, just let str1 point to the reference of "hello" again, and then change the reference, while the original storage of "hello" remains unchanged. You can run the following code: String str1 = "hello"; String str2 = str1; str1 = str1.substring (0, 3) + "p! "; System. out. println (str1); System. out. println (str2); running result: help! Hello // because the 2nd line of code directs str1 and str2 to the same address segment, and then changes the direction of str1, the things that str2 points to remain unchanged. [Java code] copy the public class Text {public static void main (String [] args) {// specifies the same address segment pointed to by s1 and s2, s3 and s4 point to different address segments, and the stored content is the same String s1 = "hello"; String s2 = "hello"; System. out. println (s1 = s2); String s3 = new String ("hello"); String s4 = new String ("hello"); System. out. println (s3 = s4); System. out. println (s3.equals (s4); // specifies that str1 and str2 point to the same address segment, the str1 changed later is only a reference to "hello". String str1 = "hello"; String str2 = str1; Str1 = str1.substring (0, 3) + "p! "; System. out. println (str1); System. out. println (str2) ;}} copy the Code [run result] truefalsetruehelp! Hello [next]

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.