Java Koans – Beginner – Part 1

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

AboutEquality

  • == test if two objects are the same objects
  • equals method also test if two objects are the same
  • however, the subclass can change equals method to test if two objects are equal
Object object = new Integer(1);
Object diffObject = new Integer(1);
assertEquals(object.equals(object), true);
assertEquals(object.equals(diffObject), true);
assertEquals(object == diffObject, false);
  • new object does not equal to null

AboutPrimitives

  • primitive type : its object type : size : MIN_VALUE : MAX_VALUE
    • int:Integer:32:-2147483648:2147483647
    • long:Long:64:-9223372036854775808l: 9223372036854775807l
    • short:Short:16:-32768:32767
    • byte:Byte:8:-128:127
    • char:Character:16:0:65535
    • double:Double:64:4.9E-324:1.7976931348623157E308
    • float:Float:32:1.4E-45f:3.4028235E38f
assertEquals(getType(1), int.class);
Object number = 1;
assertEquals(getType(number), Integer.class);
assertEquals(Integer.Size, 32);

AboutStrings

  • new String() equals to “” (empty string)
  • MessageFormat allow reuse of same arguments in the token
assertEquals(MessageFormat.format("{0} {1} {0}", "a", "b"), "a b a");
assertEquals(MessageFormat.format("{0} {1} {0}", "a", "b", "c"), "a b a");
assertEquals(MessageFormat.format("{0} {1} {0}", "a"), "a {1} a");

AboutObjects

  • toString() is the same as Object.class.getName() @ Integer.toHexString(object.hashCode())) if it is not override.

AboutLoops

  • for loop
    • for (int j : lst) –> where lst is an array of int.
  • for loop continue with label mean execution stop in the inner loop but continue for the outerloop in this case. (count += 10 never executed). In contrast, the count = 75 without the label.
    • enter the outer for loop with i = 0
      • enter the inner for loop with j = 0
        • increment count to 1
        • repeat inner loop for j = 1, 2 and count = 1 + 2 =3
        • count > 2 so continue to outerLabel
    • repeat outer loop for i = 1, enter inner loop for j=0 with count = 4 and hit continue outerLabel again
    • do this until i = 5 (i = 2, 3, 4 and count = 4+3 = 7).
public void forLoopContinueLabel() {
  int count = 0;
  outerLabel:
  for(int i = 0; i < 5; i++) {
    for(int j = 0; j < 5; j++) {
      count++;
      if(count > 2) {
        continue outerLabel; 
      }
    }
    count += 10;
  }
  // What does continue with a label mean? 
  // What gets executed? Where does the program flow continue?
  assertEquals(count, 7);
}
  • for loop break with label mean execution leave all loop in this case and continue after the outer for loop. In contrast, count = 57 without the label.
    • enter the outer loop with i = 0
      • enter the inner loop with j = 0
        • increment count to 1
        • repeat inner loop for j = 1, 2 and count = 1 + 2 = 3
        • count > 2 so break to outerLabel and done.
    public void forLoopBreakLabel() {
        int count = 0;
        outerLabel:
        for(int i = 0; i < 5; i++) {
            for(int j = 0; j < 5; j++)
            {
                count++;
                if(count > 2) {
                    break outerLabel;    
                }
            }
            count += 10;
        }
        // What does break with a label mean? 
        // What gets executed? Where does the program flow continue?
        assertEquals(count, 3);
    }
Advertisements