JAVA BEGINNERS TUTORIAL – Syntax

A normal deffinaton of Java  program is a collection of objects that communicate by using each other’s methods. And:

  • Object:
    • In language
      • a material thing that can be seen and touched.
      • a person or thing to which a specified action or feeling is directed.
    • in Programming :
      • A lines of codes that have have states/properties and behaviors/methods. also it is an object is an instance of a class.
  • Class:
    • Defined as a template/ blueprint that describes the states/properties and behaviors/methods that object of its type/category support.
  • Methods:
    •  a smaller set of code lines that do an action as a behavior. A class may have many methods.
  • Instance Variables:
    • Evry object has its unique set of properties and the object’s state is the values of these instance variables.

 Back to Hello World example:

public class MyFirstApp {

    public static void main(String []args) {
       System.out.println("Hello World");
    }
}

this is the basic syntax only basic lines to print “Hello World” words on our sceen. now lets compile it and excute it and see the expected result.

sami-mbp:Java sami$ javac MyFirstApp.java    
sami-mbp:Java sami$ java MyFirstApp
Hello World

I am using my Macbook pro machine but the same commands can be used in Linux terminal or Windows DOS commandline.

About Java programs, it is very important to keep in mind the following points.

  • Case Sensitivity – Java is case sensitive, which means identifier Hello and hello would have different meaning in Java.
  • Class Names – For all class names the first letter should be in Upper Case.
  • Method Names – All method names should start with a Lower Case letter. (camel case)
  • Application File Name – Name of the application file should match the class name.
  • public static void main(String args[]) – Java program processing starts from the main() method which is a mandatory part of every Java application.

Identifiers:

Names used for classes, variables and methods are called identifiers. basically:

  • identifiers have to begin with (A to Z or a to z), currency character ($) or an underscore (_). then any combination of characters.
  • identifiers are case sensitive.
  • Examples: name, $position, _valid, __2nd_value

Modifiers:

generaly it can be defines as a person or thing that makes partial or minor changes to something. in java is is defined as a keyword placed in a class, method or variable declaration that changes how it operates.

  • Access Modifiers: default, public , protected, private
  • Non-access Modifiers: final, abstract, strictfp, native, static, synchronized, transient, and volatile.

​we will have many examples within this Tutorial.

Variables:

A variable is a container that holds values that are used in a Java application. and it have many types:

  • Local Variables
  • Class Variables (Static Variables)
  • Instance Variables (Non-static variables)

Arrays:

An array, in Java, is a dynamically-created object that serves as a container to hold constant number of values of the same type. By declaring an array, memory space is allocated for values of a particular type. At the time of creation, the length of the array must be specified and remains constant.

Enums:

An enum, or enumeration type, is a type whose fields consist of a fixed set of constants. The very basic purpose of enums is to enforce compile time type safety. (in general) are generally a set of related constants. They have been in other programming languages like C++ from beginning. After JDK 1.4, java designers decided to support it in java also, and it was officially released in JDK 1.5 release.For example, if we consider an application for a coffee shop, it would be possible to restrict the cup size to small, medium and large. This would make sure that it would not allow anyone to order any size other these.

Java Keywords:

The following list shows the reserved words in Java. These reserved words may not be used as constant or variable or any other identifier names.

abstract, assert, boolean, break, byte, case, catch, char, class, const, continue, default, do, double, else, enum, extends, final, finally, float, for, goto, if, implements, import, instanceof, int, interface, long, native, new, package, private, protected, public, return, short, static, strictfp, super, switch, synchronized, this, throw, throws, transient, try, void, volatile, while

maybe I forget something but it is ok as evry time you will compile the application it will be shown as error.

Comments in Java

Java supports single-line and multi-line comments very similar to c and c++. All characters available inside any comment are ignored by Java compiler.

public class MyFirstApp{

   /* This is my first java applicaiton.
    * This will print 'Hello World' 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 another single line comment. */
       System.out.println("Hello World"); 
    }
}

Blank Lines:

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

Comments on this post

No comments.

Leave a Reply

Your email address will not be published. Required fields are marked *

20 − sixteen =

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Trackbacks and Pinbacks on this post

No trackbacks.

TrackBack URL