Java Syntax

Before we started discussing the actual syntax, let’s discuss how Java handles its code.

When someone runs a Java file, the first thing it does is to set up a JRE (Java Runtime Environment). As the name implies, it’s an environment that allows the Java code to run in it.

Furthermore, it also provides a layer of security for the user, as the Java code is not allowed to make changes to the PC outside the JRE. This prevents any malicious hackers gaining control of your system.

Now moving onto how Java creates portable code that runs on any platform. Java does this by using it’s compiler to generate bytecode from the actual code. Bytecode is a highly optimized set of instructions designed to be executed by the JVM (Java Virtual Machine).

The result of all this is that to run Java on a platform, all you require is a JRE. Since all JRE’s understand the same bytecode, there are no portability issues.

If haven’t read our introductory article Getting started with Java, you won’t be able to build the same level of understanding!

Java files names

For most programming languages, the name of the file that holds the source code to a program is arbitrary. However, this is not the case with Java. In Java, the name of the main class should match that of the source file. Even the capitalization of the names must match.

Furthermore, all the Java files must be saved with the .java extension.

Case sensitivity in Java Syntax

Like many programming languages Java is also case sensitive. This effects everything down to the name of files (as pointed out above) and the names of variables used in the program.

For instance, Dog and dog are treated as two different variables. To keep things easy while coding, it’s recommended to code only in lowercase.

Java Comments

Comments are useful pieces of explanatory text added alongside the code. It can serve as a useful reminder regarding the use of several variables, or the purpose of a specific block of code.

Using double slash characters can be used to declare a single line comment.

//This is a comment

You can write whole paragraphs using the multi line comments characters. Any text you may want to include comes between the /* and */ characters.

/* This is 
a multi line
comment in 
Java */

Java main() method

After you’ve declared a main class, you’ll have to declare the following line in your code. This is the line at which the program will begin executing.

The entire program is contained within the curly brackets.

public static void main (String args[]) {
    // Program

The keyword void tells the compiler that main( ) is not meant to return a value. The keyword public declares it’s access level. Keeping it private for instance would not allow anything outside the class to access it, which would cause issues. Hence we keep it public, so it can be accessed by anyone.

The exact meaning of this line cannot be explained without further knowledge of Java’s concepts. Hence this is where we stop.

Java Output

Since output is Java is not large enough of a topic to be given a separate page, it will be discussed here. Every programming has it’s own output statement. Most commonly you’ll see the print command. However, Java uses the following code.

System.out.println("Output in Java");

Remember to include the semi colon. All Java statements must end with a semi colon, else a syntax error is raised.

Java Example

To top it all off, we’re including a small example code from one of our Java articles.

You can see clearly the main() wrapper around the code like we mentioned earlier. There’s also the first line, which is public class example, a line compulsory in all Java programs. For now just remember to write this every time you begin (assuming your IDE doesn’t do it for you). Only difference is that example will be swapped out for the name of your file. The name of the file and class must be the same.

public class example {
	public static void main(String[] args) 	{	
		int x = 0; 
		int y = 0;
		do  {
		    x = x + 1;
		    y = y + 2;
		    System.out.println("X: " + x + " " + "Y: " + y);
		while (x < 5 && y < 6);    		     			

Other than that, you can see that we’ve kept all the variables in lower case and that certain lines end with a semi colon (you’ll get the hang of it pretty quick) and we’ve kept everything properly indented to increase readability.

This marks the end of the Java syntax Article. If you have suggestions or contributions for CodersLegacy, please don’t hesitate. Any questions about the article can be asked in the comments below.

Notify of
Inline Feedbacks
View all comments