Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Java Primitive Data Types: A Complete Guide with Examples

Java Data Types (Primitive)

In this tutorial, we will learn about all 8 primitive data types in Java with the help of examples.

Java Data Types

As the name suggests, data types specify the type of data that can be stored inside variables in Java.

Java is a statically-typed language. This means that all variables must be declared before they can be used.

int speed;

Here, speed is a variable, and the data type of the variable is int.

The int data type determines that the speed variable can only contain integers.

There are 8 data types predefined in Java, known as primitive data types.

Note: In addition to primitive data types, there are also referenced types (object type).


8 Primitive Data Types

1. boolean type

Example 1: Java boolean data type

class Main {
  public static void main(String[] args) {
    	
    boolean flag = true;
    System.out.println(flag);    // prints true
  }
}

2. byte type

Example 2: Java byte data type

class Main {
  public static void main(String[] args) {

    byte range;
    range = 124;
    System.out.println(range);    // prints 124
  }
}

3. short type

Example 3: Java short data type

class Main {
  public static void main(String[] args) {
    	
    short temperature;
    temperature = -200;
    System.out.println(temperature);  // prints -200
  }
}

4. int type

Example 4: Java int data type

class Main {
  public static void main(String[] args) {
    	
    int range = -4250000;
    System.out.println(range);  // print -4250000
  }
}

5. long type

Example 5: Java long data type

class LongExample {
  public static void main(String[] args) {
    	
    long range = -42332200000L;
    System.out.println(range);    // prints -42332200000
  }
}

Notice, the use of L at the end of -42332200000. This represents that it's an integer of the long type.


6. double type

Example 6: Java double data type

class Main {
  public static void main(String[] args) {
    	
    double number = -42.3;
    System.out.println(number);  // prints -42.3
  }
}

7. float type

Example 7: Java float data type

class Main {
  public static void main(String[] args) {
    	
    float number = -42.3f;
    System.out.println(number);  // prints -42.3
  }
}

Notice that we have used -42.3f instead of -42.3in the above program. It's because -42.3 is a double literal.

To tell the compiler to treat -42.3 as float rather than double, you need to use f or F.

If you want to know about single-precision and double-precision, visit Java single-precision and double-precision floating-point.


8. char type

Example 8: Java char data type

class Main {
  public static void main(String[] args) {
    	
    char letter = '\u0051';
    System.out.println(letter);  // prints Q
  }
}

Here, the Unicode value of Q is \u0051. Hence, we get Q as the output.

Here is another example:

class Main {
  public static void main(String[] args) {
    	
    char letter1 = '9';
    System.out.println(letter1);  // prints 9
    	
    char letter2 = 65;
    System.out.println(letter2);  // prints A

  }
}

Here, we have assigned 9 as a character (specified by single quotes) to the letter1 variable. However, the letter2 variable is assigned 65 as an integer number (no single quotes).

Hence, A is printed to the output. It is because Java treats characters as an integer and the ASCII value of A is 65. To learn more about ASCII, visit What is ASCII Code?.


String type

Java also provides support for character strings via java.lang.String class. Strings in Java are not primitive types. Instead, they are objects. For example,

String myString = "Java Programming";

Here, myString is an object of the String class. To learn more, visit Java Strings.


Java

  1. Master C# Variables & Primitive Data Types: A Complete Guide
  2. Mastering Python Data Types: A Practical Guide
  3. Mastering Java Encapsulation: A Practical Guide to Data Hiding
  4. Java Annotation Types: A Comprehensive Guide to Predefined, Custom, and Meta Annotations
  5. Mastering Java Type Casting: From Widening to Narrowing and Beyond
  6. Java Variables and Data Types – A Comprehensive Guide with Examples
  7. Java Primitive Data Types: A Clear Guide
  8. Understanding Java Variable Types: A Comprehensive Guide
  9. Java Data Structures: Exploring Core Classes & Collections Framework
  10. Comprehensive Guide to C# Data Types: Value, Reference, and Pointer