In this section we briefly discuss the Java programming language. We assume that the reader has already been exposed to basic programming in C++, including the object-oriented programming features of C++.
This is a list of various books for learning Java. Use them as reference
Java has borrowed a lot of syntax from C++, aiming to make the transition easier C++ programmers:
{ ... }
to group statements together into blocks.if-then-else
expressions, for
, while
and do-while
blocks and switch
statements are all similar to C.byte
, short
, int
, long
), each with fixed sizes, two floating point number types (float
and double
), a single character type char
, and a boolean
type with values true
and false
.null
value that is analogous to the null pointer in C++.int[] a = { 1, 4, 5 };
Item
, LinkedList
, Vertex
, Person
. Class constructors are used to create new objects: Person aPerson = new Person("Peter");
static
, and we can classify all object and class methods and properties as public or private (or also protected).final
. Constant properties are typically written in all capital letters.There are also a number of differences:
Object
, and all classes in Java implicitly inherit from Object
. All objects inherit base implementations for a number of methods, most important of which are: equals
, hashCode
and toString
StringBuilder
class is used when you want to incrementally build a string from smaller components.for
loop, often called the “foreach” loop, where you iterate over the elements of an array or list, rather than the indices: for (Item item : listOfItems) { ... do something with item ... }
. This is a safer iteration approach, as you don’t have to worry about running outside the array bounds, and it also works for other iterable structures that are not technically arrays. It is in many ways similar to Python’s for-in
loop.==
) tests quite literally if the objects occupy the same place in memory (same pointer). Most classes also implement an equals
method, that determines a more meaningful equality test (based for example on the properties of the object)..h
files in C++. An interface specifies a set of methods that a class should implement in order to claim that it obeys this interface. Interfaces are used to enforce certain behaviors: For instance any class that implements the Stack
interface must provide certain methods for popping from the stack, pushing onto the stack etc.Person
objects, or a list that contains Vertex
objects, and we only have to write the code once). So for example the ArrayList
class is usually written as ArrayList<E>
where E
is some other class indicating the contents of the list. A list of strings for example would be ArrayList<String>
.