Learn Java from Scratch: Basic Syntax in Java Programming7 min read

If you have some experiences with other programming languages before, such as JavaScript, Python, etc…If you look at some Java program and as you can see in the first lesson, it’s quite a little bit strange and new to you. This article is about introducing to you some kind of syntax in Java, but first I want to give you a brief look at the definition of Class and Object. Java is an Object-Oriented-Programming language then Class and Object are indispensable parts of your Java program, and besides, there are some other crucial parts as well, such as Method and Variable. Now, let’s take a brief look at those concepts and see what they mean, and we will go deeper about Objects and Classes and Variables in some upcoming articles.

Object

In the real world, you might see many different objects which have states and behaviors. For example, a dog has states – name, color, and breed as well as behaviors – wagging the tail, barking, eating. In Java, the same principles for objects are applied the same as the real world, an object in Java has states and behaviors and object is an instance of a class.

Classes

Class in Java is defined as the blueprint or the template that an object can be described by. For better envision, let’s write a small program about Class and Object in Java.

public class Person{
    int age; // age is a state of an object, which is generated below in the main method
    String name; // name also is a state of an object, which is generated below in the main method
    int personID; // personId is an identity of an object, what makes an object unique.
    
    void attendJavaCourse() { // this is a behavior of an object might have, attend a Java Course.
        // some code 
    }
    public static void main(String[] args){
        Person aPerson = new Person(); // create a new object named aPerson from class Person.
    }
}

This small program above will not run, because rather write the whole program to run this code, I just write down and make notes for you something important at the moment. In this snippet, I have demonstrated the concept of a class, as you can see we have a class called Person work as a blueprint when you creating a new object. When an object is generated, it will have some states and behaviors and also identities which have been declared in the class Person.

Here we have several states which are age and name, and one thing I haven’t talked about yet is an identity which makes an object unique to others, an identity is often implemented with a student id, social security number, employee id, whatever that make this object unique when comparing with other objects. Next, we have a behavior of a person might have, which is attendJavaCourse which tells us what is objects do. Then finally, in the main method, we create a new instance of class Person, which is an object named aPerson.

Methods

In Java, method is a behavior of an object that might have, and a class can have many different methods. A method is a block of code which runs when it’s called, methods usually perform an action, the logic of a program and manipulating data. As you can see above, we already wrote a method in the class Person, which is attendJavaCourse, it will do some action when it’s called in an object.

// this is a method which defines some kind of behaviors of objects that might have
void attendJavaCourse() {
        // some code 
}

Instance Variables

In Java, instance variables are variables defined in a class, but outside of any methods, we already saw some instance variables which are age and name inside the Person class. Those variables are defined outside of any methods and inside a class Person.

public class Person {
    int age; // instance variable
    String name; // instance variable
    void myMethodName() {
       // do something
    }
}

If a variable is defined in a method, this is not an instance variable, for instance:

public class Person {
    int age; // instance variable
    String name; // instance variable
    void myMethodName() {
       int number; // not an instance variable
    }
}

We can sum up there are 3 kinds of variables, which are a local variable, instance variable, and static variable. However, as being said before, we talk about it later.

Basic Syntax in Java

Commenting in Java

Commenting on your code explain what the code is doing. Java supports both single and multi-line comments. All characters that appear within a comment are ignored by the Java compiler.

Single line comment:

int age; // this is a single line comment

Multiple lines comment:

/ *
This is also a comment, 
but in mutiple lines.
*/

/**
Comment for Java documents
*/

A line containing only white space, possibly with a comment, is known as a blank line, and Java totally ignores it.

Case Sensitivity 

Java is case sensitive, which means identifier Hello and hello would have a different meaning in Java.

Class Names

For class naming convention in Java, the first letter of a class should be in uppercase, and each inner word after the first one always should in uppercase, for example:

class MyFirstJavaProgram

Variable and Method Names

In Java and many other languages, there is a convention that all the method names and variable names should start with a lower case letter. If there are several words in your method and variable names, each word after the first one should be in upper case, for example:

void myMethodName() {} // this is a valid method name
int thisIsAnAge; // this is a valid variable name

Program File Name 

I have talked about the program file name in the very first lesson, we know that the name of the program should be exactly the same as the public class name and your program file should append .java extension otherwise without .java the program will not compile. But remember that it is not mandatory to have a public class in the file.

public static void main(String args[]) − Java program processing starts from the main() method which is a mandatory part of every Java program.

Java Identifiers

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

In Java, there are several points to remember about identifiers. They are as follows:

  • All identifiers should be started with a letter (from A to Z or a to z), a dollar symbol or an underscore (_)
  • After the first character, identifiers can have any combination of characters, you can even declare variables such as myName123.
  • A keyword cannot be used as an identifier, which is listed below.
  • Most importantly, identifiers are case sensitive.
  • Examples of legal identifiers: age, $salary, _value, __1_value.
  • Examples of illegal identifiers: 123abc, -salary.

Java Modifiers

Like other languages, it is possible to modify classes, methods, etc., by using modifiers. There are two categories of modifiers −

Java Keywords

The following list shows the reserved words in Java. You cannot use those keywords as a variable, or constant or any other identifier names.

abstractassertbooleanbreak
bytecasecatchchar
classconstcontinuedefault
dodoubleelseenum
extendsfinalfinallyfloat
forgotoifimplements
importinstanceofintinterface
longnativenewpackage
privateprotectedpublicreturn
shortstaticstrictfpsuper
switchsynchronizedthisthrow
throwstransienttryvoid
volatilewhile
Previous Article
Next Article
Every support is much appreciated ❤️

Buy Me a Coffee