{"id":6466,"date":"2024-04-21T23:20:39","date_gmt":"2024-04-22T06:20:39","guid":{"rendered":"https:\/\/zentut.com\/java-tutorial\/java-var\/"},"modified":"2024-04-21T23:21:03","modified_gmt":"2024-04-22T06:21:03","slug":"java-var","status":"publish","type":"page","link":"https:\/\/www.zentut.com\/java-tutorial\/java-var\/","title":{"rendered":"Java var"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the Java <code>var<\/code> keyword to declare variables and understand how Java infers the type of variables.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the Java var keyword<\/h2>\n\n\n\n<p>Because Java is a statically typed language, every <a href=\"https:\/\/zentut.com\/java-tutorial\/java-variables\/\">variable<\/a> in Java is associated with a data type. The data type determines the kind of values that the variable can store.<\/p>\n\n\n\n<p>It means that when you declare a variable, you need to specify its data type upfront. For example, the following defines a variable count and initializes its value to 0:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">int<\/span> count = <span class=\"hljs-number\">0<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Java 10 introduced a new way to declare a variable using the var keyword. For example, the following declares the <code>count<\/code> variable with type int and initializes its value to 0:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">var<\/span> count = <span class=\"hljs-number\">0<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>But how does Java determine the type of the <code>count<\/code> variable? In this case, Java automatically infers the type of the variable <code>count<\/code> using its initial value. Because the value of the <code>count<\/code> is zero, the <code>count<\/code> variable has the type of <code>int<\/code>.<\/p>\n\n\n\n<p>Since Java uses the initial value to determine the type of variable, you need to initialize the variables declared with the <code>var<\/code> keyword. The following example is not allowed:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">var<\/span> count;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the following example, Java infers the type of the <code>amount<\/code> variable as double because of its initial value <code>0.00<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">var<\/span> amount = <span class=\"hljs-number\">0.00<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>It&#8217;s equivalent to the following:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">double<\/span> amount = <span class=\"hljs-number\">0.00<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The var keyword also allows you to declare multiple variables at once, each variable declaration is separated by a comma (<code>,<\/code>):<\/p>\n\n\n\n<p>For the primitive data types, the <code>var<\/code> keyword may not have benefits. But later, when you learn about classes, you&#8217;ll see how the <code>var<\/code> keyword makes your code more concise. For example, instead of writing the following code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\">StringBuilder builder = <span class=\"hljs-keyword\">new<\/span> StringBuilder();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>you can shorten it down using the <code>var<\/code> keyword:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">var<\/span> builder = <span class=\"hljs-keyword\">new<\/span> StringBuilder();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Java var and compound declaration<\/h2>\n\n\n\n<p>When you use an explicit type, you can define multiple variables using a single statement. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">double<\/span> amount = <span class=\"hljs-number\">98.95<\/span>,\n       salesTax = <span class=\"hljs-number\">0.08<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, we declare two variables amount and <code>salesTax<\/code> with the type double. This variable declaration is also known as a <strong>compound declaration<\/strong>. <\/p>\n\n\n\n<p>Unlike explicit variable declarations, Java doesn&#8217;t allow you to declare multiple variables using the <code>var<\/code> keyword in a single statement.<\/p>\n\n\n\n<p>For example, the following will result in an error: &#8220;&#8216;var&#8217; is not allowed in a compound declaration&#8221;:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">var<\/span> amount = <span class=\"hljs-number\">98.95<\/span>,\n    salesTax = <span class=\"hljs-number\">0.08<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The Java <code>var<\/code> keyword automatically infers the type of a variable using its initial value.<\/li>\n\n\n\n<li>The Java <code>var<\/code> keyword cannot be used in a compound declaration.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to use the Java var keyword to declare variables and understand how Java infers the type of variables.<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":2190,"menu_order":8,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-6466","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages\/6466","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/comments?post=6466"}],"version-history":[{"count":2,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages\/6466\/revisions"}],"predecessor-version":[{"id":6468,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages\/6466\/revisions\/6468"}],"up":[{"embeddable":true,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages\/2190"}],"wp:attachment":[{"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/media?parent=6466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}