String types in Java

Source: Internet
Author: User

1. Basic types and reference types

In C, there is a pointer to a variable type, and the pointer variable holds the address that you want to point to the content. In Java, there is no such argument for pointers, instead of a word: a reference type variable.

First of all, the basic type of Java, the so-called basic type, very simple, is a number, a character or a Boolean value, the number contains integral type, floating point type, etc., there is nothing to say.

Besides this reference type, his concept is much like a pointer. The reference variable's value points to a reference to the memory space, which is the address in C, where the content is the object to be referenced by him.

2. String is a reference type

Most of the time, when we use string, that's how it's used:

String str = "Balabala";

Direct assignment, easy to understand.

In fact, there are three ways to use string in the following way, using the string construction method inside the API:

String str = new string (char[] array);

The array is a character array, which is very image, in the array characters are separated from the characters, need array[i] to get.

After this one construction method of string, the character is given to string, and then it becomes the strings.

Another way of structuring:

String str = new string (char[] Array,int offset,int count);

Array is also the string of characters, offset is a position in the exponential group, the number of count values, the synthesis means: the array array, starting from the first offset character, the count of the number of "sub-array", the strings are strung.

Example: char[] array = {' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G '}; String str = new string (array,2,3);

At this point str is the CDE, and the array starts at 0.

The last one, the simplest:

String str = new String ("ABCDEFG");

3. The difference between the basic type and the "equal sign" in the reference type

int a = 1; int b = 1; System.out.println (A = = B);

The output is true, because in the base type, = = is the judgment of the value.

String str1 = "abc"; String str2 = "abc"; System.out.println (str1 = = str2);

The output is true, because the reference type, = = is also the judgment of the value?

No, that's not true, although on the surface it seems like this, but it's not, look at the following example:

String str1 = "abc"; String str2 = new String ("abc"); System.out.println (str1 = = str2);

Then the output is false, but str1, str2 content is the same, this is why?

This is because the reference type of "= =" is the evaluation of the address value, judging whether the referenced content is not an address, is not a place.

So, conversely, if the reference type's "= =" is False, then the reference address of the two reference types is different, which leads to the following content.

Why is the content of str1,str2 the same, but different?

4. Direct assignment creation differs from new string creation

In 2, we talked about four ways to use string, 1 direct assignments, and 3 new created.

(1) String str1 = "abc";

The direct assignment above is actually divided into three steps:

The first step is to create the string content of "abc" in the "heap".

The second step is to save the address of the created "ABC" in a Thing called a "string pool".

In the third step, give the address of "ABC" to STR1 (that is, the reference type of str points to the memory address where the string of ABC is located)

At this time, if another string str2 = "abc";

Java will first go to string pool addressing to find out if there is such a string also called "abc", if not, see the three steps above. Obviously, we've just created it, we can definitely find it, and we'll assign the found address to str2.

(2) String str1 = new String ("abc");

When using new, no matter what the content is, do not perform the above three steps, directly in the heap to create a new version of the string, the address of the string ABC (assuming 0x001) to str1.

One more time: string str2 = new String ("abc"), and the address of the string ABC (assuming 0x002) is given to str2.

(3) Therefore, the directly assigned string, if the value is the same, then the referenced address is the same.

And, new out, regardless of the value of the content of the different, the address will necessarily differ, this is why 3 inside the STR1==STR2 is the reason for false, to a picture:

  

String types in Java

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.