In java, is String an object or a class? Detailed description of the String and javastring in java

Source: Internet
Author: User

In java, is String an object or a class? Detailed description of the String and javastring in java

Many people do not understandObjectAnd class definition. For example, in javaString to the endIs it an object or a class?

Some people say that String can be both a class and an object.

In fact, there is no problem with this. Classes and objects are actually abstract concepts. We can combine a class of things to create a name, class, and specific things, but we can also say that it is an object ......

Therefore, String can be both a class and an object.

But if you want to go deeperFor example, the small Editor is hungry for a moment, I found that in java. lang, this is a basic class, you can look at the source code in the lang package.

public final class String extends Object implements Serializable,Comparable < String >,CharSequence

In java. lang, the String class represents a String. All string texts in Java programs, such as "abc", are implemented as examples of this class.

Strings remain unchanged; their values cannot be changed after creation. String Buffer supports variable strings. Because String objects are unchangeable, they can be shared. For example:

  String str =“abc”;

Equivalent:

   char data [] = {'a','b','c'};
     String str = new String(data);

Here are some examples of how to use strings:

   System.out.println( “ABC”);
     String cde =“cde”;
     System.out.println(“abc”+ cde);
     String c =“abc”.substring(2,3);
     String d = cde.substring(1,2);

This type of String includes methods used to check the characters of a sequence, used to compare strings, used to search strings, used to extract substrings, and used to create a copy of a String that translates all characters into uppercase or lowercase letters. Case ing is based on the Unicode standard version specified by the Character class.

Java provides special support for string Concatenation Operators (+) and converts other objects to strings. Use the StringBuilder (or StringBuffer) class and its append method to connect strings. String Conversion is implemented by using the toString method defined by all classes in ObjectJava and inherited by them. For more information about string connection and conversion, see Gosling, Joy and Steele, Java language specifications.

Unless otherwise specified, passing the null parameter to the constructor or method in this class will cause NullPointerException to be thrown.

A String represents a String supplemental Character in UTF-16 format that represents a replacement pair (see section Unicode characters for more information in the Character class ). The index value refers to the unit of char code, so the supplemental character uses two positions in String in.

The String class uses Unicode code points (I .e., characters) for processing, except for those used to process Unicode code units (I .e., the value of the method char ).

So what problems will you encounter in the future? You can check the documents and read the source code.You can also check the document to know that it is a class. You can quickly understand what this is.

You can use this method to check whether an object is an interface or a class.

Class: refers to the subjective abstraction and definition of a physical object. The object is the specific instance generated by this class. String s = new String (); here the String is a class, s is the object generated by the String class ..

Constructor Summary

String ()
Initializes a New String object to indicate an empty character sequence.
String (byte [] bytes)
Uses the default Character Set of the platform to decode the specified byte array and construct a new String.
String (byte [] bytes, Charset charset)
Decodes the specified byte array using the specified charset to construct a new String.
String (byte [] ascii, int hibyte)
Expired. This method cannot correctly convert bytes into characters. From JDK 1.1, the preferred method to complete the conversion is to use the String construction method with Charset, Character Set name, or the default Character Set of the platform.
String (byte [] bytes, int offset, int length)
Decodes the specified byte sub-array using the default Character Set of the platform to construct a new String.
String (byte [] bytes, int offset, int length, Charset charset)
Decodes the specified byte sub-array using the specified charset to construct a new String.
String (byte [] ascii, int hibyte, int offset, int count)
Expired. This method cannot correctly convert bytes into characters. From JDK 1.1, the preferred method to complete the conversion is to use the String construction method with Charset, Character Set name, or the default Character Set of the platform.
String (byte [] bytes, int offset, int length, String charsetName)
Decodes the specified byte sub-array using the specified character set to construct a new String.
String (byte [] bytes, String charsetName)
Decodes the specified byte array using the specified charset to construct a new String.
String (char [] value)
Assign a new String to indicate the Character Sequence currently contained in the character array parameter.

To sum up (pz): A class is a subjective abstraction and definition of a physical object. An object is a concrete example of this class. String s = new String ();

Here, String is a class, and s is the object generated by the String class .. In essence, String is a class. If you want to use it, you can instantiate an object.

String is a special class! Different from basic types

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.