Java data types



Java SE 11 Developer certification (exam number 1Z0-819)

Working with Java data types

Headlines
Java primitive types are boolean (false and true as possible values), char, byte, short (16-bit long), int, long (64-bit long), float and double

Rule(s)

Example Primitive_types.Java.zip 

try {
    assert (java.lang.Void.class == void.class); // The 'Void' type *IS NOT* itself the direct type of 'void'
    assert (java.lang.Void.TYPE == void.class); // Yes, it works!
} catch (AssertionError ae) { // Run program with '-ea' option
    System.err.println(ae.getMessage()); // Error comes from first assertion!
}

Rule(s)

Example

float x = 1.F;
Float y = new Float(1.F); // Heavy style, to be avoided…
float z = x + y; // Autoboxing: 'float' and 'Float' are seamlessly compatible

Rule(s)

Example Primitive_types.Java.zip 

int i;
// assert(i instanceof int); // Type of a variable like 'i' having a primitive type like 'int' cannot be tested (compilation error) 
// assert(i instanceof Integer); // Compilation error as well...
String surname = "Barbier";
assert(surname instanceof String); // This works (no assertion error at execution time)! Don't forget that 'java.lang.String' is not a primitive but a constructed type

Example

public class User_defined_type {
    boolean attribute0;
    byte attribute1;
    short attribute2;
    int attribute3;
    long attribute4;
    float attribute5;
    double attribute6;
    char attribute7;

    public static void main(String[] args) {
        User_defined_type object = new User_defined_type();

        object.attribute0 = true;
        System.out.println(object.attribute0); // 'true'
        object.attribute1 = '\r';
        System.out.println(object.attribute1); // '13'
        object.attribute2 = 0x7777;
        System.out.println(object.attribute2); // '30583'
        object.attribute3 = 0xFFFFFFFF;
        System.out.println(object.attribute3); // '-1'
        object.attribute4 = Long.MAX_VALUE;
        System.out.println(object.attribute4); // '9223372036854775807'
        object.attribute5 = 6.5E1F;
        System.out.println(object.attribute5); // '65.0'
        object.attribute6 = 7.2e-2D;
        System.out.println(object.attribute6); // '0.072'
        object.attribute7 = 66;
        System.out.println(object.attribute7); // 'B'
    }
}

Rule(s)

Example

short s1 = 0;
int i = s1; // Type promotion...
short s2 = (short) i; // Casting with possible undesired side effects (data loss)...

Rule(s)

Example

StringBuilder s = new StringBuilder("");
s.append("Franck");
s.append(' ');
s.append("Barbier");

String s1 = "", s2 = s1;
assert (s1 == s2);
s1 += "Franck"; // 's1' refers to a *NEW* memory object...
s1 += ' '; // Again, 's1' refers to a *NEW* memory object...
s1 += "Barbier"; // And, again, 's1' refers to a *NEW* memory object...
assert(s1 != s2);
Basic (i.e., primitive) arrays in Java

Rule(s)

Example

assert (!(given_name instanceof Object)); // So, primitive type...
assert (given_name.getClass() == char[].class); // More precisely...

Rule(s)

Example

Object tab[] = new Object[1000];
String another_tab[] = new String[1000];
assert(tab instanceof Object[]);
assert(another_tab instanceof Object[]);
// Initialization:
Object my_tab[] = new Object[]{new Object()}; // 'my_tab' is created with one object inside at position '0'
assert(my_tab.length == 1);

Rule(s)

java.lang.Object class

Rule(s)

Multithreading related method(s) in java.lang.Object

void wait() throws InterruptedException // + variants
void notify() // + variants

Object identity related method(s) in java.lang.Object

protected Object clone() throws CloneNotSupportedException
boolean equals(Object object)
int hashCode()

Example Hashtable_illustration.Java.zip 

String s1 = "Franck";
String s2 = "Franck";
assert (s1 != s2); // 's1' and 's2' are NOT the same memory object
assert (s1.equals(s2)); // However, 's1' equals to 's2'
assert (s1.hashCode() == s2.hashCode());

Utility related method(s) in java.lang.Object

String toString()

Finalization related method(s) in java.lang.Object

protected void finalize() throws Throwable

Example

System.finalization();
// Alternatively:
Runtime.getRuntime().runFinalization();
Type inference

Rule(s)

var keyword (Java 10)

Example (diamond-based instantiation omits effective type(s) in place of generic type(s))

java.util.Map<Integer, Double> polynomial = new java.util.HashMap<>();
polynomial.put(1, -12.D);
polynomial.put(39, 8.D);

Example

// var polynomial = new java.util.HashMap<>(); // Danger => 'java.util.HashMap<Object,Object>'
var polynomial = new java.util.HashMap<Integer, Double>(); // Diamond-based instantiation is avoided with 'var'...
polynomial.put(1, -12.D);
polynomial.put(39, 8.D);

var keyword in conjunction with lambda expression (Java 11)

Rule(s)

Example From_Java_9.Java.zip 

var positives = java.util.Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
var stream = positives.stream().filter((var p) -> p > 1 && java.util.stream.IntStream.rangeClosed(2, (int) Math.sqrt(p)).noneMatch((var i) -> (p % i == 0)));
var primes = stream.collect(java.util.stream.Collectors.toList());
primes.forEach((var p) -> System.out.print(" " + p)); // '2 3 5 7 11' is displayed...

Rule(s)

Example From_Java_9.Java.zip  (annotation imposes Integer and int as types of lambda expression parameters)

@java.lang.annotation.Target(value = java.lang.annotation.ElementType.PARAMETER)
@interface Must_be_positive {
}
…
var stream = positives.stream().filter((@Must_be_positive Integer p) -> p > 1 && java.util.stream.IntStream.rangeClosed(2, (int) Math.sqrt(p)).noneMatch((@Must_be_positive int i) -> (p % i == 0)));
…
primes.forEach((@Must_be_positive Integer p) -> System.out.print(" " + p)); // '2 3 5 7 11' is displayed...

Example From_Java_9.Java.zip  (using var ejects Integer and int)

…
var stream = positives.stream().filter((@Must_be_positive var p) -> p > 1 && java.util.stream.IntStream.rangeClosed(2, (int) Math.sqrt(p)).noneMatch((@Must_be_positive var i) -> (p % i == 0)));
…
primes.forEach((@Must_be_positive var p) -> System.out.print(" " + p)); // '2 3 5 7 11' is displayed...

See also