Basic Java Syntax and Rules

Basic Java Syntax

If we talk about the Java syntax, Than a basic will cover a Object name referring to an instance with a dot operator that separates the Object from message. "Message" in above sentence refers to fields or methods accessible to object. Always define the method name keeping in mind what it does. All statements are terminated by a semi-colon sign(;).

What is a Method

You can define it is a measurement unit code. It is a smallest unit of code. You can add comments before defining the method. It have following properties
  • Has a name.
  • Defines the parameters that are passed and their data type.
  • Define the visibility - Controlled by access modifiers like public, private, protected.
  • Defines the Behavior and what it is going to return, if someone access it.


Comments in Java can be Single line as well as Multi-line also. Single line comment starts with a two forward slashes(//) and ends at line end. Multi-line comment starts with a forward sign and an asterisk(/*) and ends with an asterisk and a forward slash(*/).

Java Statement
They are building blocks of code. Statement must be terminated by a semi-colon(;). A statement can be
  • An empty statement(Ex only ;).
  • Instance creation.
  • Variable assignment.
  • Operators that work on data.
  • Messages being send to object.
  • If block or while, for loop.

What is Block
Block is a group of statements defined between two curly braces({}). Block start with { and ends with }. Every statement inside a block is terminated by semi-colon.

Java Package
A Package is Java language component that contains classes. It is a directory structure, dots in package name defines the order of directory.

Java Class
To create a Java class either use an IDE(like Eclipse, Netbeans etc) or use any test editor. Only one public class is allowed per file. To save the Class remember File name is same as the public class name with .java as extension. To compile the file run the compiler javac.exe with source code file in a command prompt window.

If the file compiles without any errors then you will see another file name in same directory with .class as extension.


More Java Stories
Convert String Array to Arraylist



No comments:

Post a Comment