JAVA Basic Syntax

In this tutorial we discuss do class, object, methods, and instance variables mean.

Java Program Example

Let us look at a simple code that will print the words Hello Java.

public class JavaProgramExample {

   /* This is my first java program.
    * This will print 'Hello Java' as the output
    */
   public static void main(String []args) {
      System.out.println("Hello Java"); // prints Hello Java
   }
}

Let’s look at how to save the file, compile, and run the program. Please follow the subsequent steps:

  • Open notepad and add the code as above.
  • Save the file as: JavaProgramExample.java.
  • Open a command prompt window and go to the directory where you saved the class. Assume it’s C:\.
  • Type ‘javac JavaProgramExample.java’ and press enter to compile your code. If there are no errors in your code, the command prompt will take you to the next line (Assumption : The path variable is set).
  • Now, type ‘ java JavaProgramExample’ to run your program.
  • You will be able to see ‘ Hello Java ‘ printed on the window.

Output

Hello Java

Object have states and behaviors.Example: A cat has states – color, name, breed as well as behavior such as wagging their tail, maw-maw, eating. An object is an instance of a class.
Class can be defined as a template/blueprint that describes the behavior/state that the object of its type supports.
Methods A method is a behavior.It is in methods where the logics are written, data is manipulated and all the actions are executed.
Instance Variables Each object has its unique set of instance variables.

Basic Syntax

Class Names − For all class names the first letter should be in Upper Case. If several words are used to form a name of the class, each inner word’s first letter should be in Upper Case.

Example:

class JavaProgramExampleClass

Case Sensitivity − Java is case sensitive, which means identifier Java and java would have different meaning in Java.

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. examplepublic void myMethodName()

Program File Name − Name of the program file match the class name. exampleAssume ‘JavaProgramExample’ is the class name. Then the file should be saved as ‘JavaProgramExample.java’

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

Java Variables

Java variable are three type:

  • Local Variables
  • Class Variables
  • Instance Variables

Java Modifiers

By using java we can modify classes, methods, etc., by using modifiers.

  • Access Modifiers: default, public , protected, private.
  • Non-access Modifiers: final, abstract, strictfp.

Comments in Java

Single line comments can be written followed by “//”, whereas multi line comments has to be enclosed within “/*” and “*/” as shown in the example below:

public class MyExampleJavaProgram {

   /* This is my first java program.
    * This will print 'Hello Java' as the output
    * This is an example of multi-line comments.
    */
   public static void main(String []args) {
      // This is an example of single line comment
      /* This is also an example of single line comment. */      System.out.println("Hello Java");
   }
}

Output

Hello Java

Java Enums

Enums restrict a variable to have one of only a few predefined values. The values in this enumerated list are called enums. For example, if we consider an application for a fresh juice shop, it would be possible to restrict the glass size to good, better, and best. This would make sure that it would not allow anyone to order any size other than good, better, or best.

Example:

class FreshJuice {
   enum FreshJuiceSize{ GOOD, BETTER, BEST }
   FreshJuiceSize size;
}

public class FreshJuiceTest {

   public static void main(String args[]) {
      FreshJuice juice = new FreshJuice();
      juice.size = FreshJuice.FreshJuiceSize.BETTER ;
      System.out.println("Size: " + juice.size);
   }
}
Related Post