When we look into java program it is basically using the concept of class and object. So now we concentrate on the class, object and method.
Class:
A class is a user defined blueprint which contains the entire set of similar data and functions that the object possess.
Object:
An object contains of state and behaviour. In the real world we may see many objects like house, car, dog etc. the object of class is known as instance.
Method:
A method is a logical operation written by programmer. A class may 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:
public class MyFirstProgram { /* My first program. It will print “First Program” as the output */ public static void main(String []args) { System.out.println("First Program"); // prints First program } }
Steps for save, compile and run the java program:
Output:
C:\> javac MyFirstProgram.java
C:\> java MyFirstProgram
First Program
Key points must remember while creating your program
Ex: class MyFirstProgram
Ex: public firstMethodname()
Ex: if 'MyFirstProgram' is the class name means then the file should be saved as ‘MyFirstProgram.java’
Ex: public static void main(String []args)
o public
: public means that we can call the method from outside the class.
o static
: No need to create an object for static methods to run. It can run itself.
o void
: It cannot return anything.
o main
: It is the method name.
o String[] args
: it is used for argument passing as a string to command line argument.
Identifiers:
In java the name used for the methods, class and variables are known as identifiers.
Identifiers must start with a letter that is (A - Z or a – z),$,_(underscore).
It is case sensitive, keyword cannot be used.
Ex: name, $age, _name
Ex: 4age, #name ( wrong identifiers)
Follow Us