String vs string vs char vs Character
initialize
string s = "abcd";
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
char c = 'a';
useful methods
s.length
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
Character.isDigit(c); // return true or false
Last updated
Was this helpful?