Home   |   About   |   Terms   |   Contact    
Read & Learn
 

Basic structure of a simple Java program

Java

☯ All Java Articles     ☯ All Articles


A new product.






Popular Google Pages:




This article is regarding Basic structure of a simple Java program.
Last updated on: .



◕ Basic structure of a simple Java program.

We will write a Java program in which we will add the number of Redballs with the number of Blueballs.
The complete program is given bellow.

public class balls{
public static void main(String[] args){
int numBalls=0;
int redBalls=3;
int blueBalls=2;
numBalls = redBalls+blueBalls;
System.out.println("Total number of balls is: "+ numBalls); // This displays the result on screen.
}
}


■ If we run this program then it will produce the result:
Total number of balls is: 5



Description
The description of the keywords that we used in the above program are given bellow:

public
This keyword specifies that the method main() is accessible from outside the class.
Here it specifies that the method main() is accessible from outside the class balls.

main()
This is a method.
Execution of a program always starts from the first executable statement of the method main().

int numBalls=0;
This line is the first statement of the body of the method main() to be execute.

numBalls = redBalls+blueBalls;
This line is a mathematic expression that calculate the total number of balls.

System.out.println("Total number of balls is: "+ numBalls);
This line displays the result or output on the screen.
Here the output is Total number of balls is: 5

numBalls, redBalls, blueBalls
These are variables and we initialize these variables with a value.

int
This keyword tells the compiler about the type of the variables.

system
This is the name of the class that contains the object out.

out
This is a static variable in the class system.
On the other hand we can say, this is an object in the class system
This contains the method println().

println()
This is a method in the object out.
This method is used to displays the text output.




Related Java articles:
What is Java? Where we can apply Java?
Why Java is a Machine Independent Program? What is Machine Independent Program?
Root Directory of Java
How to Compile and run a Java Program?
What is .class file in Java?
What is variable in a program?
What is identifier in Java?
Integer data types in Java?
What is Primitive Types in Java?
How to declare constant variables in Java?
Why should we declare the initial value of a variable in Java?
Use of Underscores to write an integer in Java
Assignment Statement & Assignment Operator in Java
Increment a variable by a given amount in Java





Popular Google Pages:





Top of the page

Amazon & Flipkart Special Products

   


Top of the page