String Flashcards
What are the three rules for concatenation?
- If both operands are numeric, +1 means numeric addition
- If either operand is a String, +1 means concatenation,
- The expression is evaluated left to right
What will the following print:
````
System.out.println(1+2);
System.out.pirntln(“a” + “b”);
System.out.println(“a”+”b” +3);
System.out.println(1+2+”c”);
````
3
ab
ab3
3c
What does the following output?
````
String s = “1”;
s += “2”;
s += 3;
System.out.println(s);
````
123
What’s the output?
int a =9;
String c = “hello”;
String b = c + a + 1;
System.out.println(b);
hello91
What’s the output?
int a =9;
String b = a + 1;
System.out.println(b);
will not compile
What’s the output?
int a =9;
String b = “”+ a + 1;
System.out.println(b);
91
If an String object is created, can it be allowed to change?
No.
Strings are immutable which means they cannot be changed.
Are String mutable or immutable?
Cannot be changed, Hence IMMUTABLE
What is the output of the following?
````
String s1 = “1”;
String s2 = s1.concat(“2”);
s2.concat(“3”);
System.out.println(s2);
~~~
12
Can you do this? What’s the difference?
````
String name = “Fluffy”;
String name = new String(“Fluffy”);
````
The string pool is like a cache. If you do this:
String s = “Fluffy”;
String p = “Fluffy”;
then the Java compiler is smart enough to make just one String object, and s and p will both be referring to that same String object. If you do this:
String s = new String(“Fluffy”);
then there will be one String object in the pool, the one that represents the literal “Fluffy”, and there will be a separate String object, not in the pool, that contains a copy of the content of the pooled object. Since String is immutable in Java, you’re not gaining anything by doing this; calling new String(“literal”) never makes sense in Java and is unnecessarily inefficient.
What method length() does ?
````
String string = “0123456789”;
System.out.println(string.length());
````
The method length() returns the number of characters in the String.
````
System.out.println(string.length()); // 10
````
String a=”hello”;
String b=”hello”;
if(a==b) returns what?
false.
only a.equals returns true.
What method chartAt() does ?
````
String string = “animals”;
System.out.println(string.charAt(0));
````
The method charAt() lets you query the string to find out what character is at a specific index. The method signature is as follows: char charAt(int index)
````
a
````
StringBuilder a = “aa”;
Must know. DNC
What method indexOf() does ?
````
String string = “animals”;
System.out.println(string.indexOf(‘a’));
System.out.println(string.indexOf(“al”));
System.out.println(string.indexOf(“al”, 5));
````
The method indexOf()looks at the characters in the string and finds the first index that matches the desired value. indexOf can work with an individual character or a whole String as input. It can also start from a requested position. indexOf() returns –1 when no match is found.
````
String string = “animals”;
System.out.println(string.indexOf(‘a’)); // 0
System.out.println(string.indexOf(“al”)); // 4
System.out.println(string.indexOf(“al”, 5)); // -1
````
````
String four = “LMAO”;
System.out.println(four.substring(1));
System.out.println(four.substring(0));
System.out.println(four.substring(3));
System.out.println(four.substring(4)); //nothing
System.out.println(four.substring(-1)); //outofbounds
System.out.println(four.substring(5)); //outofbounds
System.out.println(four.substring(0, 1));
System.out.println(four.substring(0, 2));
System.out.println(four.substring(0, 3));
System.out.println(four.substring(0, 4));
System.out.println(four.substring(0, 5)); //out of bounds
System.out.println(four.substring(0, 0)); //empty
````
String four = “LMAO”;
System.out.println(four.substring(1)); //MAO
System.out.println(four.substring(0)); //LMAO
System.out.println(four.substring(3)); //O
System.out.println(four.substring(4)); //nothing
//System.out.println(four.substring(-1)); //outofbounds
//System.out.println(four.substring(5)); //outofbounds
System.out.println(four.substring(0, 1)); //L System.out.println(four.substring(0, 2)); //LM System.out.println(four.substring(0, 3)); //LMA System.out.println(four.substring(0, 4)); //LMAO //System.out.println(four.substring(0, 5)); //out of bounds System.out.println(four.substring(0, 0)); //empty
String a= “LMAO”;
String aa =null;
System.out.println(a+aa);
LMAOnull
String a= “LMAO”;
String aa =null;
System.out.println(a+aa.toUpperCase());
nullpointerexception