Basic Syntax Elements

In this tutorial, we'll go over the basics of Java syntax. Java syntax refers to the set of rules and conventions for writing programs in Java.

Case Sensitivity

Java is case-sensitive, which means that Hello and hello are different identifiers.

Class Names

For all class names, the first letter should be in Upper Camel Case. If several words are used to form the name of the class, each inner word's first letter should be in Upper Case.

Example: class MyFirstJavaClass

Method Names

All method names should start with a Lower Case letter. If several words are used to form the name of the method, then each inner word's first letter should be in Upper Case.

Example: public void myMethodName()

Program File Name

The name of the program file should exactly match the class name. When saving the file, you should save it using the class name (Remember Java is case-sensitive) and append .java to the end of the name (if the file is saved with a .java extension).

Example: Assume 'HelloWorld' is the class name. Then the file should be saved as HelloWorld.java.

main Method

Every Java application must contain a main method whose signature is:

java
1public static void main(String[] args)

This method is the entry point for the program and will subsequently invoke all the other methods required by your program.

Comments

Java supports single-line and multi-line comments very similar to C and C++. Comments in Java are ignored by the compiler and can be used to provide information or explanation about the variable, method, class, or any statement.

Example:

java
1// This is a single-line comment 2 3/* 4This is a multi-line comment 5that spans over multiple 6lines 7*/

Identifiers

All Java components require names. Names used for classes, variables, and methods are called identifiers.

  • All identifiers should begin with a letter (A to Z or a to z), currency character ($) or an underscore (_).
  • After the first character, identifiers can have any combination of characters.
  • A key thing to remember is that Java is case-sensitive, which means that identifier and Identifier would have different meaning in Java.

Modifiers

Java provides a number of access modifiers to set access levels for classes, variables, and methods. The four access levels are:

  • Visible to the package, the default. No modifiers are needed.
  • Visible to the class only (private).
  • Visible to the world (public).
  • Visible to the package and all subclasses (protected).

Java also has a number of non-access modifiers, such as static, abstract, synchronized, native, volatile, and transient.

Variables

Java programming language supports the following types of variables:

  • Local Variables: Variables defined inside methods, constructors, or blocks are called local variables. They are declared and initialized within the method and the variable will be destroyed when the method has completed.
  • Instance Variables (Non-static fields): Instance variables are variables within a class but outside any method. These variables are instantiated when the class is loaded.
  • Class Variables (Static fields): Class variables are variables declared with the static modifier within a class, but outside any method.

Arrays

In Java, arrays are objects that store multiple variables of the same type. However, the size of an array must be decided upon instantiation.

Example:

java
1int[] myIntArray = new int[10]; // Declares an array of integers. 2myIntArray[0] = 100; // Accesses the first element of the array and sets it to 100.

Enums

Enums were introduced in Java 5. Enums restrict a variable to have one of only a few predefined values. The values in this enumerated list are called enums.

Example:

java
1enum Color { 2 RED, GREEN, BLUE; 3} 4 5Color myVar = Color.RED; // myVar can only be one of RED, GREEN, or BLUE.

Naming Conventions

  • Classes: Class names should be nouns in UpperCamelCase, with the first letter of each separate word capitalized.
  • Methods: Methods should be verbs in lowerCamelCase; that is, the first letter is lower case and then each subsequent word starts with an uppercase letter.
  • Variables: Also in lowerCamelCase, but might have more descriptive names.
  • Constants: Should be in all uppercase letters with words separated by underscores (_).