Java Syntax

Java is a case sensitive language

That means for Java Compiler ‘Maradona’ and ‘maradona’ are different.

Class Name Rule/Convention

While writing code in eclipse the words which will take the color violate ( by default violate, there are ways to change the setting, but that is for styling purpose, just don’t think about those evil things ) is the key words. In the code above the key words are: public, class, static and void. Java has set of key words in its store, which you cannot use for any other purpose.  ( we will tell you what does this means)

The class name for your code should start with a uppercase letter. For example try to create a class with the name ‘myJava’, eclipse will tell you the name is invalid. Only the first letter should be in uppercase, so you are free to create a java class with the name ‘Myjava’. But since logically My and Java are two separate word it is a good practice to use uppercase while starting a new word. But compiler only understands rule, for that it will allow you to create class with the name ‘Myjava’.

If you want to use numbers (not at all a good practice) in the class name, you can do it but the class name should start with a uppercase letter. For example, MyJava1 is a valid class name, whereas 1MyJava is not valid.

Method Name Rule/Convention

For method name also, you cannot use numbers as the first character. So ‘1MyMthod’ is not a valid name for a method whereas ‘Mymethod1’ is a valid one. Unlike class name, method name need not to be started with an uppercase character. Therefore, ‘mymethod1’ is a completely valid method name.

May be you have noticed already, that the file is created with the same name as the class name. We have mentioned this in the first tutorial also. In fact in single file there can be multiple class , but only one of them is allowed to be public . You have seen that in our example the Learnjava class is written as ‘public class LearnJava’. The file name should match with the public class name. For example if you try (of course you won’t ) to change the file name to something else , eclipse will not allow you to do.

Every java program should start with: public static void main(String args[]). We will come later into details of the declaration.

A simple java program basically consists of class, method , variables( will be introduced shortly) etc , and all of them requires names. The names are called identifiers.

There are some basic rules for identifiers

  • All identifiers should begin with a letter only, currency character ($) or an underscore (_), but never with a numbers or sign ( +/-).
  • You cannot use a keyword as an identifier, we mentioned it above: do not use key word for any other purpose; keywords are reserved word for Java.

Modifiers in Java

Java has some modifiers, mainly divided into Access and Non-access Modifiers.

Access Modifiers

public, protected, private

Non-access Modifiers

final, abstract

You can use these modifiers before a class or method or variable names . In our example we have written :

public class LearnJava

Why we have done it, and what features does this modifiers provide us, we will this see later.

Java Keywords

Like our programs had four keywords, let’s have a look into the complete list of keywords java kept reserved:

abstract continue for new switch
assert default goto package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp volatile
const float native super while


Blank lines

What if you leave a line blank? either accidentally or for making your code better readable? Do not worry, compiler ignores blank line, so it is of no harm and very much useful sometime to use  blank like between code lines.

Java Comments

Remember you have modified the code to make it look like this?


 * My journey becomes here towards becoming a Java Expert.
public class LearnJava
	public static void main(String []args) // main method 
        	System.out.println("In life, sooner or later,  you have to learn Java"); 
        	// output to console

The proud statement which have written inside /** and */ is called comments in java. Java compiler ignores this part. Anything written between **/ and */ is for your own understanding purpose, it has no syntactical constraint. So you can write anything, even you can use any java keyword, as compiler is not sneaking into that part.

What about  '// main method' ? Same logic applies here, compiler completely ignores this part also. Or in other way, you are telling the compiler: bro, leave this part alone, it is not for you, it is for me and my fellow coder friend’s understanding purpose.

These statements which is written between /** and */ or after // is called Java comment. 

In the next tutorial, we will learn about Java Classes and Objects.

Next > < Prev
Scroll to Top