Skip to content

book excerpts

Very terse extract if the java book "The java programming language 4th edition". This is the book from the creators of java themselves. I will mention interesting facts in the same order as the books chapters.

1.7 classes and objects

Classes can have three kinds of members :

  • fields
  • methods
  • subclasses and interfaces

1.7.1 Creating objects

If you don't provide a constructor a no-args constructor is generated for the class.

1.8.3 static - or Class methods

You can not only have a static member but also a static method, which then can only access static members of the class since there is no current object en thus no 'this'. The name 'method' is used for object methods and explicit 'static method' for static ones.

You could pass the this object as a parameter to a static method, but doing that probably means your method is not meant to be static !

1.10.1 String conversion

String concatenation is done by using the toString() method for non string arguments. So

string concatenation
int x = 10;
String s = "X has a value of " + x;

Is equivalent to

same as
String s = "X has a value of " + x.toString();

1.10.1 String formatting

The function System.out.printf() internally uses the java.util.Formatter class. Example :

string formatting
System.out.printf("The value of PI is %.3f %n", Math.PI); or 
System.out.format("The value of PI is %.3f %n", Math.PI);

Is a shorthand for :

means
1
2
3
4
StringBuilder sb = new StringBuilder();
Formatter formatter = new Formatter(sb, Locale.US);
formatter.format("The value of PI is %.3f", Math.PI);
System.out.print(sb);

1.11 Extending a class

not mentioned here.. but i will say it : A subclass's constructor first calls the superclass constructor as the first statement. If you do it yourself, there is no real difference, and if you don't use it as the first statement you get a compile error. :

inheritance
1
2
3
4
5
6
class x extends y {
    x() { 
        super(); // ok but pointless
        super(); // an error, ... super() must be the first statement
    } 
} 

1.11.2 The object class

Classes that do not explicitly extend a class implicitly extend the Object class. So ultimately all objects have Object as the root of their hierarchy.

object class
1
2
3
4
5
6
// no constructor is allowed 
static void run() {
    Integer x = 11;
    System.out.println(x);
    System.out.println(x instanceof Object);
}

prints

output
11 
true

Of course this works for all reference types, but not for basic types like int and float.

1.12 interfaces

An interface is like a class but has only empty declarations of it;'s methods. It only dictates the methods, but gives no implementation, the subclass should fill those in.

All the members of an interface are public.

  • A classes supertypes are the class it extends and the interfaces it implements, including all their supertypes.
  • A classes subtypes are the classes that extend including all of their subtypes.
  • An interfaces subtypes are the classes that implement it, and the interfaces that extend it, including all of their subtypes.

That last one seems turned around but it's correct, classes implement an interface, interfaces can be extended.

1.15 annotations

An annotation is considered a modifier. It is however placed in a separate line above instead of before a function or method.

An annotation is a special kind of interface (NOT a class)

1.16 packages

Packages are mainly used for resolving name clashes.

By prefixing package names with your FQDN you already have a globally used scheme that resolves names and can be absolutely sure no one uses :

package
org.klopt.package.print();

Import statements just provide information to the compiler :

package
import org.klopt.package;

Does not 'include' the complete package into the code.

  • if a class is not specified via a package declaration the class is made part of the 'unnamed package'