

{"id":6161,"date":"2018-01-20T07:48:30","date_gmt":"2018-01-20T02:18:30","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=6161"},"modified":"2026-05-05T15:53:46","modified_gmt":"2026-05-05T10:23:46","slug":"basic-java-syntax","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/basic-java-syntax\/","title":{"rendered":"Java Syntax &#8211; A Complete Guide to Master Java"},"content":{"rendered":"<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:2168,&quot;href&quot;:&quot;https:\\\/\\\/codegym.cc\\\/groups\\\/posts\\\/java-syntax&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20250909044138\\\/https:\\\/\\\/codegym.cc\\\/groups\\\/posts\\\/java-syntax&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-11 00:33:25&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-15 14:38:52&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-18 23:34:11&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-27 04:51:52&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-05 19:13:29&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-09 21:03:40&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-14 02:15:14&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-18 11:21:45&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-22 16:53:04&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-26 18:20:04&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-06 13:34:47&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-10 12:40:57&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-13 14:59:05&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-17 00:45:40&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-23 10:11:18&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-05 09:35:55&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-10 14:03:17&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-13 15:59:00&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-17 13:20:34&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-25 03:56:55&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-29 06:06:23&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-04 11:54:05&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-07 22:55:01&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-30 20:02:56&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-06 02:19:54&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-09 04:18:57&quot;,&quot;http_code&quot;:200}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-05-09 04:18:57&quot;,&quot;http_code&quot;:200},&quot;process&quot;:&quot;done&quot;}]'><\/div>\n<p>In this Java Tutorial, we will learn about Java Syntax. So let&#8217;s start!!!<\/p>\n<h3>What does Syntax mean?<\/h3>\n<p>Well, simply put, syntax is a particular format for writing commands in a programming language. Every language has its individual syntax.<\/p>\n<p>Without proper knowledge of syntax, it would be difficult to generate desired output from a programming language. Syntax are also referred to as the language of the computer.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Java-Syntax.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78473\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Java-Syntax.jpg\" alt=\"Java Syntax\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Java-Syntax.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Java-Syntax-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Java-Syntax-1024x536.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Java-Syntax-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Java-Syntax-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Java-Syntax-520x272.jpg 520w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><\/p>\n<h3>Java Syntax<\/h3>\n<p><a href=\"https:\/\/codegym.cc\/groups\/posts\/java-syntax\">Java syntax<\/a> is similar to C and C++ because it comes from them. So, let&#8217;s dive into the depths of syntax in Java!<\/p>\n<p>As soon as a Java program starts, it has a package. A package consists of many classes, each consisting of functions, variables and methods. We start with knowing the syntax for identifiers in Java.<\/p>\n<h3>Identifiers in Java<\/h3>\n<p>Identifiers are the names given to entities such as classes, variables, and functions to uniquely identify them throughout the program.<\/p>\n<p><strong>They contain:<\/strong><\/p>\n<ul>\n<li>Underscore( _ ) and dollar($) (only special characters allowed in naming identifiers.)<\/li>\n<li>Unicode characters such as numbers and letters.<\/li>\n<\/ul>\n<h3>Java Keywords<\/h3>\n<p>Keywords are the identifiers that have special meaning to the compiler. These cannot be used to name variables, classes, functions, etc. These are reserved words.<\/p>\n<p><strong>Some of the keywords are:<\/strong><\/p>\n<ul>\n<li><strong>abstract<\/strong> &#8211; This keyword specifies that the class is abstract.<\/li>\n<li><strong>boolean<\/strong>&#8211; This is a data type specifier which mentions that a particular variable is a Boolean.<\/li>\n<li><strong>byte<\/strong>&#8211; this is a data type specifier that specifies a particular variable to be of byte type.<\/li>\n<li><strong>Case<\/strong>&#8211; a switch case keyword which specifies a program to be performed if a particular case is satisfied<\/li>\n<li><strong>catch<\/strong> &#8211; during a throw case of error handling, catch encloses actions to be performed if an exception occurs<\/li>\n<li><strong>break<\/strong>&#8211; The break keyword breaks the control out of a loop<\/li>\n<li><strong>Void<\/strong> &#8211; this keyword renders a method non-returnable<\/li>\n<li><strong>char<\/strong>&#8211; This is a data type specifier that specifies that the variable is of character type<\/li>\n<li><strong>Class<\/strong>&#8211; This keyword specifies the creation of a new class followed by a class name.<\/li>\n<li><strong>Extends<\/strong>&#8211; This is used to indicate that the class mentioned after it is the derivation of a superclass.<\/li>\n<\/ul>\n<h3>Java Literals<\/h3>\n<p>These are the identifiers that have a particular value in themselves. These can be assigned to variables. Literals can also be thought of as constants.<br \/>\nThese are of different types, such as numeric, characters, strings, etc.<\/p>\n<h4>a. Numeric Literals<\/h4>\n<p><strong>For numeric literals, there are 4 kinds of variations:<\/strong><\/p>\n<p><strong>i.<\/strong> <strong>Decimal<\/strong>(Any number of base 10), Example- 87,53<br \/>\n<strong>ii.<\/strong> <strong>Binary<\/strong>(Any number with a base 2), Example- 1011,110<br \/>\n<strong>iii.<\/strong> <strong>Octal<\/strong> Point(Any number with base 8), Example= 1177<br \/>\n<strong>iv.<\/strong>\u00a0<strong>Hexadecimal<\/strong> Point(Any number with base 16), Example- A54C<\/p>\n<h4>b. Floating point Literals<\/h4>\n<p>We can specify the numeric values only with the use of a decimal point(.). These numbers represent fractional numbers that cannot be expressed as whole integers.<\/p>\n<p>For Example: 10.876<\/p>\n<h4>c. Character Literals<\/h4>\n<p>These are the literals which deal with characters, i.e., inputs which are not numeric in type.<\/p>\n<p><strong>i. Single quoted character<\/strong>&#8211; This encloses all the uni-length characters enclosed within single quotes. Example- \u2018a \u2019,\u2019j\u2019.<\/p>\n<p><strong>ii. Escape Sequences<\/strong>&#8211; These are the characters preceded by a backslash that perform a specific function when printed on screen such as a tab, creating a new line, etc. Example-\u2019\\n\u2019<\/p>\n<p><strong>iii. Unicode Representation<\/strong>&#8211; It can be represented by specifying the concerned Unicode value of the character after \u2018\\u\u2019. Example- \u201c\\u0054\u2019<\/p>\n<h3>Comments in Java<\/h3>\n<p>Comments are needed whenever the developer needs to add documentation about a function that is defined within the program. This is to enhance the code-readability and understandability. Comments are not executed by the compiler and are simply ignored during execution.<\/p>\n<p><strong>The comments are of the following types:<\/strong><\/p>\n<h4>a. Single Line Comments in Java<\/h4>\n<p>These comments, as the name suggests, consist of a single line of comment generally written after a code line to explain its meaning.<br \/>\nThey are marked with two backslashes(\/\/) and are automatically terminated when there<br \/>\nA new line is inserted in the editor.<\/p>\n<p><strong>For Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">int i = 6;\r\nString s = \u201cDataFlair\u201d; \/\/ The value of i is set to 6 initially.The string has value \u201cDataFlair\u201d\r\n<\/pre>\n<h4>b. Multi-Line Comments in Java<\/h4>\n<p>These comments span multiple lines throughout the codebase. They are generally written at the beginning of the program to elaborate about the algorithm. These are also used by developers to comment out blocks of code during debugging. They comprise of a starting tag(\/*) and an ending tag(*\/)<\/p>\n<p><strong>For Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">class Comment {\r\n  public static void main(String[] args) throws IOException {\r\n    \/* all this is under a multiline comment\r\n    These wont be executed by compiler\r\n    Thank you*\/\r\n  }\r\n}<\/pre>\n<h4>c. Documentation Comment<\/h4>\n<p>The javadoc tool processes these comments while generating documentation<\/p>\n<h3>Basic structure of a Java program<\/h3>\n<p>There are two basic parts of a Java program, namely, Packages and the main method.<\/p>\n<h4>1. Java Package<\/h4>\n<p>This is the same thing as a folder in your computer. It contains classes, interfaces, and many more. It organizes the classes into namespaces. The classes that are in the same package can access each other&#8217;s protected and private members.<\/p>\n<p>They are generally imported by using the import keyword, i.e., import java.util.*, where we are importing Java\u2019s util package<\/p>\n<h4>2. Main Method in Java<\/h4>\n<p>If the main method is not present in a program or it is not correct, then the program will compile but won\u2019t run. The main method marks the entry point of the compiler in the program. The main method must always be static.<\/p>\n<p><strong>For Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public class DataFlair {\r\n  void teachJava() {\r\n    System.out.println(\u201cTeaching Java by DataFlair\u201d);\r\n  }\r\n  public static void main(String[] args) throws IOException {\r\n    System.out.println(\u201cIn main\u201d);\r\n    DataFlair ob = new DatFlair();\r\n    ob.teachJava();\r\n  }\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">In main<br \/>\nTeaching Java by DataFlair<\/div>\n<p>The compiler first executes the main method and then the object method.<\/p>\n<h4>5. Java Control Statements<\/h4>\n<p>These are the statements in a Java program that control the flow of a program. Several control statements manage the flow of the program by making decisions, performing repetitive tasks, or jumping to other statements.<\/p>\n<p>The syntax of control statements in Java is pretty straightforward. Let&#8217;s take a deeper look at the control statements in Java.<\/p>\n<h4>a. Conditional Statements in Java<\/h4>\n<p>These statements are purely based on condition flow of the program. It&#8217;s divided into the following 3 parts<\/p>\n<p><strong>i. if statement<\/strong><\/p>\n<p>The statement suggests that if a particular statement yields true, then the block enclosed within the if statement gets executed.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">if (condition) {\r\n  action to be performed\r\n}<\/pre>\n<p><strong>ii. if else statement<\/strong><\/p>\n<p>This statement is of the format that if a condition enclosed is true, then the if block gets executed. If not, the else block gets executed<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">if (condition) {\r\n  action1;\r\n}\r\nelse {\r\n  action2\r\n}<\/pre>\n<p><strong>iii. Else if statement<\/strong><\/p>\n<p>This statement encloses an if statement in an else block.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">if (condition) {\r\n  action 1\r\n}\r\nelse if (condition2) {\r\n  action 2\r\n}<\/pre>\n<p><strong>iv. Switch case<\/strong><\/p>\n<p>The switch case is used for multiple condition checking. It&#8217;s based on the value of the variable. The value of the variable mentioned marks the flow of the control to either of the case blocks mentioned.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">switch (var_name)\r\ncase value1:\r\n  action1;\r\n  break;\r\ncase value2:\r\n  action2;\r\n  break;\r\ndefault:\r\n  action3;\r\n  break;<\/pre>\n<h4>b. Iteration Statements in Java<\/h4>\n<p>These are the statements which are primarily known as loops in programming, which run a particular set of programs a fixed number of times,<\/p>\n<p><strong>Some of the types of iterative statements are:<\/strong><\/p>\n<p><strong>i. For loop in Java<\/strong><\/p>\n<p>The for loop is responsible for running the snippet of code inside it for a predetermined number of times.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">for (i = 0; i &lt; 5; i++) {\r\n  System.out.println(\u201cHi\u201d);\r\n}<\/pre>\n<p>This prints \u201cHi\u201d 5 times on the output screen<\/p>\n<p><strong>ii. While loop in Java<\/strong><\/p>\n<p>This type of loop runs indefinitely until the condition is false<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">while (i &lt; 6) {\r\n  System.out.println(\u201cHi\u201d);\r\n  i++;\r\n}<\/pre>\n<p>This prints Hi on the screen five times until the value of i becomes 6<\/p>\n<p><strong>iii. Do-while loop in Java<\/strong><\/p>\n<p>This is the same as the while loop. The only difference lies in the fact that the execution occurs once, even if the condition is false.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">do {\r\n  System.out.println(\u201cHi\u201d);\r\n}\r\nwhile ( i &gt; 6 );<\/pre>\n<h4>c. Control Flow Statements\/Jump Statements in Java<\/h4>\n<p>Sometimes we need to discontinue a loop during execution.<\/p>\n<p><strong>i. Break statement in Java<\/strong><\/p>\n<p>This breaks the nearest loop inside which is mentioned. The execution continues from the next line just when the current scope ends.<\/p>\n<p><strong>ii. Continue Statement in Java<\/strong><\/p>\n<p>This continues the execution from the next iteration of the loop and skips the current execution.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">while (i &lt; 10) {\r\n  if (i == 3) continue;\r\n  i++;\r\n}<\/pre>\n<p>This prints all the values from 0 to 9 except 3<\/p>\n<p><strong>iii. Return statement in Java<\/strong><\/p>\n<p>The return statements are generally useful in methods when returning a value when the function is done executing. After the return statement executes, the remaining function does not execute.<\/p>\n<h3>Exception Handling in Java<\/h3>\n<p>Exception handling is important to custom output the errors in the unfortunate case of an error occurrence. The syntax of the exception handling is fairly simple and structured.<\/p>\n<p><strong>It goes as follows:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">try {\r\n  \/\/ Code block within which error can occur\r\n}\r\ncatch(Exception e) {\r\n  \/\/Code block to perform when error thrown\r\n}\r\nFinally {\r\n  \/\/Code to be executed after the end of try block. This block is executed even if there is no error\r\n}<\/pre>\n<p>There is a special keyword called <strong>throws, it is<\/strong> useful to throw custom exceptions.<\/p>\n<p>For Example, <strong>throw new ArithmeticException();<\/strong><\/p>\n<p><strong>Try:<\/strong> This block houses the code that is responsible for an error thrown. Generally, programmers enclose the code that they think may throw an error in the try block.<\/p>\n<p><strong>Catch:<\/strong> This block houses the code to be performed when a particular exception is found. There can be custom messages defining what kind of error has occurred for better documentation and flow of the program.<\/p>\n<p><strong>Finally:<\/strong> The finally block executes whether or not there is any error faced by the compiler. This part is generally used to enclose the code that has to be executed irrespective of the errors occurred during compilation\/execution of the program.<\/p>\n<p>Example program to evaluate exception handling in Java<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import java.io. * ;\r\nclass ExcptionHandle {\r\n  public static void main(String[] args) throws IOException {\r\n    int a = 10,\r\n    b = 0;\r\n    int c;\r\n    try {\r\n      c = a \/ b;\r\n    } catch(Exception e) {\r\n      System.out.println(e);\r\n      \/\/TODO: handle exception\r\n    }\r\n  }<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">java.lang.ArithmeticException: \/ by zero<\/div>\n<h3>Operators in Java<\/h3>\n<p>As the name suggests, operators are the ones for performing operations on two or more entities. They are of multiple types as follows:<\/p>\n<h4>a. Arithmetic Operators in Java<\/h4>\n<p>These are the operators that are solely for performing arithmetic operations. These include addition (+), subtraction(-), multiplication (*), division (\/), modulo(%) and many more.<\/p>\n<h4>b. Relational Operators in Java<\/h4>\n<p>These are the operators that obtain the relation between two different entities in a program. These include less than(&lt;), greater than(&gt;), less than or equals to(&lt;=), greater than or equals to(&gt;=) ,equals to(==), not equals to(!=)<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">if (a &lt; b) {\r\n  System.out.println(\u201cA greater\u201d);\r\n}<\/pre>\n<h4>c. Bitwise Operators in Java<\/h4>\n<p>These operators are useful for performing bitwise operations on an entity. These include AND(&amp;), bitwise OR(|), bitwise XOR(^), bitwise complement (~), bitwise left shift(&lt;&lt;) and so on.<\/p>\n<p><strong>Example:<\/strong> (A&amp;B) will give 12 if a = 0000 and b= 1100<\/p>\n<h4>d. Logical Operators in Java<\/h4>\n<p>These operators are useful to check the logic of a particular operation of two operands. These include Logical AND(&amp;&amp;), Logical OR(||), logical NOT(!) and so on.<\/p>\n<p><strong>For Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">if (a == 6 &amp;&amp; b == 5)<\/pre>\n<h4>e. Assignment Operators in Java<\/h4>\n<p>These operators are responsible for assigning variables to variables. These include equal(=),add AND(+=), subtract AND operator(-=) and so on.<br \/>\n<strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">int x = 65;\r\nint y += 6<\/pre>\n<p>int y+=6(equivalent to int y=y+6;)<\/p>\n<h3>Object in Java<\/h3>\n<p>Objects are created from classes in Java. Once we define a class, we can create an object of the class using the following simple syntax. These are the instances of the class:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt; class - name &gt; &lt;object - name &gt; =new &lt; class - name of instance creation &gt; ()<\/pre>\n<p><strong>Example of a Java object:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">DataFlair java = new DataFlair();<\/pre>\n<h3>Class in Java<\/h3>\n<p>Classes generally start with the class name, which has its first letter capital. Generally, the case is CamelCase for class names. It has a very simple syntax as follows:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">class &lt; Class - name &gt; {\r\n  instance variables;\r\n  class method1() {}\r\n  class method2() {}\r\n} \/\/end class\r\n<\/pre>\n<p><strong>Example of a Java class:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">class DataFlair {\r\n  int a;\r\n  void teach() {\r\n    System.out.println(\u201cLearning java from DataFlair\u201d);\r\n  }\r\n}<\/pre>\n<h3>Methods in Java<\/h3>\n<p>Methods or functions are specific entities that return a value only when they are called. They have a syntax similar to classes.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;\r\nreturn type &gt; &lt;\r\nfunction - name &gt; {\r\n  action1;\r\n  action2;\r\n}<\/pre>\n<p><strong>Example Methods in Java:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">void print() {\r\n  System.out.println(\u201chey I am learning Java at DataFlair\u201d);\r\n}<\/pre>\n<h3>Java Interfaces<\/h3>\n<p>Interfaces are a collection of abstract methods in Java. We will learn more about Java in the following articles. We define interfaces as follows;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">interface &lt; interface - name &gt; {\r\n  static functions;\r\n  abstract methods;\r\n}<\/pre>\n<p><strong>Example of Java Interfaces:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">interface DataFlair {\r\n  void teach();\r\n  static void evaluate();\r\n}<\/pre>\n<h3>Access modifiers in Java<\/h3>\n<p>Access modifiers, as the name suggests, limit the access of the entities they are defined with. The access modifiers used by Java are:<\/p>\n<p><strong>a. Public<\/strong> &#8211; Accessible to every other class or interface. There is no restriction on access.<\/p>\n<p><strong>b. Private<\/strong>&#8211; This keyword renders all entities to be accessible only inside the class in which they are declared.<\/p>\n<p><strong>c. Protected<\/strong>&#8211; The protected members of the class are accessible to classes within the same package or subclasses of different packages.<\/p>\n<p><strong>d. Default<\/strong>&#8211; If no access modifier is mentioned, then the default access modifier is invoked. This limits the access of the particular entity within the same package.<\/p>\n<p><strong>For Example:<\/strong><br \/>\npublic int a=8;<\/p>\n<h3>Arrays in Java<\/h3>\n<p>Arrays are consecutive data items of the same datatype. They have a fairly simple syntax of declaration.<\/p>\n<p>If the array has to be declared explicitly, it has the syntax of:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt; data type &gt; &lt;array - name &gt; [ &lt; array - size &gt; ] = {\r\n  data1,\r\n  data2,\r\n  data3...\r\n};<\/pre>\n<p><strong>Example of Java Arrays:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">int arr[3] = {\r\n  1,\r\n  2,\r\n  3\r\n};<\/pre>\n<p>else if the array has to be declared during runtime.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt; data type &gt; &lt;array - name &gt; [] = new &lt; array - name &gt; [ &lt; sizeofArray &gt; ]<\/pre>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">int arr[] = new int[10];<\/pre>\n<h3>Variables in Java<\/h3>\n<p>The variables concept has been explained in the following articles, the syntax of variables is simple and easy to learn.<\/p>\n<p><strong>Java Syntax for variables:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt; data type &gt; &lt;variable - name &gt;<\/pre>\n<p><strong>Example of Java variables:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">String s = \u201dDataFlair\u201d;<\/pre>\n<h3>Datatype<\/h3>\n<p>The datatypes come before variables to define the type of data they would be storing. These include int,short,byte,float,double;<\/p>\n<p><strong>Syntax<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt; datatype &gt; &lt;\r\nvar - name &gt;<\/pre>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">int b = 12;<\/pre>\n<h3>Compiling and Executing a Java Program<\/h3>\n<p>Once we have written a Java program and saved it, we need to compile and execute it using the following methods.<\/p>\n<ul>\n<li>Open up a CMD window on the saved location by &lt;Shift+right-click&gt; and then select your configured CLI, i,e CMD or PowerShell. If you are using any other OS like Ubuntu or Linux, open up a terminal and navigate to the directory in which you have saved the Java program.<\/li>\n<li>Next, type in<strong> javac &lt;filename.java&gt;<\/strong><\/li>\n<li>follow that by typing <strong>java &lt;filename&gt;<\/strong><\/li>\n<\/ul>\n<p><strong>Example:<\/strong><\/p>\n<p>javac DataFlair.java (This compiles the file and lets us know if there are errors. )<br \/>\njava DataFlair (if no errors are found, run this command in CLI)<\/p>\n<h4>Java Apps: Driving a Range of Solutions<\/h4>\n<p>Java&#8217;s flexibility goes beyond syntax, allowing it to be used in a multitude of contexts. These are a few well-known examples:<\/p>\n<p>1. Word processors, spreadsheets, and games are just a few examples of the easily navigable desktop apps that Java can produce.<\/p>\n<p><strong>2. Web Applications:<\/strong> A large percentage of web applications use Java on the client (with Java applets for interactive elements) and server (with Java servlets and JSP).<\/p>\n<p><strong>3. Android mobile applications:<\/strong> Java is a key component of the Android development platform, so knowing it is a must for Android app developers.<\/p>\n<p><strong>4. Cloud Computing:<\/strong> Java works well for cloud-based applications because of its portability and scalability. Strong support for Java deployments is offered by numerous cloud platforms.<\/p>\n<h3>Summary<\/h3>\n<p>Syntax is important as they are the language that the compiler understands. If the syntax is incorrect, even the fastest algorithms can come to a standstill. So we must strengthen our concepts of Java syntax before proceeding further into the concepts of Java.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this Java Tutorial, we will learn about Java Syntax. So let&#8217;s start!!! What does Syntax mean? Well, simply put, syntax is a particular format for writing commands in a programming language. Every language&#46;&#46;&#46;<\/p>\n","protected":false},"author":7,"featured_media":78473,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[1652,1653,2966,7447,7624,7646,8153,10175,14040],"class_list":["post-6161","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-basic-java","tag-basic-java-syntax","tag-control-statement-in-java","tag-java-control-statements","tag-java-operators","tag-java-program-structure","tag-learn-java-syntax","tag-program-structure-in-java","tag-syntax-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java Syntax - A Complete Guide to Master Java - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn the basic syntax of Java to master the Java programming language. Explore the Operators, keywords, and literals with examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/basic-java-syntax\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Syntax - A Complete Guide to Master Java - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn the basic syntax of Java to master the Java programming language. Explore the Operators, keywords, and literals with examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/basic-java-syntax\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-01-20T02:18:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-05T10:23:46+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Java-Syntax.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java Syntax - A Complete Guide to Master Java - DataFlair","description":"Learn the basic syntax of Java to master the Java programming language. Explore the Operators, keywords, and literals with examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/basic-java-syntax\/","og_locale":"en_US","og_type":"article","og_title":"Java Syntax - A Complete Guide to Master Java - DataFlair","og_description":"Learn the basic syntax of Java to master the Java programming language. Explore the Operators, keywords, and literals with examples.","og_url":"https:\/\/data-flair.training\/blogs\/basic-java-syntax\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-01-20T02:18:30+00:00","article_modified_time":"2026-05-05T10:23:46+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Java-Syntax.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/basic-java-syntax\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/basic-java-syntax\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/beb0cab24b7aa54423a3b50e669a9dcd"},"headline":"Java Syntax &#8211; A Complete Guide to Master Java","datePublished":"2018-01-20T02:18:30+00:00","dateModified":"2026-05-05T10:23:46+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/basic-java-syntax\/"},"wordCount":2348,"commentCount":12,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/basic-java-syntax\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Java-Syntax.jpg","keywords":["basic java","basic java syntax","control statement in java","java control statements","java operators","java program structure","learn java syntax","program structure in java","syntax in java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/basic-java-syntax\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/basic-java-syntax\/","url":"https:\/\/data-flair.training\/blogs\/basic-java-syntax\/","name":"Java Syntax - A Complete Guide to Master Java - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/basic-java-syntax\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/basic-java-syntax\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Java-Syntax.jpg","datePublished":"2018-01-20T02:18:30+00:00","dateModified":"2026-05-05T10:23:46+00:00","description":"Learn the basic syntax of Java to master the Java programming language. Explore the Operators, keywords, and literals with examples.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/basic-java-syntax\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/basic-java-syntax\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/basic-java-syntax\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Java-Syntax.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Java-Syntax.jpg","width":1200,"height":628,"caption":"Java Syntax"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/basic-java-syntax\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Java Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/java\/"},{"@type":"ListItem","position":3,"name":"Java Syntax &#8211; A Complete Guide to Master Java"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/beb0cab24b7aa54423a3b50e669a9dcd","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team specializes in creating clear, actionable content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Backed by industry expertise, we make learning easy and career-oriented for beginners and pros alike.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam3\/"}]}},"amp_enabled":false,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/6161","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=6161"}],"version-history":[{"count":28,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/6161\/revisions"}],"predecessor-version":[{"id":148216,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/6161\/revisions\/148216"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/78473"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=6161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=6161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=6161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}