{"id":1037,"date":"2019-07-21T13:42:09","date_gmt":"2019-07-21T13:42:09","guid":{"rendered":"http:\/\/kabiliravi.com\/?page_id=1037"},"modified":"2019-07-21T21:55:45","modified_gmt":"2019-07-21T21:55:45","slug":"boolean-in-java","status":"publish","type":"page","link":"http:\/\/kabiliravi.com\/index.php\/software\/programming\/my-java-tutorial\/java-basic-syntax\/primitive-data-types\/boolean-in-java\/","title":{"rendered":"Boolean In Java"},"content":{"rendered":"\n<p>In the first sample in&nbsp;<strong>TryBoolean.java<\/strong>, you will learn how to define a boolean variable and how to set its value.<\/p>\n\n\n\n<p>Save following content in a .java file called <strong>TryBoolean.java<\/strong> that is equivalent to your class name <strong>TryBoolean<\/strong>. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public class TryBoolean {\n   public static void main(String[] args) {\n      boolean tryMe1 = false;\n      System.out.print(\"tryMe1 value: \");\n      System.out.println(tryMe1);\n      boolean tryMe2 = true;\n      System.out.print(\"tryMe2 value: \");\n      System.out.println(tryMe2);\n   }\n }<\/pre>\n\n\n\n<p>Using <strong>javac<\/strong> command compile your .java file<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ javac TryBoolean.java<\/pre>\n\n\n\n<p>And using <strong>java<\/strong> command run it. You will see two printed phrases there which shows two values for two boolean variables <strong>tryMe1<\/strong> and <strong>tryMe2<\/strong>. The value of <strong>tryMe1<\/strong> is false and the value of <strong>tryMe2<\/strong> is true. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ java TryBoolean\ntryMe1 value: false\ntryMe2 value: true<\/pre>\n\n\n\n<p>After defining your class in the above sample with <strong>public class TryBoolean<\/strong>, like all other executable java classes you can find <strong>main<\/strong> method. <br>In the first line of main() function block, I defined a boolean variable simply by having&nbsp;<strong>boolean<\/strong>&nbsp;type at the beginning of the line. The variable name&nbsp;<strong>tryMe1<\/strong> comes right after that. You can assign your variable value using equal sign <strong>=<\/strong>. I set <strong>tryMe1<\/strong> to false. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">boolean tryMe1 = false;<\/pre>\n\n\n\n<p>In the next lines I printed a prompt before printing the value of <strong>tryMe1<\/strong> in the console using <strong>System.out.print<\/strong> and I printed the value of <strong>tryMe1<\/strong> which is false using <strong>System.out.println<\/strong> that move the cursor to the line after as well. Click here to learn more about printing in console.  <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">System.out.print(\"tryMe1 value: \");\nSystem.out.println(tryMe1);<\/pre>\n\n\n\n<p>As you see in the next three lines, I tried to declare a variable called <strong>tryMe2<\/strong> and I set its value to <strong>true<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">boolean tryMe2 = true;\nSystem.out.print(\"tryMe2 value: \");\nSystem.out.println(tryMe2);<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the first sample in&nbsp;TryBoolean.java, you will learn how to define a boolean variable and how to set its value. Save following content in a .java file called TryBoolean.java that is equivalent to your class name TryBoolean. public class TryBoolean { public static void main(String[] args) { boolean tryMe1 = false; System.out.print(&#8220;tryMe1 value: &#8220;); System.out.println(tryMe1); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":951,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"ngg_post_thumbnail":0,"footnotes":""},"class_list":["post-1037","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/1037","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/comments?post=1037"}],"version-history":[{"count":11,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/1037\/revisions"}],"predecessor-version":[{"id":1070,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/1037\/revisions\/1070"}],"up":[{"embeddable":true,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/951"}],"wp:attachment":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/media?parent=1037"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}