Basics of Java syntax

When you step back from the very technical side of programming languages, most programming languages are based on the same principles and native functionality that help the programmer achieve their goal. The most obvious difference between languages, is the syntax. The syntax of a programming language is, basically, the rules for how programmers need to structure their program in order for the compiler or interpreter to understand the code at all.


Coming into programming and seeing a bunch of strange placements of brackets and semicolons everywhere can seem a bit intimidating at first but if you understand what these things are, the language comes much more enjoyable. That is the goal of this section; to teach you the basics of Java syntax so later lessons are not so intimidating.


	public class HelloWorld {

		public static void main(String[] args){

			

			// Display message to console

			System.out.println("Hello, World!");

			

		}

	}

We will use this code as an example of basic Java syntax thoughout this lesson.

This is the most basic program you're likely to create. All this program does is displays the text, "Hello, World!" in the console. There are a few main things you need to notice about this syntax.

Note: the line numbers are just for reference purposes and in no way effect the code.

Class and main()

Starting from line 1, you define a class. You are required to have at least one class for any Java program. In line 2, you declare the main() method. You do not need to have a main method for every class you create. However, you do need a least one main method somewhere in your program. Deciding when and where to use a main method is out of the scope of this lesson. For now, we will use a main method in every class we create.


You can think of the class as a car and you have one main method that the car does, drive. So, if you were to represent that car in a java program, it would look like this:


	public class Car {

		public static void main(String[] args){

			

			// Code that makes the car drive goes here

			

		}

	}

This is a class, named "Car", with a main function. The code that will make the car drive, goes inside of this main method.


In Java syntax, you define the bounds of a class and method with braces "{" and "}"