Working with Strings, Arrays, and ArrayLists Flashcards
How each character of a String is represented in Java?
In Java, each character in a string is a 16-bit Unicode character. Because Unicode characters are 16 bits (not the skimpy 7 or 8 bits that ASCII provides), a rich, international set of characters is easily represented in Unicode.
Are strings object or primitive?
Strings are objects and like every other object you can create instance of a string using new keyword.
What are the constructors that can be used to create a string object?
String s = new String("abcdef"); String s = "abcdef"; What they have in common is that they all create a new String object, with a value of "abcdef", and assign it to a reference variable s
What happens when we concat a String?
String s = “abcdef”;
s = s.concat(“ more stuff”);
The Java Virtual Machine (JVM) took the value of string s (which was “abcdef”) and tacked “ more stuff” onto the end, giving us the value “abcdef more stuff”. Since strings are immutable, the JVM couldn’t stuff this new value into the old String referenced by s, so it created a new String object, gave it the value “abcdef more stuff”, and made s refer to it. At this point in our example, we
have two String objects: the first one we created, with the value “abcdef”, and the second one with the value “abcdef more stuff”. Technically there are now three String objects, because the literal argument to concat, “ more stuff”, is itself a new String object. But we have references only to “abcdef” (referenced by s2) and “abcdef more stuff” (referenced by s).
What is the output? For extra credit, how many String objects and how many reference variables were created prior to the println statement?
String s1 = "spring "; String s2 = s1 + "summer "; s1.concat("fall "); s2.concat(s1); s1 += "winter "; System.out.println(s1 + " " + s2);
Answer: The result of this code fragment is spring winter spring summer. There are two reference variables: s1 and s2. A total of eight String objects were created as follows: “spring “, “summer “ (lost), “spring summer “, “fall “(lost), “spring fall “ (lost), “spring summer spring “ (lost), “winter “(lost), “spring winter “ (at this point “spring “ is lost). Only two of the eight String objects are not lost in this process.
How JVM uses String Constant Pool?
To make Java more memory efficient, the JVM sets aside a special area of memory called the String constant pool. When the compiler encounters a String literal, it checks the pool to see if an identical String already exists. If a match is found, the reference to the new literal is directed to the existing String, and no new String literal object is created. (The existing String simply has an additional reference.)
Is it possible to override String methods and for example make it mutable?
String class is marked final. Nobody can override the behaviours of any of the String methods.
What is the difference between these two string construction? String s = "abc"; String s = new String("abc");
First one creates one String object and one. abc goes to pool and s references to it.
In second case, because we used the new keyword, Java will create a new String object in normal (nonpool) memory and s will refer to it. In addition, the literal “abc” will be placed in the pool. It creates two objects and one reference variable.
What is difference between String length and array length?
Arrays have an attribute (not a method) called length. You may encounter questions in the exam that attempt to use the length() method on an array or that attempt to use the length attribute on a String. Both cause compiler errors-consider these, for example:
String x = “test”;
System.out.println( x.length ); // compiler error
and
String[] x = new String[3];
System.out.println( x.length() ); // compiler error
What is the result?
String x = “0123456789”;
System.out.println( x.substring(5) );
System.out.println( x.substring(5, 8));
First one is “56789”
Second one is “567”
When should one use StringBuilder class?
The java.lang.StringBuilder class should be used when you have to make a lot of modifications to strings of characters.A common use for StringBuilders is file I/O when large, ever-changing streams of input are being handled by the program. In these cases, large blocks of characters are handled as units, and StringBuilder objects are the ideal way to handle a block of data, pass it on, and then reuse the same memory to handle the next block of data.
What is the difference between StringBuilder and StringBuffer?
Everything same apart from synchronization. StringBuffer’s method are sync.ed so it’s thread safe but slower.
What is the result? StringBuilder sb = new StringBuilder("abc"); sb.append("def"); System.out.println("sb = " + sb);
StringBuilder sb = new StringBuilder("abc"); sb.append("def").reverse().insert(3, "---"); System.out.println( sb );
First one is “sb = abcdef” and second one is “fed—cba”.
These StringBuilder methods operate on the value of the StringBuilder object invoking the method. So a call to sb.append(“def”); is actually appending “def” to itself (StringBuilder sb).
What are the rules to keep in mind when appending and inserting to StringBuilder?
- If an append() grows a StringBuilder past its capacity, the capacity is updated automatically.
- If an insert() starts within a StringBuilder’s capacity, but ends after the current capacity, the capacity is updated automatically.
- If an insert() attempts to start at an index after the StringBuilder’s current length, an exception will be thrown.
What is the result?
StringBuilder sb2 = new StringBuilder(“pi = “);
sb2.append(3.14159f);
System.out.println(sb2);
Output is “pi = 3.14159”
What is the result? StringBuilder sb = new StringBuilder("0123456789"); System.out.println(sb.delete(4,6));
Output is “01236789”