top of page

Basic Syntax

Tutoring > Java Coding > Basic Syntax

Java is a case sensitive language, if you type String, this means something different than string.

 

Identifiers

An identifier is the name of a single element in some Java code.

An identifier in Java must:

  • not start with a number

  • not be a reserved keyword, NULL literal or boolean literal (see below)

Other than that, it can be anything.

 

Keywords

Keywords are reserved words that Java uses for certain purposes. You cannot use a keyword for anything other than what it is meant for.

 

Here are the Java keywords and what they mean, don't worry if the explanations don't make sense yet, we will look in detail at them later:

 

abstract - makes a class abstract

assert - 

 

break - 

 

case - 

catch -

class - denotes a class being declared.

const - Java does list "const" as a keyword although it isn't actually used. Constants are declared using the "final" keyword.

continue - used to continue execution of a program at the end of the current loop body.

 

default - 

do - used in conjunction with "while" in the "do-while" loop. The "do" part will execute the loop before the while part tests for a condition. 

double -

 

else - 

enum -

extends - used to inherit data and methods from a super class. More on this in Inheritance.

 

final - 

finally - 

float - used to declare a 32 bit variable of floating point type.

for - begins a "for loop".

 

 

new - creates a new object (an instance of a class).

switch - begins a switch statement.

 

 

goto - 

package - denotes which package the current class is in. This goes at the top of a class file, outside any braces {}.

synchronized - 

boolean - this is a variable type that can either be true or false.

While the condition evaluates to true, the loop will continue to execute.

if - begins an "if statement", a conditional loop

private - an access modifier preventing access to data from outside classes.

this - refers to the current instance of an object.

 

 

implements - used to implement an interface. More than one interface can be implemented by one class if needed.

protected - 

throw - 

byte - 

 

import - imports 

public - an access modifier meaning anything can access the data it is applied to from anywhere.

throws - 

 

 

instanceof - 

return - returns execution back to the caller. You can return a value or just nothing at all (just stop executing the current method and go back to what you were doing before).

transient - 

 

 

int - used to declare an integer type variable. To be more correct, a 32-bit signed two's complement integer. 

short - used to declare an integer variable of 16 bits. 

try - 

char - used to declare an integer variable of 8 bits.

 

interface - 

static - means you don't need an object created to use the variable/method.

void - returns nothing. Not zero, literally nothing.

 

 

long - 

strictfp - 

volatile - 

 

 

native - 

super - will inherit from the current classes super class.

while - begins the conditional "while loop" statement.

 

Literals

 

 

Comments

Comments are not executed, they are annotations in the code to clarify and explain it and are ignored by the compiler.

There are two ways to write comments in Java.

  • // This will start a single line comment, anything on this one line after the // will not be executed.

  • /* This starts a multi-line comment for when you need more room for annotations..........

         ..........and this will end the comment */

 

Whitespace 

Java does not recognise whitespace, it ignores it so you can put as many spaces or tabs as you like. It is recommended though that you adhere to conventional indentation formats, if you are using an IDE (integrated development environment) like Eclipse, you can get your code formatted automatically but generally you should indent every time you open curly braces {.

 

Here is a basic Hello World program:

 

public class MyFirstProgram{

    public static void main(String[] args) {

        System.out.print("Hello World!");

    }

}

 

We will look at each part seperately:

 

public class MyFirstProgram{

 

//This means we are declaring a class and naming it "MyFirstProgram". Everything between the curly braces {}

is what is called the class body

 

public is an access modifier, there are three types:

  • public

  • protected

  • private

 

class is the keyword which tells Java we are declaring a class, followed by the class name. At this stage, each class should have it's own file, you can have nested classes but more on that later.

 

public static void main(String[] args) {

 

// This is a "method". A method is simply a piece of code that performs some action on some data and then returns execution to the caller. The caller is what actually called or "invoked" the method to start with. This is the main methos, every program has one, it is the entry point for the program. The compiler looks for this main method first, and begins sequentially running through the lines of code from here.

 

The static part means we can execute the method without having to create an object of it's containing class. More on this in Objects.

 

When a method is declared, we need to declare a "return type". Here we return nothing, we just want to invoke the method and not end up with any particular value, so we use the keyword void.

 

The main method is the entry point for the program. This is the first line of code executed by the compiler.

 

We can pass arguments to our methods by declaring them in braces (). The main method takes an array of String variables called "args" as it's arguments. String[] means "an array of Strings", and args is the name we give this argument.

 

The method body is everything between the curly braces following the parenthesis ().

 

System.out.print("Hello World!");

 

This statement makes a call to the System class and uses the dot operator "." to specify which method to use, in this case, the println method, which prints out whatever is between the parenthesis () to the console. Here we print a string literal, basically whatever we put between the double quotation marks "...".

If we used println instead of print, the compiler would add a carriage return to the end of the printed statement, essentially starting a new line. This is useful for formatting.

 

The last few lines just close the curly braces for the main method and the class. Every opening bracket must have a closing one.

 

The result of running this code is that the console prints out this:

 

Hello World!

 

 

bottom of page