A Little Bit of Java Core

Le Quang Truong

java

1034 Words 4 Minutes, 42 Seconds

2024-10-03 11:02 +0000


Java

Java Language

Java is a programming language. Java code is stored in files suffixed with .java. These files will be compiled into Java byte code using the Java compiler. The byte code is then executed using the JVM. The JDK contains the Java compiler and the JVM.

Java Bytecode

Storing in binary .class files. Made by compiling the Java language.
Can be executed by the JVM.

Java APIs

These APIs enable our Java programs to access the local file system, the network and many other things.
The standard Java APIs come bundled with the JRE or the Java SDK which also includes a JRE.

JRE

JRE = JVM + the standard Java APIs (+ JSE).
Using to execute a Java application, not to compile it.

Java SDK

Java SDK = JRE (+ Java APIs + JVM + JSE) + Java compiler + other tools.
Develope Java programs -> the full Java SDK (Java compiler).
Java Servers may need tools in JDK to compile JSPs (Java Server Pages) into Java Bytecode -> the full Java SDK (extra tools).

APIs, or “profiles”

  • Java SE: Desktop and standalone server applications (Java APIs + JRE + JDK)
  • Java EE: Develope and execute Java components that run embedded in a Java server
  • Java ME: Develope and execute Java applications on mobile phones and embedded devices

Data Types

Primitive Data Types

  • boolean: true or false
  • byte: 1 byte
  • short: 2 bytes
  • char: 2 bytes
  • int: 4 bytes
  • long: 8 bytes
  • float: 4 bytes
  • double: 8 bytes

Object Types

  • Boolean
  • Byte
  • Short
  • Character
  • Integer
  • Long
  • Float
  • Double
  • String: n byte Unicode string of textual data. Immutable.

Interfaces

A Java class that only contains method signatures (name + parameters + exceptions) and fields.
Used to achieve polymorphism.

An interface can have:

  • Constants
  • Default Methods
  • Static Methods

Java Types can implement interfaces:

  • Class
  • Abstract Class
  • Nested Class
  • Enum
  • Dynamic Proxy

Interfaces and Inheritance

An interface can inherit from another interface (just like classes can inherit from other classes) using keyword extends.
Inherit from multiple superinterfaces? Possible.

Inheritance and Default Methods

If two interfaces contain the same method signature and one of the interfaces declare this method as a default method, a class cannot automatically implement both interfaces. The situation is the same for an interface inherits from multiple interfaces.

The Java compiler requires that the class implementing the interface(s) explicitly implements the method which causes the problem. Not using the default implementation anymore.

Interfaces and Polymorphism

Polymorphism is a concept that takes an class (an object) can be used as if it were of different types. Here, a type means either a class or an interface.

Assume we have these classes (not in the same file):

1public class Vehicle {
2}
3
4public class Car extends Vehicle {
5}
6
7public class Truck extends Vehicle {
8}

Imagine we need to be able to store those objects in a database. We want that implemented using a single method for the operation, available for each object, like a store() method.

Creating methods directly on the objects may lead to a messy class hierarchy. They cannot be accessible on all classes.

One solution would be creating a common superclass for the Vehicle, which has the storage method. But then it would result a conceptual mess. The class hierarchy would no longer model vehicles, but also be tied to the storage mechanism used in the application.

A better solution: create an interface with the storage method on, and let the classes implement these interfaces.

1public interface Storable {
2  public void store();
3}

When each classes implement the interface and its method store(), we can access the method of the interface by casting the objects to instances of the interface types. We don’t need to know exactly what class a given object is of, as long as we know what interface it implements.

1Car car = new Car();
2
3Storable storable = (Storable) car;
4storable.store();

Interfaces vs. Abstract Classes

Interfaces are used to decouple the interface of some component from the implementation, or to make the classes using the interface independent of the classes implementing the interface. So, we can exchange the implementation of the interface, without having to change the class using the interface.

Abstract classes are used as base classes for extension by subclasses. A Java class can only have 1 superclass, but it can implement multiple interfaces. If a class already has a different superclass, it can implement an interface, but it cannot extend another abstract class. Thus, interfaces are a more flexible mechanism for exposing a common interface.

Collections

Java Collection

We normally do not instantiate a Collection directly, but rather a subtype of Collection (List, Set, SortedSet, NavigableSet, Queue, Deque). We may often treat these subtypes uniformly as a Collection

1// Collection is an interface from java.util.Collection, one of the root interfaces of the Java Collection API
2Collection collection = new ArrayList();

List

The interface java.util.List represents an ordered sequence of objects.

List vs. Set

  • The same element can occur more than once in a List, not in Set
  • The elements in a List has an order, and they can be iterated in that order

Implementations

All implementations of List:

  • ArrayList
  • LinkedList
  • Vector
  • Stack
  • Concurrent List implementations in java.util.concurrent

Set

The interface java.util.Set represents a collection of objects where each object is unique.

Implementations

All implementations of Set:

  • EnumSet
  • HashSet
  • LinkedHashSet
  • TreeSet

HashSet is backed by a HashMap. No guarantees about the sequence of the elements when we iterate them.

LinkedHashSet guarantees that the order of the elements during iteration is the same as the order they were inserted into the LinkedHashSet. Reinserting an existed element does not change this order.

TreeSet also guarantees the order when iterated, but the order is the sorting order of the elements, like using Collection.sort() on a List or array containing these elements. This order is determined by their natural order (if they implement Comparable), or by a specific Comparator implementation.

There are also Set implementations in java.util.concurrent.

Map

The interface java.util.Map represents a mapping between a key and a value. This interface is not a subtype of the Collection interface.

Implementations

All implementations of Map:

  • HashMap
  • Hashtable
  • EnumMap
  • IdentityHashMap
  • LinkedHashMap
  • Properties
  • TreeMap
  • WeakHashMap