Showing posts with label java basic. Show all posts
Showing posts with label java basic. Show all posts

Saturday, 4 July 2015

Structure of a Java program

The following program demonstrates the structure of a Java program. A basic Java program consists of a class that has member fields and methods.

class Fundamental
{
 public int x; //this is a member field of the class

 public Fundamental()  //this is the default constructor
 {
x = 10;
 }

 public void printX() //this is a member method of the class
 {
System.out.print("x = "+x);
 }

 public static void main(String []args)
 {
Fundamental obj = new Fundamental(); //initializing an object of the class
obj.x = 20;
obj.printX();
 }
}

Hello World

The 'Hello World' program is the first program in any programming language. The following[ program simply writes the string in double quotes (" "), on the output screen.


class HelloWorld
{
 public static void main(String[] args)
 {
System.out.print("Hello World! Welcome to Programming Infinitum.");
 }
}