Java Basics
Java is object oriented programming language, which is easy to learn, compile and run on any Operating System. Java Programs are run by Java Virtual Machine(JVM).Every Java Program have a class. Basically a class is a set of variable and functions.
Example:
A car of blue color having functions like blow horn, turn light on or off, drive, show Fuel Available.
In java this can be written as:
class Car
{
String color;
int carWeight;
void getFuelCapacity()
{
}
void drive()
{
}
}
In the above example a Car class is taken with attribute color(string type), carWeight(integer type) and with functions like getFuelCapacity and drive.
A class which includes main() function is called by JVM when program executes.
When we compile our program, each class is compiled and saved seperated with their name with prefix ".class"
After that we run only the class which contains the main class.
Example:
FileName: JavaExample1.java
class Test1
{
public static void main()
{
}
}
class Test2
{
public void helloWorld()
{
}
}
This program compile by using: javac.exe JavaExample1.java
When we compile the above program it will generate two files because it contains two class with name "Test1.class" and "Test2.class"
If we want to run the program, we have two class:
If we run class Test1, it perfectly run because it contain main() function.
If we run class Test2, it shows error, because it not contains main() function.
To run the program we use: java.exe ClassName
Example in the above program: java.exe Test1
Note: to run the program dont use any extension.
Prev: Java Installation and Setup Next: A HelloWorld Example
No comments:
Post a Comment