Sage-Code Laboratory
index<--

Java Types

Java is a statically typed language. Java type system involve primitive types and user defined types that are actually classes. In this article we will focus on primitive types: You will learn how to define variables and how to operate.

Primitive Types

Java programming language supports eight primitive data types. A primitive type is predefined by the language and is named by a reserved keyword. Each primitive type have a fixed size in memory. The eight primitive data types supported by the Java programming language are:

type default size minimum maximum
byte 0 8-bit -128 127
short 0 16-bit -32768 32767
int 0 32-bit -2**31 2**31-1
long 0L 64-bit -2**63 2**63-1
float 0f 32-bit 1/10**6 10**6
double 0d 64-bit 1/10**15 10**15
boolean false 8-bit false true
char '\u0000' 16-bit '\u0000' '\uFFFF'
Default Values:  Usually you specify the initial value when you declare them. If you don't initialize, fields (properties) of a class get default values but local variables get Null value instead. Don't let JVM lead you, always use initializers.

Constant Literals

You can specify the initial value for fields (properties) or local variables by using constant literals. In addition, constant literals, can be also used in expressions. Let's investigate first the most common literals:

Examples:

/* initialize variables using literals */
boolean result = true;
char capitalC = 'C';
byte b = 100;
short s = 10000;
int i = 100000;

// hexadecimals and binary prefix: 0x and 0b
int hexVal = 0x1abcdef;
int binVal = 0b11101001;

// long integer literals
long x1 = 1000l; // don't use lowercase l
long x2 = 1000L; // use uppercase L

// numeric literal with underscore "_"
long y = 1000_000_000_000L;

// double precision literals
double d1 = 123.45;
double d2 = 10234e2;

//float single precision literal
float f1  = 123.45f;

Type Casting

Sometimes you need to convert one variable from one type to another type that can be more or less compatible. Type casting is when you assign a value of one primitive data type to another type. There is a special notation for explicit casting in Java, and it looks like this:

(TypeName)Expression

Using type casting is very important skill especially in object oriented programming we will study later. For primitive data types, there are two techniques for conversion. First is the transparent, implicit conversion and the second is explicit conversion.

1. Widening Casting (transparent) - converting a smaller type to a larger type size
byte -> short -> char -> int -> long -> float -> double
2. Narrowing Casting (explicit) - converting a larger type to a smaller size type
double -> float -> long -> int -> char -> short -> byte

Example:

Next example demonstrate both: implicit & explicit casting.

public class Main {
  public static void main(String[] args) {
    /* implicit casting initialization */
    long   x = 10L;  // No casting involved
    double y = x;    // Automatic casting

    // test automatic casting
    System.out.println(x);   // Outputs 10
    System.out.println(y);   // Outputs 10.0

    /* explicit casting initialization*/
    double demoDob = 30.42d;
    int    demoInt = (int)demoDob; // Explicit casting

    // test explicit casting
    System.out.println(demoDob);   // Outputs 30.42
    System.out.println(demoInt);   // Outputs 30
  }
}

Read next: Strings