Get the App
SLTechnology News&Howtos  ›  Development  › 

How to define a string in Java

Shulou Source: shulou.com Published: 2022-06-01 12:49:54 09月21日 Update

Most people do not understand the knowledge points of this article "how to define strings in Java", so the editor summarizes the following contents, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to define strings in Java" article.

Method 1: define the string directly

We can directly define the characters we need in learning and development. Let's take a look at the following related code:

String str= "Hello Java"; String str;str= "You are in my heart!"

When we directly define the characters we need, we need to note that string variables must be initialized before they can be used.

Method 2: use the String class definition

1. String: we represent a sequence of empty characters by initializing a newly created String object.

2. String (String original): initialize a new String object to represent a sequence of characters with the same parameters. Let's take a look at the following code:

String str1 = new String ("Hello Java"); String str2 = new String (str1)

We can see in the code that the values of str1 and str2 are equal.

3. String (char [] value): after assigning a new string, we change all the character array elements in the parameters into strings. Let's take a look at the following code:

Char a [] = {'Hutchison]; String sChar = new String (a); a [1] =' s'

Modify the character array by copying the contents of the character array. And in the code, the value of the sChar variable is the string "hello". Even after the character is created, modifications to the second element of the an array do not affect the value of sChar.

4. String (char [] value,int offset,int count): this method, by reassigning the new String, and it contains characters from a subarray of this array parameter, is the index of the first character of the subarray for the offset parameter, and the count parameter is the length of the stator array. Not only that, the contents of the array have been assigned, but subsequent modifications to the character array will not affect the newly created string, let's take a look at the following example, the code is as follows:

Char a [] = {'Homo]; String sChar=new String; a [1] =' s'

In the code, the value of the sChar variable is the string "ello", and the constructor uses some contiguous elements in the character array to create a string object.

The above is about the content of this article on "how to define strings in Java". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Tags: Characters strings arrays content codes parameters methods elements variables objects sequences articles knowledge articles facets assignments influences same more than that size Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Docker Linux Xiaomi Apple