Java Essentials Printer Friendly
Java Essentials Printer Friendly
Contents
Contents
1 Arrays and Lists 1
2 Objects 2
3 Constructors 3
4 References 4
5 Exceptions 5
6 Javadoc 6
7 Annotations 6
8 Interfaces 6
9 Inheritance 7
List
• It is better to use a java.util.List if the number of elements is not both fixed and known.
1 import java.util.ArrayList;
2 import java.util.List;
3
4 public class Lists {
5 public static void main(String[] args) {
6 List myList = new ArrayList();
7 myList.add("Hej");
1
8 myList.add(3);
9 }
10 }
• A List can contain objects of any class, this example stores a String (line 7) and an Integer
(line 8).
Generic List
• A generic list can be iterated using a for-each loop, see lines 11-13.
1 import java.util.ArrayList;
2 import java.util.List;
3
4 public class Lists {
5 public static void main(String[] args) {
6 List<String> myList =
7 new ArrayList<>();
8 myList.add("Hej");
9 myList.add("Hopp");
10
11 for(String value : myList) {
12 System.out.println(value);
13 }
14 }
15 }
2 Objects
What is an Object?
• The goal of object-oriented programming is to declare classes that group data and methods oper-
ating on that data.
• A class represents an abstraction, for example person. An object of the class represents a specific
instance of the class, for example the person you.
2
Code Example
• Create class
• If for example the account balance was static, all accounts would have the same balance. Such a
program would be useless.
• Since fields can not be static, neither can methods since static methods can only access static
fields.
• Static fields and methods are normally not used at all, except in few very special cases.
Code Example
3 Constructors
Providing Initial Values
• The values passed to the constructor are saved in the object’s fields on lines 6 and 7.
3
Calling the Constructor
Account acct = new Account(1234567, 100);
• Parameters are passed to the constructor just the same way parameters are passed when an ordinary
method is called.
• We need more constructors if we do not always provide the same set of initialization parameters.
• The constructor on lines 5-7 is used when no initial balance is specified.
• Calls constructor on lines 9-11, with balance = 0.
4 References
A Reference Is a Value
4
• A reference can, like any other value, be stored in variables, sent to methods, sent to constructors,
etc.
• Whenever the new operator is used, a new object with a new reference is created. Many bugs
arise because wrong reference is used.
Code Example
• Passing references
5 Exceptions
Exception Changes Execution
5
Code Example
Runtime Exceptions
• There are also runtime exceptions, which inherits the class java.lang.RuntimeException.
6 Javadoc
Javadoc
• It is strongly recommended to write Javadoc for all declarations (classes, interfaces, methods,
fields etc) that are not private.
• The tags @param and @return are used to document method parameters and return values.
Code Example
Write Javadoc comments and generate html pages.
7 Annotations
Annotations
• Annotations provide information about a piece of source code for the compiler, JVM or something
else.
• Usually used for properties unrelated to the functionality of the source code, for example to con-
figure security, networking or multithreading.
6
8 Interfaces
Interface Is a Contract
• An interface is a contract. A class implementing the interface must fulfill the contract specified
by the interface.
• The contract is specified as a set of methods. The implementing class must provide implementa-
tions for those methods.
• The methods must do what is intended in the interface. This should be documented in the interface.
Interface Example
The following interface defines the contract Write the specified string to the log.
public interface Logger {
/**
* Writes the specified message to the log.
* @param message This string is written
* to the log.
*/
void log(String message);
}
• The @Override annotation specifies that the annotated method should be inherited from a
superclass or interface.
• Always use @Override for inherited methods since it eliminates the risk of accidentally speci-
fying a new method.
• For example accidentally naming the method logg instead of log in the implementing class in
the previous example.
9 Inheritance
Inheritance
Everything in the superclass that is not private is also present in the the subclass.
7
public class Superclass {
public void methodInSuperclass() {
System.out.println(
"Printed from methodInSuperclass");
}
}
Override (Omdefiniera)
• A method in the subclass with the same signature as the method in the superclass will override
the superclass’ method.
• Overriding means that the overriding method will be executed instead of the overridden.
• Do not confuse with overloading (överlagra), which is to have methods with same name but dif-
ferent signatures, due to different parameter lists. This has nothing to do with inheritance.
Override Example
public class Superclass {
public void overriddenMethod() {
System.out.println("Printed from overriddenMethod" +
" in superclass");
}
}
8
To Call the Superclass
super is a reference to the superclass.
public class Superclass {
public void overridenMethod() {
System.out.println("Printed from Superclass");
}
}