String Flashcards
(46 cards)
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?
true
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
String a= “LMAO”;
boolean aa =true;
System.out.println(a+aa);
LMAOtrue
What method substring() does ?
````
String string = “animals”;
System.out.println(string.substring(3));
System.out.println(string.substring(string.indexOf(‘m’)));
System.out.println(string.substring(3, 4));
System.out.println(string.substring(3, 7));
~~~
`
It returns parts of the string. The first parameter is the index to start with for the returned string. There is an optional second parameter, which is the end index you want to stop at.
String string = “animals”;
System.out.println(string.substring(3)); // mals
System.out.println(string.substring(string.indexOf(‘m’))); // mals
System.out.println(string.substring(3, 4)); // m
System.out.println(string.substring(3, 7)); // mals
What toLowerCase() and toUpperCase() do ?
````
String string = “animals”;
System.out.println(string.toUpperCase());
System.out.println(“Abc123”.toLowerCase());
~~~
`
toUpperCase() converts any lowercase characters to uppercase in the returned string. toLowerCase() converts any uppercase characters to lowercase in the returned string. These methods leave alone any characters other than letters.
String string = “animals”;
System.out.println(string.toUpperCase()); // ANIMALS
System.out.println(“Abc123”.toLowerCase()); // abc123
What equals() and equalsIgnoreCase() do ?
````
System.out.println(“abc”.equals(“ABC”));
System.out.println(“ABC”.equals(“ABC”));
System.out.println(“abc”.equalsIgnoreCase(“ABC”));
~~~
`
The equals() method checks whether two String objects contain exactly the same characters in the same order. The equalsIgnoreCase() method checks whether two String objects contain the same characters with the exception that it will convert the characters’ case if needed.
System.out.println(“abc”.equals(“ABC”)); // false
System.out.println(“ABC”.equals(“ABC”)); // true
System.out.println(“abc”.equalsIgnoreCase(“ABC”)); // true
What startsWith() and endsWith() do ?
````
System.out.println(“abc”.startsWith(“a”));
System.out.println(“abc”.startsWith(“A”));
System.out.println(“abc”.endsWith(“c”));
System.out.println(“abc”.endsWith(“a”));
~~~
`
The startsWith() and endsWith() methods look at whether the provided value matches part of the String.
System.out.println(“abc”.startsWith(“a”)); // true
System.out.println(“abc”.startsWith(“A”)); // false
System.out.println(“abc”.endsWith(“c”)); // true
System.out.println(“abc”.endsWith(“a”)); // false
What method contains() does ?
````
System.out.println(“abc”.contains(“b”));
System.out.println(“abc”.contains(“B”));
~~~
`
The contains() method also looks for matches in the String. It isn’t as particular as
startsWith() and endsWith()—the match can be anywhere in the String.
System.out.println(“abc”.contains(“b”)); // true
System.out.println(“abc”.contains(“B”)); // false