{"id":2080,"date":"2020-03-06T18:09:41","date_gmt":"2020-03-06T18:09:41","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=2080"},"modified":"2020-03-10T13:07:41","modified_gmt":"2020-03-10T13:07:41","slug":"java-variables-and-data-types","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/java-variables-and-data-types\/","title":{"rendered":"Java Variables and Data Types"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">Variable is a name associated with a piece of memory that stores data and whose associated value may be changed\u00a0 To declare a variable you need to write the <a href=\"https:\/\/www.h2kinfosys.com\/blog\/what-is-java\/\">Java <\/a>variable type with giving it a name.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">For example, let&#8217;s declare two variables. One is named\u00a0 <\/span><i><span style=\"font-weight: 400;\">personName<\/span><\/i><span style=\"font-weight: 400;\"> and has type String, other is named <\/span><i><span style=\"font-weight: 400;\">personAge<\/span><\/i><span style=\"font-weight: 400;\"> and is of type int.<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">String personName;<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">int personAge;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">Now that we\u2019ve declared a variable, we can give it a value. This is called <\/span><b>initializing<\/b><span style=\"font-weight: 400;\"> a variable. To initialize a variable, you just type the variable name followed by an equal sign, followed by the desired value:<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">personName = &#8220;Denis&#8221;;<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">personAge = 30;<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">Since you often want to initialize a variable right away, you can do so in the same statement as the declaration. For example, here we merge the previous declarations and initializations into more concise code:<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">String personName = &#8220;Denis&#8221;;<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">int personAge = 30;<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">In the picture below you can find basic terms that developers use for variables:<\/span><\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-2081 size-full\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_1-1.png\" alt=\"Java Variables and Data Types\" width=\"440\" height=\"238\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_1-1.png 440w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_1-1-300x162.png 300w\" sizes=\"(max-width: 440px) 100vw, 440px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">You can also declare and initialize multiple variables in the same statement. How many\u00a0<\/span><span style=\"font-weight: 400;\">variables do you think are declared and initialized in the following two lines?<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">String str1, str2;<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">String str3 = &#8220;yes&#8221;, str4 = &#8220;no&#8221;;<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">We declared 4 String variables were declared: str1, str2, str3, and str4. You can declare many variables in the same declaration in case they have the same type. You can also initialize any\u00a0<\/span><span style=\"font-weight: 400;\">or all of those values inline. In the previous example, we have two initialized variables: str3\u00a0<\/span><span style=\"font-weight: 400;\">and str4. The other two variables remain declared but not initialized.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Pay attention to tricky things! This is where it gets tricky.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Let&#8217;s try one more example, how many variables do you think are declared and initialized in this code?<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">int i1, i2, i3 = 0;<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">As you should expect, 3 variables were declared: i1, i2, and i3. If you look with the attention you will understand that only one of those values i3 was initialized. The other two are declared but not initialized.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">That\u2019s the trick. Each snippet separated by a comma is a little declaration of its own. The\u00a0<\/span><span style=\"font-weight: 400;\">initialization of i3 only applies to i3. It doesn\u2019t have anything to do with i1 or i2 despite\u00a0<\/span><span style=\"font-weight: 400;\">being in the same statement.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Look at this line:<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">int num, String value; \/\/ DOES NOT COMPILE<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">This code doesn\u2019t compile because it tries to declare multiple variables of different types\u00a0<\/span><span style=\"font-weight: 400;\">in the same statement. The shortcut to declare multiple variables in the same statement only\u00a0<\/span><span style=\"font-weight: 400;\">works when they share a type.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">To make sure you understand this, see if you can figure out which of the following are\u00a0<\/span><span style=\"font-weight: 400;\">legal declarations:<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">boolean b1, b2;<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">String s1 = &#8220;1&#8221;, s2;<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">double d1, double d2;<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">int i1; int i2;<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">int i3; i4;<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">The first statement declares two variables without initializing them. The\u00a0<\/span><span style=\"font-weight: 400;\">second statement declares two variables and initializes only one of them.\u00a0<\/span><span style=\"font-weight: 400;\">Look at the third statement. It is not legal. Java does not allow you to declare two different types\u00a0<\/span><span style=\"font-weight: 400;\">in the same statement. Variables d1 and d2 are the same types. They are both of type double. Although that\u2019s true, it still isn\u2019t allowed. If you want to declare multiple\u00a0<\/span><span style=\"font-weight: 400;\">variables in the same statement, they must share the same type declaration and not repeat<\/span><\/p>\n<ol>\n<li><span style=\"font-weight: 400;\"> double d1, d2; would have been legal.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">The fourth statement is legal. Although int does appear twice, each one is in a separate\u00a0<\/span><span style=\"font-weight: 400;\">statement. A semicolon (;) separates statements in Java. It just so happens there are two\u00a0<\/span><span style=\"font-weight: 400;\">completely different statements on the same line. The fifth statement is not legal. Again,\u00a0<\/span><span style=\"font-weight: 400;\">we have two completely different statements on the same line. The second one is not a\u00a0<\/span><span style=\"font-weight: 400;\">valid declaration because it omits the type. When you see an oddly placed semicolon on the\u00a0<\/span><span style=\"font-weight: 400;\">exam, pretend the code is on separate lines and think about whether the code compiles that\u00a0<\/span><span style=\"font-weight: 400;\">way. In this case, we have the following:<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">int i1;<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">int i2;<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">int i3;<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">i4;\/\/ DOES NOT COMPILE<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">Looking at the last line on its own, you can easily see that the declaration is invalid.\u00a0<\/span><span style=\"font-weight: 400;\">And yes, the exam really does cram multiple statements onto the same line partly to\u00a0<\/span><span style=\"font-weight: 400;\">try to trick you and partly to fit more code on the screen. In the real world, please limit\u00a0<\/span><span style=\"font-weight: 400;\">yourself to one declaration per statement and line. Your teammates will thank you for the\u00a0<\/span><span style=\"font-weight: 400;\">readable code.<\/span><\/p>\n<p><b><br \/>\nIdentifiers<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Java has rules about identifier names. The same rules for identifiers apply to variables, methods, classes, and fields.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The rules are:<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">The identifier must begin with a letter or the symbol $ or _.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Subsequent characters in the name may also be numbers.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">A reserved word is a keyword that Java has reserved so that you are not allowed to use it. You cannot use the same name as a Java reserved word.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Java is case sensitive, so you can use versions of the keywords that only differ in case.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">These examples are not legal:<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">3DPointIdentifier \/\/ identifiers cannot begin with a number<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">hello@identifier \/\/ @ is not a letter, digit, $ or _<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">*$mycoffee \/\/ * is not a letter, digit, $ or _<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">public \/\/ public is a reserved word<\/span><\/i><span style=\"font-weight: 400;\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">The following examples are legal:<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">okidentifier<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">$OK5Identifier<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">_alsoOK8d2ntifi3r<\/span><\/i><\/p>\n<p><b>There are also code conventions for identifier names:<\/b><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">In Java methods and variables names have to start from a lowercase letter followed by CamelCase.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Class names have to begin with an uppercase letter followed by CamelCase. The compiler uses this symbol $ for some files. Don\u2019t start any identifiers with $.\u00a0<\/span><\/li>\n<\/ul>\n<p><b>Understanding Default Initialization of <a href=\"https:\/\/www.iitworkforce.com\/building-work-experience\/advanced-java\/\" rel=\"nofollow noopener\" target=\"_blank\">Variables<\/a><\/b><\/p>\n<p><span style=\"font-weight: 400;\">Before you start using a variable, it needs a value. Some types of variables are getting value automatically, some require the programmer to specify it.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0A local variable is defined within a method and must be initialized before use. They do not have a default value and contain garbage data.\u00a0 Not local variables are called instance or class variables. Instance variables are also called fields. Class variables can be shared across several objects. Instance and class variables do not require to be initialized. When you declare these variables, they are given a default value.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><b>Data Types<\/b><\/p>\n<p><span style=\"font-weight: 400;\">There are two types of data in Java: primitive types and reference types.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Java has eight built-in data types, referred to as the Java <\/span><b><i>primitive<\/i><\/b><span style=\"font-weight: 400;\"> types. The table below shows the Java primitive types together with their size in bytes and the range of values that each holds.<\/span><\/p>\n<p>&nbsp;<\/p>\n<table style=\"height: 707px;\" width=\"794\">\n<tbody>\n<tr>\n<td><strong>Keyword<\/strong><\/td>\n<td><strong>Type<\/strong><\/td>\n<td><strong>Example<\/strong><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">boolean<\/span><\/td>\n<td><span style=\"font-weight: 400;\">True or false<\/span><\/td>\n<td><span style=\"font-weight: 400;\">true<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">byte\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">8-bit integral value\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">123<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">short\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">16-bit integral value\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">123<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">int\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">32-bit integral value\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">123<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">long\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">64-bit integral value\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">123<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">float\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">32-bit floating-point value\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">123.45f<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">double\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">64-bit floating-point value\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">123.456<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">char\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">16-bit Unicode value\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">&#8216;a&#8217;<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">Here are key points about types:<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">for floating-point values float and double types are used.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">a float type must have the letter f following the number.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">for numbers without decimal points byte, short, int, and long types are used.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Let&#8217;s talk a bit more about numeric primitives. When a number is present in the code, it is called a literal.\u00a0 Java assumes you are defining an int value with a literal. Look at the example below, the number listed is bigger and does not fit in an int type. There is a possibility to get maximum value for an int.<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">long max = 3923656245789; \/\/ DOES NOT COMPILE<\/span><\/i><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Java compiler will complain that the number is out of range. But it is for an int. However, we don\u2019t\u00a0<\/span><span style=\"font-weight: 400;\">have an int. To fix the problem we need to add the character L to the number:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><i><span style=\"font-weight: 400;\">long max = 3123456789L; \/\/ it is a long<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">Also, you could add a lowercase l to the number, but better is to use the uppercase L.\u00a0<\/span><span style=\"font-weight: 400;\">The lowercase l looks similar to the number 1.<\/span><\/p>\n<p><span style=\"font-weight: 400;\"><a href=\"https:\/\/www.h2kinfosys.com\/courses\/java-online-training-course-details\">Primitive Data types<\/a> hold values in the memory where the variable is allocated, references do not hold the value of the object they refer to.\u00a0 <\/span><b><i>Reference type<\/i><\/b> <span style=\"font-weight: 400;\">points to an instance of a class by storing the memory address where the object is located, a concept referred to as a pointer. Unlike other\u00a0<\/span><span style=\"font-weight: 400;\">languages, Java does not allow you to learn what the physical memory address is. You can only use the reference to refer to the object.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Reference types in Java: class types\u00a0(this reference type points to an object of a class), array types (this reference type points to an array), interface types (this reference type points to an object of a class which implements an interface).<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Here are examples that declare and initialize reference types. We declare a reference of type java.util.Date and a reference of type String:<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">java.util.Date today;<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">String greeting;<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">The <\/span><i><span style=\"font-weight: 400;\">today<\/span><\/i><span style=\"font-weight: 400;\"> variable is a reference of type Date and can only point to a Date object. The\u00a0<\/span><span style=\"font-weight: 400;\">greeting variable is a reference that can only point to a String object. A value is assigned\u00a0<\/span><span style=\"font-weight: 400;\">to a reference in one of two ways:<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">A reference can be assigned to another object of the same type.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">A reference can be assigned to a new object using the new keyword.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">For example, the following statements assign these references to new objects:<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">today = new java.util.Date();<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">greeting = &#8220;How are you?&#8221;;<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">Today reference now points to a new Date object in memory, and <\/span><i><span style=\"font-weight: 400;\">today<\/span><\/i><span style=\"font-weight: 400;\"> can be used to access the various fields and methods of this Date object. Similarly, the greeting reference points to a new String object, &#8220;How are you?&#8221;. The String and Date objects do not have names and can be accessed only via their corresponding reference.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Variable is a name associated with a piece of memory that stores data and whose associated value may be changed\u00a0 To declare a variable you need to write the Java variable type with giving it a name.\u00a0 For example, let&#8217;s declare two variables. One is named\u00a0 personName and has type String, other is named personAge [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2104,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,42],"tags":[394,58,395,396,393],"class_list":["post-2080","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","category-java-tutorials","tag-data-types","tag-java","tag-primitive","tag-reference","tag-variables"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/2080","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=2080"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/2080\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/2104"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=2080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=2080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=2080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}