Skip to main content

Command Palette

Search for a command to run...

Datatypes in Java

Published
•2 min read•View as Markdown

There are two types :

1) Primitve Datatypes

2) Non-Primitive Datatypes

Let's Discuss Primitive ones in this one :

1. Boolean type

In this datatype it's all about true and false these are used when you want to see whether the given condition is true or false.

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

boolean flag = true;

System.out.println(flag);

}

}

It will print true.

2. Byte type

  • The byte data type can have values from -128 to 127 (8-bit signed two's complement integer).

  • If it's certain that the value of a variable will be within -128 to 127, then it is used instead of int to save memory.

  • Default value: 0

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

    byte range; range = 124;

  • System.out.println(range);

  • }

  • }

    It will print 124.

  • 3. short type

    • The short data type in Java can have values from -32768 to 32767 (16-bit signed two's complement integer).

    • If it's certain that the value of a variable will be within -32768 and 32767, then it is used instead of other integer data types (int, long).

    • Default value: 0

4. int type

  • The int data type can have values from -231 to 231-1 (32-bit signed two's complement integer).

  • If you are using Java 8 or later, you can use an unsigned 32-bit integer. This will have a minimum value of 0 and a maximum value of 232-1.

  • Default value: 0

  • 5. long type

    • The long data type can have values from -263 to 263-1 (64-bit signed two's complement integer).

    • If you are using Java 8 or later, you can use an unsigned 64-bit integer with a minimum value of 0 and a maximum value of 264-1.

    • Default value: 0

We will see more basics of java in next blog till then HAPPY READING GUYS!!!! follow me on instagram and linkedin.