.

Java Tutorials: Java Basic concept and rules

Basic concept and rules

When we look into java program it is basically using the concept of classNameand object. So now we concentrate on the class, object and method.

Class:

A classNameis a user defined blueprint which contains the entire set of similar data and functions that the object possess.

Object:

An object carries instances and methods. In real world we may see many objects like house, car, dog etc. the object of classNameis known as instance.

Method:

A method is a logical operation written by programmer. A classNamemay have many method that is depends on the all the actions are executed.

First Java program:

Now we consider simple code that will print first program.

Example:

basic concept and rules img1

Steps for saving compiling  and executing the java program:

  • Type code in a notepad
  • Save this file as MyFirstPgm.Java.
  • Using command prompt compile the program using following syntax “javac MyFirstPgm.Java”.
  • If there is no error using this syntax for run your program “java Java” .
  • Now you can see your output as ‘Welcome to Java World.

Output:

C:\>javac MyFirstProgram.java

C:\> java MyFirstProgram

First Program 

Key points must remember while creating your program

  • classNameName – first letter of classNameand inner word must be Uppercase.
  • Ex:  classNameMyFirstProgram

  • Method Name - first letter of method must be lower case and inner word must be Uppercase.
  • Ex: public firstMethodname()

  • File Name – file name and classNamename must be same. If more than one classNameused for your program means the main classNamename you have to use. If it is not match the compiler will not compile it. It also case sensitive.
  • Ex: if 'MyFirstPgm' is the classname , it is must to save  program as ‘MyFirstPgm.java’   

  • main method () – Every program must have main() method because the program begins its implementation starting from the same method which is represented as follows
  • Ex: public static void main (String[]args)
    • public: public which is open access
    • static: static function requires no object
    • void: It cannot return.
    • main: main function
    • String[] args: it is used for argument passing as a string to command line argument.

Identifiers:

In java the name used for the methods, classNameand variables are known as identifiers.

  • Identifiers must start with an alphabet or $ or
  • It is case sensitive, keyword cannot be used.
  • Ex: name,$age, _name

  • Ex: 4age, #name ( wrong identifiers)
.