Chapter 6: Strings, I/O, Formatting, and Parsing Flashcards
How many objects are created? String s1 = new String("abc"); String s2 = s1; String s2 = s1.concat("def");
3: abc, def and abcdef
Only two are still referenced abc and abcdef
What is the difference between:
String s1 = "abc"; // 1 String s2 = new String("abc"); // 2
- The string literal “abc” will go into the String literal pool and s1 will refer to it.
- A new object will be created in the normal memory pool, and s2 will refer to it. In addition the String literal “abc” will go into the String literal pool.
String methods: Describe the method for getting a character from an specific index in a String.
public char charAt(int index)
Zero-based:
“MyString”.charAt(0); // returns ‘M’
String methods: Describe the method for concatting a String.
public String concat(String s)
String test = “test”;
test.concat(“123”);
test + “123”;
test += “123”;
String methods: Describe the equals methods.
public boolean equals(String s)
public boolean equalsIgnoreCase(String s)
String methods: Describe the method for retrieving the String size.
public int length();
“123”.length() // reeturns 3
String methods: Describe the method for replacing every occurance of a character in a String.
public String replace(char old, char new);
“xoxoxoxoxoxoxox”.replace(‘o’,’0’);
String methods: Describe the method for retrieving a part of a String.
public String substring(int begin)
public String substring(int begin, int end)
The begin argument is zero-based. The end argument is not zero based.
String s = “0123456789”;
s. substring(5); // 56789
s. substring(5, 8); // 567
String methods: Describe the mothods for lowercasing and upercasing.
public String toLowerCase();
public String toUpperCase();
String methods: Describe the method for removing whitespace
public String trim();
” hello world “.trim(); // returns “hello world”
What is the differecence between the StringBuffer and StringBuilder classes?
StringBuffer is threadsafe, which means it’s methods are synchronized. StringBuilder is not threadsafe but is faster.
StringBuffer/StringBuilder methods: Describe the method for appending.
public StringBuilder append(String s)
It can also take other arguments: boolean, char, double, float, int, long and others.
StringBuilder sb = new StringBuilder("test"); sb.append("123"); // sb is now test123
StringBuffer/StringBuilder methods: Describe the method for deleting a part of a String
public StringBuilder delete(int start, int end);
The start argument is zero-based the end argument is not zero-based.
StringBuilder sb = new StringBuilder("0123456789"); sb.delete(4,6); // sb is now 01236789
StringBuffer/StringBuilder methods: Describe the method for inserting a String.
public StringBuilder insert(int offset, String s)
The offset argument is zero-based. It can also take other arguments: boolean, char, double, float, int, long and others.
StringBuilder sb = new StringBuilder("01234567"); sb.insert(4, "---"); // sb is now "0123---4567
StringBuffer/StringBuilder methods: Describe the method for reversing a String.
public StringBuilder reverse();
StringBuilder sb = new StringBuilder("123"); sb.reverse(); // sb is now "321"
Describe the reader classes for File reading
FileReader
Used to read character files. Methods are fairly low level, allowing to read single characters, whole stream or a fixed number of characters.
BufferedReader
Used to make lower-level classes like FileReader easier to use. Keeps large chuncks of data in its buffer so file read operations are kept to a minimum.
Describe the writer classes for file writing
FileWriter
Used to write to character files. Methods are faily low level.
BufferedWriter
Used to make lower-level classes like FileWriter easier to use. Writes large chuncks of data at a time to keep write operations at a minimum.
PrintWriter
Easy write capabilities for files. Contains a set of handy methods and constructors (building a PrintWriter using a String or File).
How do you create a new (empty) file on disk?
How do you check if a file exists?
File file = new File("test.txt"); boolean exists = file.exists(); boolean newFileCreated = file.createNewFile();
What do the flush and close methods on the FileWriter class do?
Flush
Write the last data from the buffer to the file.
Close
Close the file and give back the resources to the system.
How do you write to a file using the FileWriter?
File file = new File("test.txt"); FileWriter fw = new FileWriter(file); fw.write("testcontent"); fw.flush(); fw.close();
How do you read (to a char array) a file using a FileReader?
char[] contents = new char[50]; File file = new File("test.txt"); FileReader fr = new FileReader(file); fr.read(contents); fr.close();
What are two ways to create actual files from Java?
- call the createNewFile() method on a File object.
2. Create a Writer or Stream: FileWriter, PrinterWriter or a FileOuputStream.
How to create a new empty file in a new dir?
File dir = new File(“dir”);
dir.mkdir();
File file = new File(dir, “test.txt”);
file.createNewFile();