Java:string is not a primitive type, is not an array, is a class, and an array is also a class

Source: Internet
Author: User

Environment: JDK 1.7.

This article can be summed up in one sentence: **string is not a basic type, it is not an array, it is a class, and an array is a class. **

Because scripting languages are more used, I've written this code in Java:


String str = "hi";System.out.println(str[1]); // 错误的

Unfortunately, it's wrong. STR is an object of the Java.lang.String class and cannot be used []Operator. The code that is really available is this:

String str = "hi";System.out.println(str.charAt(1));
Arrays are classes


Refer to Java: Inverse array, and the implementation mechanism of System.out.

In fact, it can be seen from the following statement:


int[] ia = new int[9];

You can also see it from the following code snippet:


char[] arr = {‘a‘, ‘b‘, ‘c‘};char[] arr2 = arr; // 引用 System.out.println(arr); // abcSystem.out.println(arr2); // abcarr[1] = ‘6‘;System.out.println(arr); // a6cSystem.out.println(arr2); // a6c

String is not a basic type


Java provides 8 basic types, namely: Byte, short, int, long, float, double, Boolean, and Char, and no string. The base type can define variables and assign values in the following form:

int a = 5;

The above variable A is a basic type variable, not an object, so the variable A does not have any properties and methods, only .

Of course, these 8 basic types also have corresponding classes, respectively, the Java.lang packet of Byte, short, Integer, Long, Float, Double, Boolean, Character. These 8 classes have a private variable that uses the base type value to hold the value.

In Java there is the concept of "auto-boxing", "auto-unpacking", which is for basic types and their corresponding classes.

Auto-Boxing: The basic types are packaged with their corresponding reference types so that they have the attributes of the object and can be called by ToString (), Hashcode (), GetClass (), Equals (), and so on.

Unpacking: In contrast to automatic boxing, objects of reference types such as integers and double are re-simplified to the basic type of data.

Note: Automatic boxing and unpacking is done by the compiler, and the compiler determines whether boxing and unboxing are performed at compile time based on syntax.

code example:

Integer a = 5;          // 自动装箱int b = new Integer(6); // 自动拆箱System.out.println(a);  // 5System.out.println(b);  // 6
String is not an array


Because the array operator cannot be used [] .

string internal implementation


String is a class, specifically java.lang.String, to view its source code, you can see


public final class String implements java.io.Serializable, Comparable<String>, CharSequence { /** The value is used for character storage. */ private final char value[];

The char array value is used to store strings. Note the following properties of the char type:

char类型是一个单一的16位Unicode字符;最小值是’\u0000’(即为0);最大值是’\uffff’(即为65,535);char数据类型可以储存任何字符;例子:char letter = ‘A‘。

The string class does not expose a method that can modify value, so you can assume that a string cannot be modified.

Here is an example:

char[] arr = {‘a‘, ‘b‘, ‘c‘};String str = new String(arr);System.out.println(arr);  // abcSystem.out.println(str);  // abcarr[1] = ‘6‘;System.out.println(arr);  // a6cSystem.out.println(str);  // abc

Where: The new String(arr); following construction methods are used:


public String(char value[]) { this.value = Arrays.copyOf(value, value.length);}

This is equivalent to copying the contents of arr and letting This.value point to its newly generated array. So, in arr[1] = ‘6‘;After the string object strThe value does not change.

So "123"Mean?

Take a look at the following code:


package hellojava; public class HelloJava { void hi(char[] arr) { System.out.println("hi, char[]"); } void hi(String str) { System.out.println("hi, String"); } public static void main(String[] args) { HelloJava hj = new HelloJava(); char[] arr = {‘a‘, ‘b‘}; hj.hi("123"); // hj.hi({‘a‘, ‘b‘}); // 错误 hj.hi(arr); }}

The operating result is:

hi, Stringhi, char[]

So "123" it represents a string.

If there is no method in the code above, the void hi(String str) hj.hi("123"); error will also be.

Add


This construction method is also available under the String class:

String(char[] value, boolean share) {    // assert share : "unshared not supported";    this.value = value;}

If the construction method is decorated with public, the following code is set up:

// 这段代码在实际中是不成立的char[] arr = {‘a‘, ‘b‘, ‘c‘};String str = new String(arr, true);System.out.println(arr);  // abcSystem.out.println(str);  // abcarr[1] = ‘6‘;System.out.println(arr);  // a6cSystem.out.println(str);  //-> 结果会是: a6c

That is, the string can be modified, but the construction method does not use the **public modifier. Only classes that are in the same Java.lang package can be used.

Java:string is not a primitive type, is not an array, is a class, and an array is also a class

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.