Studydroid is shutting down on January 1st, 2019

Bookmark and Share

Front Back
What's the big different between StringBuilder and StringBuffer
StringBuilder isn't thread-safe so it's faster. StringBuffer and StringBuilder have EXACTLY THE SAME INTERFACE!
String x = ""hello ""; x.concat(""world""); System.out.println(x); What prints?
""hello "" Most (all?) methods called on a string to change that string create a new String object which must be assigned to a reference --- notice that the original reference points to the unchanged String.
String s1 = ""spring ""; String s2 = s1 + ""summer ""; s1.concat(""fall ""); s2.concat(s1); s1 += ""winter ""; System.out.println(s1 + "" "" + s2); What is the output? How many String objects were created prior to the println statement?
output: spring winter spring summer String objects created (8): 1: ""spring "" (s1) 2: ""summer "" (lost) 3: ""spring summer "" (s2) 4: ""fall "" (lost) 5: ""spring fall "" (lost) 6: ""spring summer spring "" (lost) 7: ""winter "" (lost) 8: ""spring winter "" (s1 ""spring "" now lost) Strings are immutable so they can't be updated. Instead when a string is ""updated"" - a brand new string is actually created.
String s1 = new String(""test""); String s2 = new String(""test""); How many string objects are created by this statement?
3. 1: the literal ""test"" gets turned into a String object and placed in the ""String constant pool"" 2: a new String object with the value ""test"" placed in the non-pool. 3: another new String object with the value ""test"" also placed in the non-pool.
String method - returns the character located at the specified index?
public char charAt(int index)
String method - appends one String to the end of another?
public String concat(String s)
String method - determine the equality of two Strings ignoring case?
public boolean equalsIgnoreCase(String s)
String method - returns the number of characters in the String?
public int length()
String method - swaps occurrences of some character with a new character?
public String replace(char old char new)
String method - returns part of a string?
public String substring(int begin) public String substring(int begin int end) NOTE: ""substring"" is all lower case NOTE: begin is inclusive and end is exclusive
String method - return string with uppercase converted to lowercase?
public String toLowerCase() NOTE: starts with ""to"" NOTE: IS camel cased!
String method - return string with lowercase converted to uppercase?
public String toUpperCase()
String method - return string with whitespaces removed from front AND end?
public String trim()
StringBuffer str = """"; What is wrong with this?
Only String objects use the shortcut of creation by assignement of a literal. To create a StringBuffer object you must explicitly build an object: new StringBuilder(""..."");
Name 2 big differences between a String and a StringBuffer
A String is immutable while a StringBuffer isn't - it can be changed. This leads to the second big difference. String methods do not change the original string and instead only return new strings. StringBuffer methods though do change the object on which the method is called. String s = ""test""; s = s.concat("" one""); // calling object unchanged. new String object is created and s now points at it. StringBuffer sb = ""test""; sb.append("" two""); // change calling object. sb still points at same object which has had it's contents changed
StringBuffer method - add to the end?
public synchronized StringBuffer append(String s) NOTE: Returns a reference to itself for chaining of calls. NOTE: StringBuilder method is same except it is NOT synchronized.
StringBuilder method - return with a substring removed?
public StringBuilder delete(int begin int end) NOTE: object itself is changed as with all StringBuilder/StringBuffer methods NOTE: begin is inclusive end is exclusive NOTE: there is no overloaded type with only a single begin argument
StringBuilder method - insert a string at a certain offset point?
public StringBuilder insert(int offset String s) NOTE: all insert methods require an offset NOTE: offset is inclusive NOTE: everything at and above insert point is moved over to make way for the inserted string
StringBuffer method - reverse contents?
public synchronized StringBuffer reverse()
What's the basic difference between InputStream/OutputStream and Reader/Writers?
Streams are about moving bytes while Reader/Writers are about moving characters.
What does the File class represent?
An abstract represenation of a file and directory pathnames.
What are FileReader/FileWriter used for?
Read and Write characters out of files.
Describe how BufferedReader/BufferedWriter are used.
Both take lower level Reader and Writer classes (i.e. FileReader & FileWriter) easier to manipulate. NOTE: BufferedWriter provides method: newLine() which allows for creation of platform specific line separators (newLine is camel cased) NOTE: BufferedReader has a readLine() method (readLine is camel cased)
Enhanced in Java 5. In past could only construct with OutputStream or a Writer. As of 1.5 can also create with a File and String. close() flush() format() print() printf() println() write()
What package do you find java I/O in?*
File f = new File(""temp.txt""); If temp.txt doesn't exist is it created automatically?
No. Creating a File object is not the same as creating a file on disk. Simply creating a File has nothing to do with any existing (or not existing) file. You must MANIPULATE/USE the File object for actual files to be affected.
How do you check to see if a file exists?
File f = new File(""myfile.txt""); if (f.exists()) {}
How do you create a new file on disk?
File f = new File(""temp.txt""); try { f.createNewFile(); } catch (IOException e) {} NOTE: There are other classes that automatically create files as part of their object creation (i.e. BufferedReader / BufferedWriter) NOTE: return true if file doesn't exist and was created return false if already exists. NOTE: createNewFile()might throw an IOException as do most I/O operations.
What are the 3 key methods of the FileWriter?
close() flush() write()
What is the 1 key method of the FileReader?
What method does BufferedReader add to a Reader it wraps?
readLine(); NOTE: notice that camel case!
What are the key constructor argument types of a File?
(String) (String String) (File String)
What are the key constructor argument types of a FileWriter/FileReader?
File or String
How do you create a new directory?
File f = new File(""foo""); f.mkdir();
Assume temp.txt does not exist. File f = new File(""temp.txt""); FileWriter fw = new FileWriter(f); Compiles? Runs?
Yes and yes. temp.txt will actually be automatically created on disk when the FileWriter (or a FileReader) object is created.
File f1 = new File(""foo""); File f2 = new File(f1 ""foo""); f2.createNewFile(); Compiles? Runs?
Compiler error. The createNewFile() has to be wrapped in a try clause because IOException might be thrown. If it's wrapped thusly it will throw an IOException if the directory ""foo"" doesn't exist. If directory ""foo"" exists but the file ""foo"" doesn't it will create the file and return true.
Assume relative directory foo/foo doesn't exist. File f1 = new File(""foo/foo""); try{ f1.mkdir(); } catch (IOException e){} Compiles? Runs?
Yes and yes. Running does NOT mean the directory gets created though. If the first foo directory already exists then the second WILL get created and true is returned. If the first foo directory does NOT exist then the mkdir() method creates no directories and false is returned. An exception is NOT thrown! mkdir() can only create one directory at a time.
Assume directory foo does not exist nor does foo.txt FileWriter f = new FileWriter(""foo/foo.txt""); Compiles? Runs?
During runtime an IOException will be thrown because while FileWriter/FileReader can create files when their objects are created... they cannot also create directories. if foo directory did exist this would run w/out throwing an exception and the foo.txt would get automatically created.
How do you know when you're at the end of a file using a BufferedFileReader?
when readLine() returns null. NOTE: readLine is camel cased.
File dir = new File(""foo""); dir.mkdir(); File f = new File(""foo/temp.txt""); try{ f.createNewFile(); } catch(IOException e) {} dir.delete(); Compiles? Runs?
Yes and yes. A directory named ""foo"" is created on disk and then in that directory a file called temp.txt is also successfully created on disk. When an attempt to delete the directory is made it simply returns false because you cannot delete a non-empty directory. Notices that it does NOT throw an exception in this case.
How do you rename a File?
Use the renameTo(File f) method on the File you want to change.
What is the difference between these 2 declarations? How do they function differently? File f1 = new File(""foo"" ""temp.txt""); File f2 = new File(""foo/temp.txt"");
They are the same. They function exactly the same.
File dir = new File(""foo""); dir.mkdir(); File f = new File (""foo/stuff.txt""); try { f.createNewFile(); } catch (IOException e){} File dir2 = new File(""fee""); dir.renameTo(dir2); Compiles? Runs? Exception thrown?
It compiles runs and no exception is thrown (assuming system allows file creation) Directory foo gets created. Then stuff.txt gets put inside of it. Then directory foo simply gets renamed to fee -- it doesn't matter that foo had stuff in it. The is now no foo directory just a fee directory with a file called stuff.txt
How do you generate a list of all the files in directory?
File dir = new File(""foo""); []String files = dir.list();
What I/O streams are used to serialize and then unserialize objects? What methods are called to perform these serialization behaviors?
ObjectOutputStream.writeObject(); ObjectInputStream.readObject();
What argument does the ObjectOutputStream constructor include?
An OutputStream or no arguments(used by children of this class). There is not constructor that takes a File or a String.
What is definition of the interface used to serialize?
public interface Serializable{} NOTE: Serializable is a ""marker"" interface -- just marks any implementing objects as a certain kind of thing (in this case objects mark themselves as Serializable things)
What will you probably have to do after calling readObject() on an ObjectInputStream?
Cast the Object to it's actual class.
Does serialization make deep or shallow copies of everything it stores?
Deep. All the objects that the top level objects owns are (and that they then own and so on) are also serialized. Primitives are also serialized.
If a object marked Serializable owns an instance of another object type that is NOT serializable what happens when you call on writeObject() on the top level object?
Runtime error. NotSerializableException
x of y cards Next > >|