{"id":40,"date":"2019-01-15T10:54:35","date_gmt":"2019-01-15T10:54:35","guid":{"rendered":"https:\/\/kindsonthegenius.com\/java\/?p=40"},"modified":"2019-03-02T04:35:24","modified_gmt":"2019-03-02T04:35:24","slug":"07java-basic-data-types","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/java\/07java-basic-data-types\/","title":{"rendered":"Java &#8211; Basic Data Types"},"content":{"rendered":"\n<ol class=\"wp-block-list\"><li><a href=\"#t1\">Introduction to Data Types<\/a><\/li><li><a href=\"#t2\">Primitive Data Types<\/a><\/li><li><a href=\"#t3\">Reference Data Types<\/a><\/li><li><a href=\"#t4\">Literals in Java<\/a><\/li><li><a href=\"#t5\">Escape Sequences<\/a><\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong id=\"t1\">1. Introduction&nbsp;to&nbsp;Data&nbsp;Types<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">First of all, you should remember that Java is a statically typed languages. For this reason, variables must be declared before use. Variable declaration allocated memory for the variable.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Amount of space allocated depends of the data type. Therefore, we would examine Java basic data types. We have two types of data in Java:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Primitive Data Type<\/li><li>Reference Data Type<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong id=\"t2\">2. Primitive Data Type<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">We have eight of them. They are byte, short, long, float, double, char and boolean.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>byte<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>8 bit signed integer<\/li><li>Ranges from -128 to 127<\/li><li>Default value of 0<\/li><li>For example: byte x = 20, byte y = -5<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>short<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>16 bit signed integer<\/li><li>Ranges from -32,768 to 32,767<\/li><li>Default value of 0<\/li><li>For example: short a = 20,000, short b = -12,434<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>int<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>32 singed integer<\/li><li>Ranges from -2,147,483,648 to 2,147,483,648 <\/li><li>Default value of 0<\/li><li>For example: int i = 300000, int j = -574800<\/li><li>Generally used for integers <\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>long<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>64 bit signed integer values<\/li><li>Ranges from -2<sup>63<\/sup> to 2<sup>63<\/sup> &#8211; 1<\/li><li>Used when very large value is needed<\/li><li>Default value of 0L<\/li><li>For example: long x = 200000L; long y = -100000L<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>float<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>32 bit floating point numbers<\/li><li>Default value of 0.0f<\/li><li>Not generally used<\/li><li>Lacks precision relative to double<\/li><li>For example: float fnum = 0.01f; float fnum2 = 21.840f<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>double<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>64 bit floating point numbers<\/li><li>Has better precision than float<\/li><li>A preferred type for decimals<\/li><li>Default value of 0.0d<\/li><li>For example: double mydouble = 47.02984; double d = 0.034d<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>char<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>A single 16 bit unicode character<\/li><li>Ranges from &#8216;\\u0000&#8217; (0) to &#8216;\\uffff&#8217; (65,535)<\/li><li>Stores a single character<\/li><li>For exampe: char a = &#8216;a&#8217;; char grade = &#8216;B&#8217;<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>boolean<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>A single bit data type<\/li><li>Has only two values: true or false (1 and 0)<\/li><li>Default value of false<\/li><li>For example: boolean tall = true<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong id=\"t3\">3. Reference Data Types<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Reference types are used to access objects. Therefore, they are created using constructors. Note the following about reference types:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Class objects are declared as reference types<\/li><li>Arrays are also reference types<\/li><li>Default value of null<\/li><li>Reference variable can be used to refer to any object of the declared type<\/li><li>For example: Vehicle vehicle = new Vehicle (&#8220;Camry&#8221;);<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong id=\"t4\">4. Literals in Java<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><!-- HTML generated using hilite.me --><div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\">\t\t<span style=\"color: #333399; font-weight: bold\">char<\/span> grade <span style=\"color: #333333\">=<\/span> <span style=\"color: #0044DD\">'A'<\/span><span style=\"color: #333333\">;<\/span>\n\t\t<span style=\"color: #333399; font-weight: bold\">byte<\/span> b <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">20<\/span><span style=\"color: #333333\">;<\/span>\n\t\t<span style=\"color: #333399; font-weight: bold\">boolean<\/span> rich <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">true<\/span><span style=\"color: #333333\">;<\/span>\n<\/pre><\/div><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here, &#8216;A&#8217;, 20 and true are all literals.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Furthermore, byte, int, long and short can be express in decimal,  octal or hexadecimal<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>decimal is base 10<\/li><li>octal is base 8<\/li><li>hexadecimal is base 16<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The following prefix rules applies:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>hexadecimal literals are prefixed with 0x<\/li><li>octal literals are prefixed with 0<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Example is given below:<\/p>\n\n\n\n<p><div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\"> <span style=\"color: #333399; font-weight: bold\">int<\/span> decimalval <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">200<\/span><span style=\"color: #333333\">;<\/span> <\/pre><\/div><\/p>\n\n\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333399; font-weight: bold;\">int<\/span> octalval <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0200<\/span><span style=\"color: #333333;\">;<\/span> <\/pre>\n<\/div>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333399; font-weight: bold;\">int<\/span> hexval <span style=\"color: #333333;\">=<\/span> <span style=\"color: #005588; font-weight: bold;\">0x128F<\/span><span style=\"color: #333333;\">;<\/span> <\/pre>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>String Literals<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">String literals are enclosed in double quotes. For example:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>&#8220;The Tech Pro<\/li><li>&#8220;Java&#8221;<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Also note that string literals can be assigned to string variables. We would learn this in subsequent tutorials.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong id=\"t5\">5. Escape Sequences&nbsp;&nbsp;in&nbsp;Java<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Used to control display of strings. Note list of escape sequences below<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><th>Sequence<\/th><th>What it Represents<\/th><\/tr><tr><td>\\n<\/td><td>New line (0x0a)<\/td><\/tr><tr><td>\\r<\/td><td>Carriage return (0x0d)<\/td><\/tr><tr><td>\\f<\/td><td>Formfeed (0x0c)<\/td><\/tr><tr><td>\\b<\/td><td>Backspace (0x08)<\/td><\/tr><tr><td>\\s<\/td><td>Space  (0x20)<\/td><\/tr><tr><td>\\t<\/td><td>tab<\/td><\/tr><tr><td>\\&#8221;<\/td><td>Double quotes<\/td><\/tr><tr><td>\\&#8217;<\/td><td>Single quote<\/td><\/tr><tr><td>\\\\<\/td><td>backslash<\/td><\/tr><tr><td>\\ddd<\/td><td>Octal character (ddd)<\/td><\/tr><tr><td>\\uxxxx<\/td><td>Hex UNICODE character (xxxx)<\/td><\/tr><\/tbody><\/table>\n","protected":false},"excerpt":{"rendered":"<p>Introduction to Data Types Primitive Data Types Reference Data Types Literals in Java Escape Sequences 1. Introduction&nbsp;to&nbsp;Data&nbsp;Types First of all, you should remember that Java &hellip; <\/p>\n","protected":false},"author":395,"featured_media":42,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-40","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorial"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/40","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/users\/395"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":2,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/40\/revisions"}],"predecessor-version":[{"id":43,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/40\/revisions\/43"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/media\/42"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/categories?post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/tags?post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}