{"id":1146,"date":"2022-02-24T18:44:28","date_gmt":"2022-02-24T18:44:28","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1146"},"modified":"2022-02-24T18:44:28","modified_gmt":"2022-02-24T18:44:28","slug":"variables-and-literals","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/24\/variables-and-literals\/","title":{"rendered":"Variables and Literals"},"content":{"rendered":"\n<p>In this tutorial, we will learn about Java variables and literals with the help of examples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"variables\">Java Variables<\/h2>\n\n\n\n<p>A variable is a location in memory (storage area) to hold data.<\/p>\n\n\n\n<p>To indicate the storage area, each variable should be given a unique name (identifier). Learn more about\u00a0Java identifiers.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"create-variables\">Create Variables in Java<\/h3>\n\n\n\n<p>Here&#8217;s how we create a variable in Java,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int speedLimit = 80;<\/code><\/pre>\n\n\n\n<p>Here,&nbsp;<var>speedLimit<\/var>&nbsp;is a variable of&nbsp;<var>int<\/var>&nbsp;data type and we have assigned value&nbsp;<strong>80<\/strong>&nbsp;to it.<\/p>\n\n\n\n<p>The int data type suggests that the variable can only hold integers. To learn more, visit\u00a0Java data types.<\/p>\n\n\n\n<p>In the example, we have assigned value to the variable during declaration. However, it&#8217;s not mandatory.<\/p>\n\n\n\n<p>You can declare variables and assign variables separately. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int speedLimit;\nspeedLimit = 80;<\/code><\/pre>\n\n\n\n<p><strong>Note<\/strong>: Java is a statically-typed language. It means that all variables must be declared before they can be used.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Change values of variables<\/h3>\n\n\n\n<p>The value of a variable can be changed in the program, hence the name&nbsp;<strong>variable<\/strong>. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int speedLimit = 80;\n... .. ...\nspeedLimit = 90; <\/code><\/pre>\n\n\n\n<p>Here, initially, the value of&nbsp;<var>speedLimit<\/var>&nbsp;is&nbsp;<strong>80<\/strong>. Later, we changed it to&nbsp;<strong>90<\/strong>.<\/p>\n\n\n\n<p>However, we cannot change the data type of a variable in Java within the same scope.<\/p>\n\n\n\n<p>What is the variable scope?<\/p>\n\n\n\n<p>Don&#8217;t worry about it for now. Just remember that we can&#8217;t do something like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int speedLimit = 80;\n... .. ...\nfloat speedLimit;<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"naming-rules\">Rules for Naming Variables in Java<\/h2>\n\n\n\n<p>Java programming language has its own set of rules and conventions for naming variables. Here&#8217;s what you need to know:<\/p>\n\n\n\n<ul><li>Java is case sensitive. Hence,\u00a0<var>age<\/var>\u00a0and\u00a0<var>AGE<\/var>\u00a0are two different variables. For example,<br>\u00a0<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>int age = 24;\n int AGE = 25;\n System.out.println(age); \n\/\/ prints 24 System.out.println(AGE); \n\/\/ prints 25<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<ul><li>Variables must start with either a\u00a0<strong>letter<\/strong>\u00a0or an\u00a0<strong>underscore, _<\/strong>\u00a0or a\u00a0<strong>dollar, $<\/strong>\u00a0sign. For example,<br><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>int age; \n\/\/ valid name and good practice int _age; \n\/\/ valid but bad practice int $age; \n\/\/ valid but bad practice<\/code><\/pre>\n\n\n\n<ul><li>Variable names cannot start with numbers. For example,<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code> int 1age; \n\/\/ invalid variables<\/code><\/pre>\n\n\n\n<ul><li>Variable names can&#8217;t use whitespace. For example,<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code><code>int my age; <\/code>\n<code>\/\/ invalid variables<\/code><\/code><\/pre>\n\n\n\n<p><br>Here, is we need to use variable names having more than one word, use all lowercase letters for the first word and capitalize the first letter of each subsequent word. For example,\u00a0<var>myAge<\/var>.<\/p>\n\n\n\n<p>When creating variables, choose a name that makes sense. For example,\u00a0<var>score<\/var>,\u00a0<var>number<\/var>,\u00a0<var>level<\/var>\u00a0makes more sense than variable names such as\u00a0<var>s<\/var>,\u00a0<var>n<\/var>, and\u00a0<var>l<\/var>.<\/p>\n\n\n\n<p>If you choose one-word variable names, use all lowercase letters. For example, it&#8217;s better to use\u00a0<var>speed<\/var>\u00a0rather than\u00a0<var>SPEED<\/var>, or\u00a0<var>sPEED<\/var>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>There are 4 types of variables in Java programming language:<\/p>\n\n\n\n<ul><li>Instance Variables (Non-Static Fields)<\/li><li>Class Variables (Static Fields)<\/li><li>Local Variables<\/li><li>Parameters<\/li><\/ul>\n\n\n\n<p>If you are interested to learn more about it now, visit\u00a0Java Variable Types.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"literals\">Java literals<\/h2>\n\n\n\n<p>Literals are data used for representing fixed values. They can be used directly in the code. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int a = 1;\nfloat b = 2.5;\nchar c = 'F';<\/code><\/pre>\n\n\n\n<p>Here,&nbsp;<code>1<\/code>,&nbsp;<code>2.5<\/code>, and&nbsp;<code>'F'<\/code>&nbsp;are literals.<\/p>\n\n\n\n<p>Here are different types of literals in Java.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"boolean-literals\">1. Boolean Literals<\/h3>\n\n\n\n<p>In Java, boolean literals are used to initialize boolean data types. They can store two values: true and false. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>boolean flag1 = false;\nboolean flag2 = true;<\/code><\/pre>\n\n\n\n<p>Here,&nbsp;<code>false<\/code>&nbsp;and&nbsp;<code>true<\/code>&nbsp;are two boolean literals.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"integer-literals\">2. Integer Literals<\/h3>\n\n\n\n<p>An integer literal is a numeric value(associated with numbers) without any fractional or exponential part. There are 4 types of integer literals in Java:<\/p>\n\n\n\n<ol><li>binary (base 2)<\/li><li>decimal (base 10)<\/li><li>octal (base 8)<\/li><li>hexadecimal (base 16)<\/li><\/ol>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ binary\nint binaryNumber = 0b10010;\n\/\/ octal \nint octalNumber = 027;\n\n\/\/ decimal\nint decNumber = 34;\n\n\/\/ hexadecimal \nint hexNumber = 0x2F; \/\/ 0x represents hexadecimal\n\/\/ binary\nint binNumber = 0b10010; \/\/ 0b represents binary<\/code><\/pre>\n\n\n\n<p>In Java, binary starts with&nbsp;<strong>0b<\/strong>, octal starts with&nbsp;<strong>0<\/strong>, and hexadecimal starts with&nbsp;<strong>0x<\/strong>.<\/p>\n\n\n\n<p><strong>Note<\/strong>: Integer literals are used to initialize variables of integer types like&nbsp;<code>byte<\/code>,&nbsp;<code>short<\/code>,&nbsp;<code>int<\/code>, and&nbsp;<code>long<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"float-literals\">3. Floating-point Literals<\/h3>\n\n\n\n<p>A floating-point literal is a numeric literal that has either a fractional form or an exponential form. For 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    double myDouble = 3.4;\n    float myFloat = 3.4F;\n \n    \/\/ 3.445*10^2\n    double myDoubleScientific = 3.445e2;\n\n    System.out.println(myDouble);  \/\/ prints 3.4\n    System.out.println(myFloat);    \/\/ prints 3.4\n    System.out.println(myDoubleScientific);   \/\/ prints 344.5\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Note<\/strong>: The floating-point literals are used to initialize&nbsp;<code>float<\/code>&nbsp;and&nbsp;<code>double<\/code>&nbsp;type variables.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"character-literals\">4. Character Literals<\/h3>\n\n\n\n<p>Character literals are unicode character enclosed inside single quotes. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>char letter = 'a';<\/code><\/pre>\n\n\n\n<p>Here,&nbsp;<code>a<\/code>&nbsp;is the character literal.<\/p>\n\n\n\n<p>We can also use escape sequences as character literals. For example,&nbsp;<strong>\\b<\/strong>&nbsp;(backspace),&nbsp;<strong>\\t<\/strong>&nbsp;(tab),&nbsp;<strong>\\n<\/strong>&nbsp;(new line), etc.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"string-literals\">5. String literals<\/h3>\n\n\n\n<p>A string literal is a sequence of characters enclosed inside double-quotes. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String str1 = \"Java Programming\";\nString str2 = \"Programiz\";<\/code><\/pre>\n\n\n\n<p>Here,&nbsp;<code>Java Programming<\/code>&nbsp;and&nbsp;<code>Programiz<\/code>&nbsp;are two string literals.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about Java variables and literals with the help of examples. Java Variables A variable is a location in memory (storage area) to hold data. To indicate the storage area, each variable should be given a unique name (identifier). Learn more about\u00a0Java identifiers. Create Variables in Java Here&#8217;s how we [&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\/1146"}],"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=1146"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1146\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}