Skip to main content

Command Palette

Search for a command to run...

Java (Data Types and type casting)

Updated
•3 min read•View as Markdown
Java (Data Types and type casting)

Data Types

As explained in the previous chapter, a variable in Java must be a specified data type:

Data types are divided into two groups:

  • Primitive data types - includes byte, short, int, long, float, double, boolean and char

Non-primitive data types - such as String, Arrays and Classes (you will learn more about these in a later chapter)

Size of data types

Data types

Size

byte

1 byte (-128 to 127)

short

2 bytes (-32768 to 32767)

char

2 bytes

boolean

1 byte (true or false)

int

4 bytes (-2B to + 2B) (no decimal)

long

8 bytes

float

4 bytes

double

8 bytes

Note: 1 byte = 8 bits.

Non-Primitive Data Types

Non-primitive data types are called reference types because they refer to objects. Examples of non-primitive types are Strings, Arrays, Classes, Interface, etc. You will learn more about these in later chapters.

The main difference between primitive and non-primitive data types are:

  • Primitive types are predefined (already defined) in Java. Non-primitive types are created by the programmer and is not defined by Java (except for String).

  • A primitive type has always a value, while non-primitive types can be null.

  • A primitive type starts with a lowercase letter, while non-primitive types starts with an uppercase letter.

  • Non-primitive types can be used to call methods to perform certain operations, while primitive types cannot.

Comments

Comments can be used to explain Java code and to make it more readable. It can also be used to prevent execution when testing alternative code.

There are two types of comments:

  • Single line comments: It starts with //.

  • Multi-line comments: It starts with /* and ends with */.

Java Type Casting

Type casting is when you assign a value of one primitive data type to another type.

In Java, there are two types of casting:

  • Widening Casting (automatically) - converting a smaller type to a larger type size
    byte -> short -> char -> int -> long -> float -> double

      public class JavaBasics {
        public static void main(String[] args) {
          int a = 9;
          double b = a; // Automatic casting: int to double
    
          System.out.println(a);      // Outputs 9
          System.out.println(b);   // Outputs 9.0
        }
      }
    
  • Narrowing Casting (manually) - converting a larger type to a smaller size type
    double -> float -> long -> int -> char -> short -> byte

      public class JavaBasics {
        public static void main(String[] args) {
          double a = 9.78;
          int b = (int) a; // Manual casting: double to int
    
          System.out.println(a);   // Outputs 9.78
          System.out.println(b);      // Outputs 9
        }
      }
    

    Type promotion in Java

    • Java automatically promotes each byte, short, or char operand to int when evaluating an expression.

    • If one operand is long, float, or double the whole expression is promoted to long, float, or double respectively.

        char a = 'a';
        char b = 'b';
        System.out.println((int)(b));       //print the integer value of alphabet a i.e.
        System.out.println((int)(a));       //print the integer value of alphabet b i.e.           
        System.out.println(b-a);            // it will solve the expression based on the integer value of a and b.
        System.out.println(a);              // it will the value stored in a as type promotion is only applicable in expressions.