{"id":1150,"date":"2022-02-24T18:48:21","date_gmt":"2022-02-24T18:48:21","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1150"},"modified":"2022-02-24T18:48:21","modified_gmt":"2022-02-24T18:48:21","slug":"data-types","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/24\/data-types\/","title":{"rendered":"Data Types"},"content":{"rendered":"\n<p>In this tutorial, we will learn about all 8 primitive data types in Java with the help of examples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"data-type\">Java Data Types<\/h2>\n\n\n\n<p>As the name suggests, data types specify the type of data that can be stored inside\u00a0variables in Java.<\/p>\n\n\n\n<p>Java is a statically-typed language. This means that all variables must be declared before they can be used.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int speed;<\/code><\/pre>\n\n\n\n<p>Here,&nbsp;<var>speed<\/var>&nbsp;is a variable, and the data type of the variable is&nbsp;<code>int<\/code>.<\/p>\n\n\n\n<p>The&nbsp;<var>int<\/var>&nbsp;data type determines that the&nbsp;<var>speed<\/var>&nbsp;variable can only contain integers.<\/p>\n\n\n\n<p>There are 8 data types predefined in Java, known as primitive data types.<\/p>\n\n\n\n<p><strong>Note<\/strong>: In addition to primitive data types, there are also referenced types (object type).<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">8 Primitive Data Types<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"boolean\">1. boolean type<\/h3>\n\n\n\n<ul><li>The&nbsp;<code>boolean<\/code>&nbsp;data type has two possible values, either&nbsp;<code>true<\/code>&nbsp;or&nbsp;<code>false<\/code>.<\/li><li>Default value:&nbsp;<code>false<\/code>.<\/li><li>They are usually used for&nbsp;<strong>true\/false<\/strong>&nbsp;conditions.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Example 1: Java boolean data type<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n  public static void main(String&#91;] args) {\n    \t\n    boolean flag = true;\n    System.out.println(flag);    \/\/ prints true\n  }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"byte\">2. byte type<\/h3>\n\n\n\n<ul><li>The&nbsp;<code>byte<\/code>&nbsp;data type can have values from&nbsp;<strong>-128<\/strong>&nbsp;to&nbsp;<strong>127<\/strong>&nbsp;(8-bit signed two&#8217;s complement integer).<\/li><li>If it&#8217;s certain that the value of a variable will be within -128 to 127, then it is used instead of int to save memory.<\/li><li>Default value: 0<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Example 2: Java byte data type<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n  public static void main(String&#91;] args) {\n\n    byte range;\n    range = 124;\n    System.out.println(range);    \/\/ prints 124\n  }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"short\">3. short type<\/h3>\n\n\n\n<ul><li>The&nbsp;<code>short<\/code>&nbsp;data type in Java can have values from&nbsp;<strong>-32768<\/strong>&nbsp;to&nbsp;<strong>32767<\/strong>&nbsp;(16-bit signed two&#8217;s complement integer).<\/li><li>If it&#8217;s certain that the value of a variable will be within -32768 and 32767, then it is used instead of other integer data types (<code>int<\/code>,&nbsp;<code>long<\/code>).<\/li><li>Default value: 0<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Example 3: Java short data type<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n  public static void main(String&#91;] args) {\n    \t\n    short temperature;\n    temperature = -200;\n    System.out.println(temperature);  \/\/ prints -200\n  }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"int\">4. int type<\/h3>\n\n\n\n<ul><li>The\u00a0<code>int<\/code>\u00a0data type can have values from\u00a0<strong>-2<sup>31<\/sup><\/strong>\u00a0to\u00a0<strong>2<sup>31<\/sup>-1<\/strong>\u00a0(32-bit signed two&#8217;s complement integer).<\/li><li>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<\/sup>-1. <\/li><li>Default value: 0<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Example 4: Java int data type<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n  public static void main(String&#91;] args) {\n    \t\n    int range = -4250000;\n    System.out.println(range);  \/\/ print -4250000\n  }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"long\">5. long type<\/h3>\n\n\n\n<ul><li>The&nbsp;<code>long<\/code>&nbsp;data type can have values from&nbsp;<strong>-2<sup>63<\/sup><\/strong>&nbsp;to&nbsp;<strong>2<sup>63<\/sup>-1<\/strong>&nbsp;(64-bit signed two&#8217;s complement integer).<\/li><li>If you are using Java 8 or later, you can use an unsigned 64-bit integer with a minimum value of&nbsp;<strong>0<\/strong>&nbsp;and a maximum value of&nbsp;<strong>2<sup>64<\/sup>-1<\/strong>.<\/li><li>Default value: 0<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Example 5: Java long data type<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class LongExample {\n  public static void main(String&#91;] args) {\n    \t\n    long range = -42332200000L;\n    System.out.println(range);    \/\/ prints -42332200000\n  }\n}<\/code><\/pre>\n\n\n\n<p>Notice, the use of&nbsp;<code>L<\/code>&nbsp;at the end of&nbsp;<code>-42332200000<\/code>. This represents that it&#8217;s an integer&nbsp;of the&nbsp;<code>long<\/code>&nbsp;type.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"double\">6. double type<\/h3>\n\n\n\n<ul><li>The&nbsp;<code>double<\/code>&nbsp;data type is a double-precision 64-bit floating-point.<\/li><li>It should never be used for precise values such as currency.<\/li><li>Default value: 0.0 (0.0d)<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Example 6: Java double data type<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n  public static void main(String&#91;] args) {\n    \t\n    double number = -42.3;\n    System.out.println(number);  \/\/ prints -42.3\n  }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"float\">7. float type<\/h3>\n\n\n\n<ul><li>The\u00a0<code>float<\/code>\u00a0data type is a single-precision 32-bit floating-point. Learn more about\u00a0single-precision and double-precision floating-point\u00a0if you are interested.<\/li><li>It should never be used for precise values such as currency.<\/li><li>Default value: 0.0 (0.0f)<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Example 7: Java float data type<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n  public static void main(String&#91;] args) {\n    \t\n    float number = -42.3f;\n    System.out.println(number);  \/\/ prints -42.3\n  }\n}<\/code><\/pre>\n\n\n\n<p>Notice that we have used&nbsp;<code>-42.3f<\/code>&nbsp;instead of&nbsp;<code>-42.3<\/code>in the above program. It&#8217;s because&nbsp;<code>-42.3<\/code>&nbsp;is a&nbsp;<code>double<\/code>&nbsp;literal.<\/p>\n\n\n\n<p>To tell the compiler to treat&nbsp;<code>-42.3<\/code>&nbsp;as&nbsp;<code>float<\/code>&nbsp;rather than&nbsp;<code>double<\/code>, you need to use&nbsp;<var>f<\/var>&nbsp;or&nbsp;<var>F<\/var>.<\/p>\n\n\n\n<p>If you want to know about single-precision and double-precision, visit\u00a0Java single-precision and double-precision floating-point.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"char\">8. char type<\/h3>\n\n\n\n<ul><li>It&#8217;s a 16-bit Unicode character.<\/li><li>The minimum value of the char data type is&nbsp;<code>'\\u0000'<\/code>&nbsp;(0) and the maximum value of the is&nbsp;<code>'\\uffff'<\/code>.<\/li><li>Default value:&nbsp;<code>'\\u0000'<\/code><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Example 8: Java char data type<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n  public static void main(String&#91;] args) {\n    \t\n    char letter = '\\u0051';\n    System.out.println(letter);  \/\/ prints Q\n  }\n}<\/code><\/pre>\n\n\n\n<p>Here, the Unicode value of&nbsp;<code>Q<\/code>&nbsp;is&nbsp;<strong>\\u0051<\/strong>. Hence, we get&nbsp;<code>Q<\/code>&nbsp;as the output.<\/p>\n\n\n\n<p>Here is another example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n  public static void main(String&#91;] args) {\n    \t\n    char letter1 = '9';\n    System.out.println(letter1);  \/\/ prints 9\n    \t\n    char letter2 = 65;\n    System.out.println(letter2);  \/\/ prints A\n\n  }\n}<\/code><\/pre>\n\n\n\n<p>Here, we have assigned&nbsp;<code>9<\/code>&nbsp;as a character (specified by single quotes) to the&nbsp;<var>letter1<\/var>&nbsp;variable. However, the&nbsp;<var>letter2<\/var>&nbsp;variable is assigned&nbsp;<code>65<\/code>&nbsp;as an integer number (no single quotes).<\/p>\n\n\n\n<p>Hence,\u00a0<code>A<\/code>\u00a0is printed to the output. It is because Java treats characters as an integer and the ASCII value of\u00a0<code>A<\/code>\u00a0is 65. To learn more about ASCII, visit\u00a0What is ASCII Code?.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">String type<\/h3>\n\n\n\n<p>Java also provides support for character strings via&nbsp;<code>java.lang.String<\/code>&nbsp;class. Strings in Java are not primitive types. Instead, they are objects. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String myString = \"Java Programming\";<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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\u00a0variables in Java. Java is a statically-typed language. This means that all variables must be declared before they [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[290],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1150"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1150"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1150\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}