Data Formats in Java

Java Data Formats and other Basics

Primitive Java Data Types

Everything that directly maps onto hardware.

  • boolean: true/false (1 bit)
  • char: Unicode characters as numbers 0 to 216-1 (16 bit)
  • short: -215 to 215-1 (16 bit)
  • int: -231 to 231-1 (32 bit)
  • long: -263 to 263-1 (64 bit)
  • float: -2-149 to (2-2-23)*2127 (32 bit)
  • double: -2-1074 to (2-2-52)*21023 (64 bit)

Non-Primitive Data Types

Combines simple types to more complex but easier to use stuff.

  • Strings: Any kind of text
  • Arrays: Table of a data type accessible by an index
  • Class: Blueprints for objects
  • Double, Integer, Float, Long, Character, Byte, Boolean, Short: Classes of the primitives
  • Object: A "living" class
  • File: A handle to an OS file
  • Streams, channels: Stuff for I/O
  • ...: Much much more...

Something special

Special types and states

  • void: When we do not care about the type
  • null: When something is not defined yet, null is not 0 aka null is not Zero. 0/zero is a state, while null is the state of nothing.

Classes

Something that sounds a little more like Java.

  • A class defines properties, states, and logic or activities
  • It is the blueprint/template/manual to build an object
  • Fields are properties
  • Logic or activities are methods
  • A special method is the constructor
public class Invoice
{
    private int subtotal;
    private int tax;

    public Invoice(int amount)
    {
        this.subtotal = amount;
        this.tax = 20;
    }

    private int getTaxAmount()
    {
        return this.subtotal * (tax / 100);
    }

    public int getTotal()
    {
        return this.subtotal + getTaxAmount();
    }

    public int getSubTotal()
    {
        return this.subtotal;
    }
}

Objects

Turn your class templates into real things.

  • An object or instance is a materialized class definition
  • Objects of the same type are independent
  • Data space is reserved with every new object
  • Methods are not copied, because they cannot be changed
  • Data identical objects are not identical objects
public void anyExample()
{
    final Invoice invoice1 = new Invoice(12);
    final Invoice invoice2 = new Invoice(42);
    final Invoice invoice3 = new Invoice(12);

    int total = invoice1.getTotal() + invoice2.getTotal() + invoice3.getTotal();
}

Mutable and Immutable

Change is not always good

Mutable

  • You can change objects data after creation
  • Can have side effects
  • Has positive and negative memory effects
public class Invoice
{
    private int subtotal;

    public Invoice(int amount)
    {
        this.subtotal = amount;
    }

    public int getSubTotal()
    {
        return this.subtotal;
    }

    public void setSubTotal(int newSubTotal)
    {
        this.subtotal = newSubTotal;
    }
}

Immutable

  • You cannot change data anymore
  • Easier to understand but also side effects
  • Functional programming mantra
public class Invoice
{
    private final int subtotal;

    public Invoice(int amount)
    {
        this.subtotal = amount;
    }

    public int getSubTotal()
    {
        return this.subtotal;
    }
}