> For the complete documentation index, see [llms.txt](https://r24zeng.gitbook.io/leetcode-notebook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://r24zeng.gitbook.io/leetcode-notebook/useful-java-knowledge/string-vs-string-vs-char-vs-character.md).

# String vs string vs char vs Character

### initialize&#x20;

{% tabs %}
{% tab title="string" %}

```java
string s = "abcd";
```

{% endtab %}

{% tab title="String" %}

```java
String s = "abc";
String s = new String("abc");
s += "abc";  // s is new generated, this is a concat

String a = "abc";
String b = "abc";
a == b; // true, because this way generated string means a constant value, when create b, system search "abc" first, then assign a address to b once find "abc" has been in memory.

String a = "abc";
String b = new String("abc");
a == b; // false, addresses are different

a.equals(b); // compare values instead of addresses
```

{% endtab %}

{% tab title="Untitled" %}

```java
char c = 'a';
```

{% endtab %}
{% endtabs %}

### useful methods

{% tabs %}
{% tab title="string" %}

```java
s.length
```

{% endtab %}

{% tab title="String" %}

```java
s.length()   
s.substring(i, j)   //=>    [i, j), copy to a new string, T = O(n)
s.concat("cde"); // s is new generated
s += "cde";   // s is new generated
String upper = s.toUpperCase();

// wrong
s1 == s2 // if s1 is equal to s2, return 0, not true/false
// right
s1.equals(s2) // return true or false

s1.split(" "); // separate String by " "
s1.toCharArray(); // return a char array
```

{% endtab %}

{% tab title="char" %}

```java
Character.isDigit(c); // return true or false

```

{% endtab %}
{% endtabs %}
