{"id":303,"date":"2015-12-08T09:00:27","date_gmt":"2015-12-08T09:00:27","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?page_id=303"},"modified":"2018-06-05T06:29:57","modified_gmt":"2018-06-05T06:29:57","slug":"expression-statement-code-blocks-in-java","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/java\/expression-statement-code-blocks-in-java","title":{"rendered":"Expression, Statement And Code Blocks In JAVA"},"content":{"rendered":"<p>In this article we will talk about expressions, statements and code blocks in JAVA.<\/p>\n<hr \/>\n<h4><strong>What Is Expressions in JAVA &amp; Definition:<\/strong><\/h4>\n<p>Expressions are building blocks off all JAVA programs which are made up of variables, operators, and method invocations. The expression are created according to the syntax and evaluates to a single value. For example, if we want to covert miles to kilometre we first need to know a mile is equal to 1.609344km. Now lets suppose we want to convert 50 miles to kilometres.<\/p>\n<pre>double kilometres = (50 * 1.609344);<\/pre>\n<p>In the above statement <code>kilometres = (50 * 1.609344)<\/code> is an expression and data type double is not part of an expression. Typically except data type and ; forms an expression.<\/p>\n<p><strong>Expression Examples In JAVA:<\/strong><\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-388\" src=\"\/java\/wp-content\/uploads\/2015\/12\/Expression-Examples-In-Java.jpg\" alt=\"Expression Examples In Java\" width=\"305\" height=\"203\" srcset=\"https:\/\/abhiandroid.com\/java\/wp-content\/uploads\/2015\/12\/Expression-Examples-In-Java.jpg 305w, https:\/\/abhiandroid.com\/java\/wp-content\/uploads\/2015\/12\/Expression-Examples-In-Java-300x200.jpg 300w\" sizes=\"auto, (max-width: 305px) 100vw, 305px\" \/><\/center>Some more examples of expression:<\/p>\n<pre>int score = 50;\u00a0 \/\/In bold is an expression<\/pre>\n<p>If you are using literal value, then except data type and ; is a part of expression. So <code>score = 50<\/code> is an expression.<\/p>\n<p>Another example of expression:<\/p>\n<pre>if(score == 50){\r\nSystem.out.println( \u201cThis is also an expression\u201d );\r\n}<\/pre>\n<p>If you are using control flow if statements, then component inside bracket is a part of expression. So <code>score == 50<\/code> is an expression. Remember here braces ( and ) is not part of expression.<\/p>\n<p>Also for methods component inside bracket is a part of expression. So <code>\u201cThis is also an expression\u201d<\/code> is an expression. Remember here also braces ( and ) is not part of expression.<\/p>\n<hr \/>\n<h4><strong>Statement In JAVA:<\/strong><\/h4>\n<p>Statement forms a complete unit of execution. All statements are terminated with semicolon (;). For example:<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-389\" src=\"\/java\/wp-content\/uploads\/2015\/12\/Statement-In-Java.jpg\" alt=\"Statement In Java\" width=\"306\" height=\"239\" srcset=\"https:\/\/abhiandroid.com\/java\/wp-content\/uploads\/2015\/12\/Statement-In-Java.jpg 306w, https:\/\/abhiandroid.com\/java\/wp-content\/uploads\/2015\/12\/Statement-In-Java-300x234.jpg 300w\" sizes=\"auto, (max-width: 306px) 100vw, 306px\" \/><\/center><\/p>\n<pre>int myRoll = 3;<\/pre>\n<p>The above complete line is a statement. In JAVA following types form expressions of assignment: any use of ++ or &#8211; &#8211; , method invocations and object creation expressions can be made into statement by terminating with semicolon(;);<\/p>\n<p>Lets see each type expression made into statement:<\/p>\n<p><strong>Assignment expression made into expression:<\/strong><\/p>\n<pre>int myRoll = 3;<\/pre>\n<p><strong>Any use of ++ or &#8212; made into expression:<\/strong><\/p>\n<pre>myRoll++;\r\nmyRoll--;<\/pre>\n<p><strong>Method invocations:<\/strong><\/p>\n<pre>System.out.println(myRoll);\r\nObject creation expressions made into statements\r\nBike myBike = new Bike();<\/pre>\n<p><strong>Important Note:<\/strong> Declaration statements and control flow statements are other two types of statements in JAVA.<\/p>\n<hr \/>\n<h4><strong>Blocks:<\/strong><\/h4>\n<p>A blocks in JAVA is a group of zero or more statements enclosed between braces. The block begins with opening braces ({) and ends with closing braces (}). Even though it is a statement but doesn\u2019t end with semicolon(;). For example:<\/p>\n<pre>{\r\nint myRoll = 3;\r\nString myName = \u201cAbhishek\u201d;\r\n}<\/pre>\n<p>It is mostly used with control flow statement. For example:<\/p>\n<pre>If (x == 50){\r\nSystem.out.println(\u201cExample of control flow statement\u201d);\r\nx--;\r\n}<\/pre>\n<p>In the above example all code inside braces {} is part of blocks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article we will talk about expressions, statements and code blocks in JAVA. What Is Expressions in JAVA &amp; Definition: Expressions are building blocks off all JAVA programs which are made up of variables, operators, and method invocations. The expression are created according to the syntax and evaluates to a single value. For example, &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/expression-statement-code-blocks-in-java\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Expression, Statement And Code Blocks In JAVA<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"footnotes":""},"class_list":["post-303","page","type-page","status-publish","hentry"],"psp_head":"<title>Expressions, Statements And Code Blocks In JAVA \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"In this article we will talk about expressions, statements and code blocks in JAVA. Expressions are building blocks off all JAVA programs which are made up of variables, operators, and method invocations.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/expression-statement-code-blocks-in-java\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/303","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/comments?post=303"}],"version-history":[{"count":2,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/303\/revisions"}],"predecessor-version":[{"id":1472,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/303\/revisions\/1472"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}