Java Fundamentals (6) Object-oriented programming (2)

Source: Internet
Author: User

Declaration and manipulation of 1.String objects

Strings are everywhere, constructed using string, and store strings,

Cases:

Common: String str = "ABC";

Not used: string str = new String ("abc");

Ps:

String s= "ABC";

String S1 = "abc";

s==s1=true; (= = number to determine whether the address is consistent, equals judgment content is consistent);

S.equals (S1) =true;

Creating a string with the New keyword creates 2 addresses in the string pool, with different addresses,

String S=new string ("abc");

String s1= new String ("abc");

S==s1=false; (= = number to determine whether the address is consistent, equals judgment content is consistent);

S.equals (S1) =true;

The string object is immutable,

Each method in the class that appears to modify a string value is the creation of a new string object (containing the modified string content).

The read-only nature of string leads to efficiency optimizations possible

String literals are stored in a string pool, and string objects take precedence over the string pool to avoid repeatedly generating duplicate string instances.

The system has a high efficiency in non-modification of string, far more than the other two string classes StringBuffer and StringBuilder

Common methods for 2.String objects

--length ()--Returns the length of this string

--charat (int index)--Returns the char value at the specified index.

--concat (String str)--connects the specified string to the end of this string

--contains (charsequence s)--whether to include the specified string sequence

--equals (Object AnObject)--compares this string to a specified object

--compareto (String anotherstring)--Comparison of two strings in dictionary order

--indextof (int ch)--Returns the index of the first occurrence of the specified character in this string

--split (string regex)-splits this string based on matching a given regular expression

--substring (int beginindex)--intercept string

--tolower/uppercase ()--converts the specified string to uppercase and lowercase

Wait a minute...

3. Object-oriented benefits

3.1 Easy to program simulation of real-world entities

Modeling the state and behavior of entity objects with class encapsulation

3.2 Hidden Details

The behavior and state of an object are encapsulated in a class, and the outside world uses the common interface--to invoke the method of the class to get state information, without paying attention to how the internal details are implemented.

3.3 can be reused

With a class template, you can create multiple object cases and reuse the class's definition code

Memory management mechanism for 4.java objects

Use new to create objects, allocate object space in heap memory, initialize

Defining local variables in the method stack, holding references to objects in heap memory

Method execution returns, stack memory is automatically freed, local variable destroyed

If the heap in-memory object does not have a variable referencing it, it becomes garbage, reclaimed by the garbage collector, freeing up the heap of memory!

Java Fundamentals (6) Object-oriented programming (2)

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.