Java Koans – Intermediate

These are some notes from the Java Koans that is part of the assignment from the Advance Java Programming Class from PSU CS410J.

AboutAutoboxing

  • Some reference regarding boxing in Java
  • auto-boxing example: int-> Integer, long-> Long
  • auto-unboxing example: Float->float, Boolean->boolean
  • auto-unboxing a null Object will cause a NullPointerException
int x = 1;
Integer y = new Integer(x);
x == y;      // auto-unbox y and compare value
x.equals(y); // will not compile regardless what y is
y.equals(x); // auto-box x and compare value

AboutCollections

  • List, ArrayList method add and get
  • Queue, PriorityQueue method add, peek, size, poll, isEmpty
  • Set, HashSet method add, size, contains
  • Map, HashMap method put, size, containsKey, containsValue, get.
  • Both the list and array reference to the same array “list” and if one of the element changes, the same element in the other one changes as well because of the same reference.
String[] array = {"a","b","c"};
List<String> list = Arrays.asList(array);
  • TreeMap and SortedMap. backedMap is a subMap of map which mean backedMap is a view of map. backedMap references to map and if we add something to backedMap, we are essentially adding it into map.
TreeMap<String, String> map = new TreeMap<String, String>();
map.put("a", "Aha");
map.put("b", "Boo");
map.put("c", "Coon");
map.put("e", "Emu");
map.put("f", "Fox");
SortedMap<String, String> backedMap = map.subMap("c", "f");
  • TreeSet is an ordered set and LinkedHashSet is a linked list implementation of set with predictable iteration order.

AboutComparison

  • a.compareTo(b) return 0 if equal, 1 if a > b and -1 if a < b.
  • implements Comparable to make Objects comparable.
  • implements Comparator to impose ordering.

AboutDates

  • Jan 1, 1970 GMT is the origin date and time. 1L = 1 millisecond pass the origin data and time.
  • Date references.
  • add() add the value to the whole date value. e.g. July 1, 1970 and add 8 months will give March 1, 1971.
  • roll() does not change the larger value. e.g. July 1, 1970 and roll 8 months will give March 1, 1970 where year is unchanged.
  • Date format using getDateInstance().format
    • no arg or Date.format.MEDIUM: MON D, YYYY
    • Date.format.SHORT: M/D/YY
    • Date.format.FULL: DayOfWeek, Month D, YYYY
    • custom date using M=month, D=day and Y=year.

AboutEquality

  • Object == Object compare identity
  • Object.equals(Object) compare value
  • null vs Object discussion here.
  • override equals() and hashcode(). Here is another references.

AboutFileIO

  • File createNewFile() and delete() does what it said.
  • FileWriter write() and close() are self explanatory. flush() is a Flushable that write any buffered output to the underlying stream.
  • FileReader read() and close().
  • Here we write to a file using a string (size = 22), then we created a larger char array (new char[50]) and read the file into the char array. At the end, we turn the char array back to a string. The size of the read is 22, but the size of the char array and the size of the string that created from the char array after the read is still 50 even though it only output 22 characters when println.
File file = new File("file.txt");
FileWriter fw = new FileWriter(file);
fw.write("First line\nSecond line");
fw.flush();
fw.close();

char[] in = new char[50]; // <-- start with 50 empty char
int size = 0;
FileReader fr = new FileReader(file);
size = fr.read(in); // <-- fill up 22 char with 28 empty char left
fr.close();

// size of read == length of the original string
assertEquals(size, "First line\nSecond line".length());
// the size of the char array string is still 50!
assertEquals((new String(in)).length(), 50);
assertEquals("First line\nSecond line".length(), 22);
// finally the char array string has 28 empty character at the end!
assertEquals(new String(in), "First line\nSecond line"+(new String(new char[28])));

AboutInnerClasses

  • If OtherInner extends AboutInnerClasses, then an OtherInner object is instanceof AboutInnerClasses as well.

AboutLocale

  • Locale with getDisplayCountry, Use with getDateInstance and getCurrencyInstance … etc.
  • Language and Country code

AboutRegularExpression

  • Pattern compile is the pattern to match
  • Matcher has the string to check for a pattern to match with.
    • find() return boolean of whether it find the pattern
    • start() return index of the last matched
    • “\\.” mean match a “.” period.
    • .split(delimiter) split up a string into an array of string base on the delimiter

AboutSerialization

  • Additional References regarding Serialization. 1 2 3
  • If the subclass that implements serializable has a superclass that does not implement serializable and the constructor of the superclass is invoke from the subclass, serialization will not happen.
Advertisements