{"id":14994,"date":"2022-02-15T00:04:30","date_gmt":"2022-02-15T00:04:30","guid":{"rendered":"http:\/\/shivaprogramming.com\/blog\/?p=14994"},"modified":"2022-12-22T20:26:09","modified_gmt":"2022-12-22T20:26:09","slug":"java-programming","status":"publish","type":"post","link":"http:\/\/shivaprogramming.com\/blog\/java-programming\/","title":{"rendered":"Java Programming"},"content":{"rendered":"\n<div class=\"wp-block-columns are-vertically-aligned-center is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_81 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a28ece7887b6\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"ez-toc-cssicon\"><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><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a28ece7887b6\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"http:\/\/shivaprogramming.com\/blog\/java-programming\/#Chapter_1\" >Chapter 1<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"http:\/\/shivaprogramming.com\/blog\/java-programming\/#What_is_Java\" >What is Java?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"http:\/\/shivaprogramming.com\/blog\/java-programming\/#Class_File\" >.Class File<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"http:\/\/shivaprogramming.com\/blog\/java-programming\/#First_Java_Program\" >First Java Program.<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"http:\/\/shivaprogramming.com\/blog\/java-programming\/#Chapter_2\" >Chapter 2<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"http:\/\/shivaprogramming.com\/blog\/java-programming\/#Scanner_Class\" >Scanner Class<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"http:\/\/shivaprogramming.com\/blog\/java-programming\/#Import_Package\" >Import Package<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"http:\/\/shivaprogramming.com\/blog\/java-programming\/#Variable\" >Variable<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"http:\/\/shivaprogramming.com\/blog\/java-programming\/#Primitive_Data_Types\" >Primitive  Data Types<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"http:\/\/shivaprogramming.com\/blog\/java-programming\/#How_to_declare_a_variable\" >How to declare a variable?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"http:\/\/shivaprogramming.com\/blog\/java-programming\/#Explicit_Types_in_Literal_Values\" >Explicit Types in Literal Values<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"http:\/\/shivaprogramming.com\/blog\/java-programming\/#Stored_by_Value\" >Stored by Value<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"http:\/\/shivaprogramming.com\/blog\/java-programming\/#Constants\" >Constants<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"http:\/\/shivaprogramming.com\/blog\/java-programming\/#Type_Casting\" >Type Casting<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"http:\/\/shivaprogramming.com\/blog\/java-programming\/#Chapter_3\" >Chapter 3<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"http:\/\/shivaprogramming.com\/blog\/java-programming\/#Selections\" >Selections<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"http:\/\/shivaprogramming.com\/blog\/java-programming\/#Java_if_condition\" >Java if condition<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"http:\/\/shivaprogramming.com\/blog\/java-programming\/#Switch\" >Switch<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1 class=\"wp-block-heading\" id=\"chapter-1\"><span class=\"ez-toc-section\" id=\"Chapter_1\"><\/span><span style=\"text-decoration: underline;\">Chapter 1<\/span><span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-java\"><span class=\"ez-toc-section\" id=\"What_is_Java\"><\/span>What is Java?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li>Java is an object-oriented programming language. <\/li>\n\n\n\n<li>Popular programming language. <\/li>\n\n\n\n<li>It is strongly typed language<\/li>\n<\/ul>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"class-file\"><span class=\"ez-toc-section\" id=\"Class_File\"><\/span>.Class File<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A class file in Java has a .class extension. It contains bytecode, which is instruction for Java Virtual Machine, which translates that bytecode into platform-specific machine-level instruction based upon whether the Java program runs on Windows or Linux.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"first-java-program\"><span class=\"ez-toc-section\" id=\"First_Java_Program\"><\/span>First Java Program.<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">public class Main {\n    public static void main(String[] args) {\n        System.out.println(\"Welcom to Java programming\");\n    }\n}<\/code><\/pre>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"chapter-2\"><span class=\"ez-toc-section\" id=\"Chapter_2\"><\/span><span style=\"text-decoration: underline;\">Chapter 2<\/span><span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"scanner\"><span class=\"ez-toc-section\" id=\"Scanner_Class\"><\/span>Scanner Class<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Scanner.html\">https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Scanner.html<\/a><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Scanner class is found in the java.util.package<\/li>\n\n\n\n<li>Scanner class is use to get input from the user in Java <\/li>\n\n\n\n<li>It can parse primitive types and strings using regex.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code class=\" line-numbers\"><strong>Scanner input = new Scanner(System.in);<\/strong> \/\/Create an instance of the scanner \nSystem.out.print(\"Enter a double value: \");\nScanner input = new Scanner(System.in);\ndouble d = input.nextDouble();\nSystem.out.println(\"Your value is \" + d);<\/code><\/pre>\n\n\n\n<div data-pym-src=\"https:\/\/www.jdoodle.com\/embed\/v0\/4rJn\">\n    <\/div>\n \n    <script src=\"https:\/\/www.jdoodle.com\/assets\/jdoodle-pym.min.js\" type=\"text\/javascript\">\n    <\/script>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"scanner\"><span class=\"ez-toc-section\" id=\"Import_Package\"><\/span>Import Package<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">import java.util.* ; \/\/ Implicit import\nimport java.util.Scanner; \/\/ Explicit Import\nNo performance difference<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"http:\/\/shivaprogramming.com\/blog\/wp-content\/uploads\/2022\/02\/scanner.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"648\" height=\"273\" src=\"http:\/\/shivaprogramming.com\/blog\/wp-content\/uploads\/2022\/02\/scanner.jpg\" alt=\"\" class=\"wp-image-15248\" srcset=\"http:\/\/shivaprogramming.com\/blog\/wp-content\/uploads\/2022\/02\/scanner.jpg 648w, http:\/\/shivaprogramming.com\/blog\/wp-content\/uploads\/2022\/02\/scanner-300x126.jpg 300w, http:\/\/shivaprogramming.com\/blog\/wp-content\/uploads\/2022\/02\/scanner-640x270.jpg 640w, http:\/\/shivaprogramming.com\/blog\/wp-content\/uploads\/2022\/02\/scanner-400x169.jpg 400w\" sizes=\"auto, (max-width: 648px) 100vw, 648px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"variable\"><span class=\"ez-toc-section\" id=\"Variable\"><\/span>Variable<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A container that holds the value<\/li>\n\n\n\n<li>Strongly typed<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"primitive-data-types\"><span class=\"ez-toc-section\" id=\"Primitive_Data_Types\"><\/span>Primitive  Data Types<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Primitive data types are stored by Value. It is the fastest possible memory.<\/li>\n\n\n\n<li>Numbers, characters, and Booleans values .<\/li>\n\n\n\n<li>Primitive type names are all lowercase.<\/li>\n\n\n\n<li>Primitive values are always signed they support positive and negative values.<\/li>\n\n\n\n<li>btye, <code>int,char,short, long, double, float, boolean<\/code><\/li>\n\n\n\n<li>String is not primitive type.It is an object. It is a complex object, an instance of a class.<\/li>\n\n\n\n<li><a href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/java\/nutsandbolts\/datatypes.html\">https:\/\/docs.oracle.com\/javase\/tutorial\/java\/nutsandbolts\/datatypes.html<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-declare-a-variable\"><span class=\"ez-toc-section\" id=\"How_to_declare_a_variable\"><\/span>How to declare a variable?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">int myVariable =20;\nint =&gt; Data type\nmyVariable =&gt; identifier\n= Assigment\n20 = value<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Starting Java 10, local variables can use type inference<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">var myVariable=20;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/github.com\/shivasharma\/IS147\/blob\/master\/src\/Week3\/DataTypes.java\">DataTypes example <\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">        int firstValue=100;\n        int secondValue=firstValue;\n        firstValue=50;\n        secondValue=70;\n        System.out.println(firstValue);\n        System.out.println(secondValue);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"explicit-types-in-literal-values\"><span class=\"ez-toc-section\" id=\"Explicit_Types_in_Literal_Values\"><\/span>Explicit Types in Literal Values<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">var  myIntValue=5;\nvar  myFloatValue=5f;\nvar myDoubleValue=5d;\nvar myLongValue=5L or 5l<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"stored-by-value\"><span class=\"ez-toc-section\" id=\"Stored_by_Value\"><\/span>Stored by Value <span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"constants\"><span class=\"ez-toc-section\" id=\"Constants\"><\/span>Constants<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A variable whose value cannot be changed once it is assigned.<\/li>\n\n\n\n<li>Constant the variable name on constants<\/li>\n\n\n\n<li>final datatype CONSTANTNAME = VALUE;&nbsp;&nbsp;<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">final int VALUE=123;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/github.com\/shivasharma\/IS147\/blob\/master\/src\/Week3\/Final.java\">Constant example<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"type-casting\"><span class=\"ez-toc-section\" id=\"Type_Casting\"><\/span>Type Casting<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Converting the value from one data type to another data type<\/p>\n\n\n<p>Implicit casting<br \/>\n  double data= 3; (type widening)<br \/>\nExplicit casting<br \/>\n  int i = (int)3.0; (type narrowing)<br \/>\n  int i = (int)3.9; (Fraction part is truncated)<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"chapter-3\"><span class=\"ez-toc-section\" id=\"Chapter_3\"><\/span>Chapter 3<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"selections\"><span class=\"ez-toc-section\" id=\"Selections\"><\/span>Selections<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">How do we compare two values in java?<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">byte value1=1;\nbyte values2=2;\nboolean result=(value1&gt;value2);\nSystem.out.println(result);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Result: false<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"java-if-condition\"><span class=\"ez-toc-section\" id=\"Java_if_condition\"><\/span>Java if condition <span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Java if condition checks for the decision-making statement based on its true or false condition.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">if(condition) \n{\n   \/\/ Statements to execute if the given condition is true\n  \n}\n---------------------------------\nif (condition)\n{\n   \/\/ Run this block if\n condition is true\n}\nelse\n{\n  \/\/ Run this block if\n condition is false\n}\n-----------------------------------------------------\nif (condition)\n    statement;\nelse if (condition)\n    statement;\n.\n.\nelse\n    statement;\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"switch\"><span class=\"ez-toc-section\" id=\"Switch\"><\/span>Switch<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Execute the code based on the value of the switch expression value.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">switch (expression)\n{\n  case value1:\n    statement1;\n    break;\n  case value2:\n    statement2;\n    break;\n\n  default:\n    statementDefault;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Chapter 1 What is Java? .Class File A class file in Java has a .class extension. It contains bytecode, which is instruction for Java Virtual Machine,&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":true,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[42,43],"class_list":["post-14994","post","type-post","status-publish","format-standard","hentry","category-java","tag-java","tag-java-programming"],"_links":{"self":[{"href":"http:\/\/shivaprogramming.com\/blog\/wp-json\/wp\/v2\/posts\/14994","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/shivaprogramming.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shivaprogramming.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shivaprogramming.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shivaprogramming.com\/blog\/wp-json\/wp\/v2\/comments?post=14994"}],"version-history":[{"count":48,"href":"http:\/\/shivaprogramming.com\/blog\/wp-json\/wp\/v2\/posts\/14994\/revisions"}],"predecessor-version":[{"id":15959,"href":"http:\/\/shivaprogramming.com\/blog\/wp-json\/wp\/v2\/posts\/14994\/revisions\/15959"}],"wp:attachment":[{"href":"http:\/\/shivaprogramming.com\/blog\/wp-json\/wp\/v2\/media?parent=14994"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shivaprogramming.com\/blog\/wp-json\/wp\/v2\/categories?post=14994"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shivaprogramming.com\/blog\/wp-json\/wp\/v2\/tags?post=14994"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}