Java APIs on Strings are
- String StringBuffer StringBuilder
String
String a = "abc", / / \u200b\u200bcreates "abc" in the pool
String b = new String ("abc") / / creates a String variable heap, and "abc" in the pool
The second method creates a variable in more, better first.
methods you need to know for the certification are:
- charAt () - Returns the character at the given, "hello world". CharAt (2) returns 'a';
- concat () - equivalent to the operator "+"
- equalsIgnoreCase () - equivalent to toUpperCase (). equals (obj.toUpperCase ());
- length () - returns the length of the string
- replace () - Replaces a character in string
- with another substring () - returns a substring starting at the (start) until (end-1)
- toLowerCase () - returns a string with all lowercase characters
- toString () - returns the string ;
- toUpperCase () - returns a string with all uppercase characters
- trim () - returns a string with no spaces neither before nor after (ie remove all '\\ n', '', '\\ r')
StringBuilder
The only difference between StringBuffer and StringBuilder is che il secondo è stato introdotto per risolvere l'eccessiva pesantezza dello StringBuffer in contesti in cui la gestione dei Thread deve essere fatta esternamente; StringBuffer infatti è sincronizzato internamente, mentre StringBuilder non lo è (e quindi è più veloce).
Mostriamo subito un caso in cui la sincronizzazione interna non funziona:
1 - StringBuffer sb = new StringBuffer();
2 - sb.append("ciao");
3 - sb.append(" ");
4 - sb.append("mondo");
se avviamo due Thread e il primo esegue riga 1 e 2, poi la jvm lo blocca e avvia il secondo, che esegue 1,2,3,4, ci ritroviamo con la stringa "ciaociao mondo mondo" , sicuramente differente da ciò che volevamo noi; per questo motivo dobbiamo gestire la sincronizzazione esternamente, e possiamo evitare di usare lo StringBuffer , che è più pesante dello StringBuilder .
Metodi da conoscere per la certificazione:
- append(String s) - accoda la stringa s al nostro buffer e restituisce il buffer
- delete(int start, int end) - cancella una parte del testo (da posizione start a posizione end - 1 )
- insert (int offset, String s) - inserts the string s position offset
- reverse () - reverses the string
- toString () - returns value as a string
The StringBu ** er and are often used by the String concatenation methods, certification examination will find many codes in the form:
String a = "hello world". toUpperCase () + "!!!"
StringBuilder sb = new StringBuilder (). Append ("hello"). Append (""). Append ("world"). Reverse ();
E' quindi importante capire come funziona, cioè che partendo da sinistra verso destra vengono eseguiti tutti i metodi, e viene restituito solo il valore finale.
Altre cosa importante da ricordare è che le stringhe sono immutabili e gli StringBuffer/Builder no; quindi le domande trabocchetto:
String a = "ciao";
System.out.println(a.concat(" mondo"));
System.out.println(a.concat("!!!"));
richiederanno attenzione, perché gli output sono "ciao mondo" e " ciao!!!" ;
0 comments:
Post a Comment