|
| 1 | +--- |
| 2 | +title: Java Data Types |
| 3 | +description: In this tutorial, we will learn about all 8 primitive data types in Java with the help of examples. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +import { Heading } from '@/components/Heading' |
| 8 | +import { List, ListItemGood } from '@/components/List' |
| 9 | +import Link from 'next/link' |
| 10 | +import { TipInfo } from '@/components/Tip' |
| 11 | + |
| 12 | +## Java Data Types |
| 13 | + |
| 14 | +As the name suggests, data types specify the type of data that can be stored inside [variables in Java](/docs/variables-and-literals). |
| 15 | + |
| 16 | +Java is a statically-typed language. This means that all variables must be declared before they can be used. |
| 17 | + |
| 18 | +```java |
| 19 | +int speed; |
| 20 | +``` |
| 21 | + |
| 22 | +Here, `speed` is a variable, and the data type of the variable is `int`. |
| 23 | + |
| 24 | +The `int` data type determines that the `speed` variable can only contain integers. |
| 25 | + |
| 26 | +There are 8 data types predefined in Java programming language, known as primitive data types. |
| 27 | + |
| 28 | +<TipInfo> |
| 29 | + |
| 30 | + **Note :** In addition to primitive data types, there are also referenced |
| 31 | + types (object type). |
| 32 | + |
| 33 | +</TipInfo> |
| 34 | + |
| 35 | +## 8 Primitive Data Types |
| 36 | + |
| 37 | + ### 1. boolean type |
| 38 | + |
| 39 | + - The `boolean` data type has two possible values, either true or false. |
| 40 | + - They are usually used for **true/false** conditions. |
| 41 | + - **Default value : `false`**. |
| 42 | + |
| 43 | + #### Example 1: Java `boolean` data type |
| 44 | + |
| 45 | + ```java |
| 46 | + class Main { |
| 47 | + public static void main(String[] args) { |
| 48 | + boolean flag = true; |
| 49 | + System.out.println(flag); // prints true |
| 50 | + } |
| 51 | + } |
| 52 | + ``` |
| 53 | + |
| 54 | +### 2. byte type |
| 55 | + |
| 56 | + - The `byte` data type can have values from **-128** to **127** (8-bit signed two's complement integer). |
| 57 | + - 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. |
| 58 | + - **Default value : 0** |
| 59 | + |
| 60 | + #### Example 2: Java `byte` data type |
| 61 | + |
| 62 | + ```java |
| 63 | + class Main { |
| 64 | + public static void main(String[] args) { |
| 65 | + byte range; |
| 66 | + range = 124; |
| 67 | + System.out.println(range); // prints 124 |
| 68 | + } |
| 69 | + } |
| 70 | + ``` |
| 71 | + |
| 72 | +### 3. short type |
| 73 | + |
| 74 | + - The `short` data type in Java can have values from **-32768** to **32767** (16-bit signed two's complement integer). |
| 75 | + - 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`). |
| 76 | + - **Default value : 0** |
| 77 | + |
| 78 | + #### Example 3: Java `short` data type |
| 79 | + |
| 80 | + ```java |
| 81 | + class Main { |
| 82 | + public static void main(String[] args) { |
| 83 | + short temperature; |
| 84 | + temperature = -200; |
| 85 | + System.out.println(temperature); // prints -200 |
| 86 | + } |
| 87 | + } |
| 88 | + ``` |
| 89 | + |
| 90 | +### 4. int type |
| 91 | + |
| 92 | + - The int data type can have values from **-2<sup>31</sup>** to **2<sup>31</sup> -1** (32-bit signed two's complement integer). |
| 93 | + - 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 2<sup>32-1</sup>. To learn more, visit [How to use the unsigned integer in java 8?](http://stackoverflow.com/questions/25556017/how-to-use-the-unsigned-integer-in-java-8) |
| 94 | + - **Default value : 0** |
| 95 | + |
| 96 | + #### Example 4: Java `int` data type |
| 97 | + |
| 98 | + ```java |
| 99 | + class Main { |
| 100 | + public static void main(String[] args) { |
| 101 | + int range = -4250000; |
| 102 | + System.out.println(range); // print -4250000 |
| 103 | + } |
| 104 | + } |
| 105 | + ``` |
| 106 | + |
| 107 | +### 5. long type |
| 108 | + |
| 109 | + - The `long` data type can have values from **-2<sup>63</sup>** to **2<sup>63</sup> -1** (64-bit signed two's complement integer). |
| 110 | + - 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 2<sup>64</sup> -1. |
| 111 | + - **Default value : 0** |
| 112 | + |
| 113 | + #### Example 5: Java `long` data type |
| 114 | + |
| 115 | + ```java |
| 116 | + class LongExample { |
| 117 | + public static void main(String[] args) { |
| 118 | + long range = -42332200000L; |
| 119 | + System.out.println(range); // prints -42332200000 |
| 120 | + } |
| 121 | + } |
| 122 | + ``` |
| 123 | + |
| 124 | + Notice, the use of `L` at the end of `-42332200000`. This represents that it's an integral literal of the `long` type. You will learn about integral literals later in this article. |
| 125 | + |
| 126 | +### 6. double type |
| 127 | + |
| 128 | + - The `double` data type is a double-precision 64-bit floating-point. |
| 129 | + - It should never be used for precise values such as currency. |
| 130 | + - **Default value : 0.0 (0.0d)** |
| 131 | + |
| 132 | + #### Example 6: Java `double` data type |
| 133 | + |
| 134 | + ```java |
| 135 | + class Main { |
| 136 | + public static void main(String[] args) { |
| 137 | + double number = -42.3; |
| 138 | + System.out.println(number); // prints -42.3 |
| 139 | + } |
| 140 | + } |
| 141 | + ``` |
| 142 | + |
| 143 | +### 7. float type |
| 144 | + |
| 145 | + - The `float` data type is a single-precision 32-bit floating-point.Learn more about [single-precision and double-precision floating-point](http://stackoverflow.com/questions/801117/whats-the-difference-between-a-single-precision-and-double-precision-floating-p) if you are interested. |
| 146 | + - It should never be used for precise values such as currency. |
| 147 | + - **Default value : 0.0 (0.0f)** |
| 148 | + |
| 149 | + #### Example 7: Java `float` data type |
| 150 | + |
| 151 | + ```java highlight=3 |
| 152 | + class Main { |
| 153 | + public static void main(String[] args) { |
| 154 | + float number = -42.3f; |
| 155 | + System.out.println(number); // prints -42.3 |
| 156 | + } |
| 157 | + } |
| 158 | + ``` |
| 159 | + |
| 160 | + Notice that, we have used `-42.3f` instead of `-42.3` in the above program. It's because `-42.3` is a `double` literal. |
| 161 | + To tell the compiler to treat `-42.3` as `float` rather than `double`, you need to use `f` or `F`. |
| 162 | + If you want to know about single-precision and double-precision, visit Java single-precision and double-precision floating-point. |
| 163 | + |
| 164 | +### 8. char type |
| 165 | + |
| 166 | + - It's a 16-bit Unicode character. |
| 167 | + - The minimum value of the `char` data type is `'\u0000'` (0) and the maximum value of the is `'\uffff'`. |
| 168 | + - **Default value : '\u0000'** |
| 169 | + |
| 170 | + #### Example 8: Java `char` data type |
| 171 | + |
| 172 | + ```java |
| 173 | + class Main { |
| 174 | + public static void main(String[] args) { |
| 175 | + char letter = '\u0051'; |
| 176 | + System.out.println(letter); // prints Q |
| 177 | + } |
| 178 | + } |
| 179 | + ``` |
| 180 | + |
| 181 | + Here, the Unicode value of Q is `\u0051`. Hence, we get Q as the output. |
| 182 | + |
| 183 | + Here is another example: |
| 184 | + |
| 185 | + ```java |
| 186 | + class Main { |
| 187 | + public static void main(String[] args) { |
| 188 | + char letter1 = '9'; |
| 189 | + System.out.println(letter1); // prints 9 |
| 190 | + char letter2 = 65; |
| 191 | + System.out.println(letter2); // prints A |
| 192 | + |
| 193 | + } |
| 194 | + } |
| 195 | + ``` |
| 196 | + |
| 197 | +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). |
| 198 | + |
| 199 | +Hence, A is printed to the output. It is because Java treats characters as integral types and the ASCII value of A is 65. To learn more about ASCII, visit What is ASCII Code?. |
| 200 | + |
| 201 | +## String type |
| 202 | +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, |
| 203 | + |
| 204 | +```java |
| 205 | +String myString = "Java Programming"; |
| 206 | +``` |
| 207 | + |
| 208 | +Here, myString is an object of the String class. To learn more, visit [Java Strings](/docs/string). |
0 commit comments