{"id":232,"date":"2020-11-09T10:44:42","date_gmt":"2020-11-09T10:44:42","guid":{"rendered":"https:\/\/usemynotes.com\/?p=232"},"modified":"2023-01-20T17:07:51","modified_gmt":"2023-01-20T17:07:51","slug":"variables-and-constants-in-java","status":"publish","type":"post","link":"https:\/\/usemynotes.com\/variables-and-constants-in-java\/","title":{"rendered":"Constants in Java Programming"},"content":{"rendered":"<p>In this tutorial, we will be focusing on two important fundamental topics of the Java programming language. We will go through Variables and <strong>Constants in Java programming<\/strong> which are also one of the building blocks of a program.<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-233 size-large\" title=\"Variables and Constants in Java\" src=\"https:\/\/usemynotes.com\/wp-content\/uploads\/2020\/11\/variables-and-constants-in-java-1024x676.jpg\" alt=\"Variables and Constants in Java Programming\" width=\"1024\" height=\"676\" srcset=\"https:\/\/usemynotes.com\/wp-content\/uploads\/2020\/11\/variables-and-constants-in-java-1024x676.jpg 1024w, https:\/\/usemynotes.com\/wp-content\/uploads\/2020\/11\/variables-and-constants-in-java-300x198.jpg 300w, https:\/\/usemynotes.com\/wp-content\/uploads\/2020\/11\/variables-and-constants-in-java-768x507.jpg 768w, https:\/\/usemynotes.com\/wp-content\/uploads\/2020\/11\/variables-and-constants-in-java-1536x1014.jpg 1536w, https:\/\/usemynotes.com\/wp-content\/uploads\/2020\/11\/variables-and-constants-in-java-150x99.jpg 150w, https:\/\/usemynotes.com\/wp-content\/uploads\/2020\/11\/variables-and-constants-in-java-696x459.jpg 696w, https:\/\/usemynotes.com\/wp-content\/uploads\/2020\/11\/variables-and-constants-in-java-1068x705.jpg 1068w, https:\/\/usemynotes.com\/wp-content\/uploads\/2020\/11\/variables-and-constants-in-java-636x420.jpg 636w, https:\/\/usemynotes.com\/wp-content\/uploads\/2020\/11\/variables-and-constants-in-java.jpg 1880w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/usemynotes.com\/variables-and-constants-in-java\/#Variables_and_Constants_in_Java_Programming\" >Variables and Constants in Java Programming<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/usemynotes.com\/variables-and-constants-in-java\/#Variable_in_Java_Programming\" >Variable in Java Programming<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/usemynotes.com\/variables-and-constants-in-java\/#Types_of_Variables_in_Java_Programming\" >Types of Variables in Java Programming<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/usemynotes.com\/variables-and-constants-in-java\/#Local_Variables_in_Java_Programming\" >Local Variables in Java Programming<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/usemynotes.com\/variables-and-constants-in-java\/#Instance_Variables_in_Java_Programming\" >Instance Variables in Java Programming<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/usemynotes.com\/variables-and-constants-in-java\/#Static_Variables_in_Java_Programming\" >Static Variables in Java Programming<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/usemynotes.com\/variables-and-constants-in-java\/#Constants_in_Java_Programming\" >Constants in Java Programming<\/a><\/li><\/ul><\/nav><\/div>\n<h2 style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"Variables_and_Constants_in_Java_Programming\"><\/span>Variables and Constants in Java Programming<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h2><span class=\"ez-toc-section\" id=\"Variable_in_Java_Programming\"><\/span>Variable in Java Programming<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A Variable in Java programming language is used to store a value. The value of a variable can be changed during program execution (runtime). A variable can either be declared without giving it a value or we can create a variable with some initial value.<\/p>\n<p><strong>In Java programming,<\/strong><\/p>\n<ol>\n<li>Variable is declared using the following syntax:\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">&lt;Data Type&gt; &lt;Variable name&gt;;<\/pre>\n<\/li>\n<li>Variable is declared and initialized using the following syntax:\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">&lt;Data Type&gt; &lt;Variable name&gt; = &lt;Initial Value&gt;;<\/pre>\n<\/li>\n<\/ol>\n<p><strong>Example 1:<\/strong><\/p>\n<p>float average;<\/p>\n<p>In the above example, the variable is named &#8220;average&#8221; which is not initialized with any value. The <a href=\"https:\/\/usemynotes.com\/what-is-data-types-in-java\/\">data type<\/a> is considered as &#8220;float&#8221; so that it will contain floating-point values.<\/p>\n<p><strong>Example 2:<\/strong><\/p>\n<p>int alphabets = 26;<\/p>\n<p>In the above example, we have a variable which is named &#8220;alphabets&#8221; whose value is initialized with a value of 26. This value 26 is an integer type of value, so we have the data type of the variable set to &#8220;int&#8221;.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Types_of_Variables_in_Java_Programming\"><\/span>Types of Variables in Java Programming<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>There are 3 types of variables in Java programming. They are,<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Local_Variables_in_Java_Programming\"><\/span>Local Variables in Java Programming<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>A variable that is declared within the scope of a function is a local variable. These variables are only accessible within the body of the function where they are declared. The memory allocated to these variables gets released as soon as the function completes its execution.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">void add(int x, int y){\r\nint sum = x+y;\r\nSystem.out.println(&quot;Sum: &quot; + sum);\r\n}<\/pre>\n<p>In the above example, variables &#8220;x&#8221;, &#8220;y&#8221; and &#8220;sum&#8221; are local variables whose scope is within the &#8220;add&#8221; function. These variables cannot be accessed out of the function body.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Instance_Variables_in_Java_Programming\"><\/span>Instance Variables in Java Programming<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>These variables are non-static variables that are declared within a class but not in the function of a class. These variables are also member variables of a class.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">class Greet {\r\nString message = &quot;hello World&quot;;\r\n            public void greet() {\r\n            \tSystem.out.println(message);\r\n\t    }\r\n}<\/pre>\n<p>In the above example, a variable &#8220;message&#8221; is declared and initialized which is a member variable of the Greet class. This variable is neither declared in a function nor is a static variable and that makes it an instance variable.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Static_Variables_in_Java_Programming\"><\/span>Static Variables in Java Programming<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Static variables are declared within a class using a special <a href=\"https:\/\/usemynotes.com\/features-of-java\/\">keyword<\/a> &#8220;static&#8221;. Due to the static nature of these variables, they can be accessed without creating an object of the class when they reside. A static variable can be accessed using the following syntax:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">&lt;Class Name&gt;.&lt;Static Variable Name&gt;<\/pre>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">class Human {\r\n       static int eyes = 2;\r\n}\r\nclass Main {\r\n       public static void main() {\r\n               System.out.println(&quot;We have &quot; + Human.eyes + &quot;eyes&quot;);\r\n       }\r\n}<\/pre>\n<p>In the above example, class Human contains a static variable &#8220;eyes&#8221; which is also initialized with a value of 2. The static nature of the variable makes it accessible without instantiating an object of the Human class. The Main class accesses its value just by using the class name followed by the dot operator and then the variable name (in this case the variable name is &#8216;eyes&#8217;).<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Constants_in_Java_Programming\"><\/span>Constants in Java Programming<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Constants in <a href=\"https:\/\/usemynotes.com\/what-is-java\/\">Java<\/a> programming can be a value or a Java variable whose value will not change during the execution of the program. A constant value can be such as 1100, 12.32, 5000, etc. These values have a fixed meaning that does not change under different circumstances.<\/p>\n<p>A constant variable in Java programming is a variable that is declared to have a value in it that will be constant till the program ends. Generally, they are declared using a keyword known as \u201cfinal\u201d. The variables which are declared using the \u201cfinal\u201d keyword will only be initialized once and then their value will be constant and will not change during the execution of the program.<\/p>\n<p><strong>Example of constants in Java programming:<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">class English {\r\n      final int vowels = 5;\r\n}<\/pre>\n<p>In the above constants in Java programming example. The class English contains an instance variable named &#8220;vowels&#8221; which is of integer type and is also a &#8220;final&#8221; type variable. This variable will have a constant value of 5 which cannot be changed during the execution of the program (runtime).<\/p>\n<p>Thanks for learning Variables and <strong>Constants in Java programming<\/strong>, I hope you like this model.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will be focusing on two important fundamental topics of the Java programming language. We will go through Variables and Constants in Java programming which are also one of the building blocks of a program. Variables and Constants in Java Programming Variable in Java Programming A Variable in Java programming language is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":233,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":{"0":"post-232","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-java-programming"},"_links":{"self":[{"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/posts\/232","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/comments?post=232"}],"version-history":[{"count":0,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/posts\/232\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/media\/233"}],"wp:attachment":[{"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/media?parent=232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/categories?post=232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/tags?post=232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}