Syntax

Introduction: Welcome to `Basic Syntax` for Java beginners. This tutorial will teach you the correct format your code should be in as well as what some syntax related errors mean and how they may occur. What is Syntax: Syntax is the arrangement or format of your code. The way your code is laid out will not generally affect the performance of your program. However, if you have some in-correct syntax within your scripts this will cause a problem (most of the time it will cause a crash). What Should Syntax Look Like? In Java there are a few things to remember when thinking about Syntax: - Java uses a line terminator (;) to end lines which are NOT; a statement, a loop, empty (including "{" or "}"), method/constructor top line. - Braces are used to surround blocks of code ("{" and "}"). - Case Sensitive Why is Good Syntax Important? Syntax is a very important part of scripting. If you have the wrong syntax within your programs this can cause anything from a single minor problem to a major crash and quit. Of course, if you have good syntax and very efficient scripting your program will complete it's tasks faster and run smoother where as having a ton of bad code will cause quite bad performance problems. Good Syntax Example: There are so many variations of how Syntax can be correct, but let's use the simple script from the first tutorial when we 'set up' the Java environment.
  1. public class Main {
  2. public static void main(String args[]) {
  3. System.out.println("Testing output.");
  4. }
  5. }
Bad Syntax Example: Can you see where the two syntax errors are in the example below?
  1. Public Class Main {
  2. public static void main(String args[]) {
  3. System.out.println("Testing output.")
  4. }
  5. }
Spotted them yet? Remember the syntax rules, most lines must end with a terminator (;), braces are used to encase blocks and keywords are case sensitive! The "Public Class" should not be capitalized and there is no terminator at the end of the console output line "System.out.println("Testing output.")". Finished! Now that you know the syntax rules, you are ready to move on!

Add new comment