Java Tutorial:Getting Started with Java Program

It's time to create our first program in java.


Let's code in Java 


/*
This is a simple java program
*/
class Foo
{
public static void main(String args[])
{
System.out.println("Welcome to Java Programming Language");
}
}

The Java compiler requires that a source file must use the .java extension. Java source file officially called compilation unit. The first thing you must learn about Java is that the name you give to a source file is very important. If you observe the above example, the class name starts with Upper case letter. It indicates that it's a class name. You may know that java is case sensitive language. To save the above program;


Foo.java

As you can see the name of the class is same as the source file name. This is not miracle. In Java all code must reside inside a class. The name of the main class should match the name of the source file that holds the class name. You should make sure that your public class name is same as the file name. That's only thing you should remember about Java file creation.


How to Compile Java Program in JDK 7

To compile Java file, you should issue a command before the source file name in command prompt (if you are using Linux , then press Alt+Ctrl+t to open Terminal window and type the command).


javac Foo.java


The javac compiler generates a file is called Foo.class that contains the bytecode version of the program. Java bytecode is a intermediate representation of your java program that contains instructions the Java Virtual Machine (JVM) will execute. javac does not provide any codes that directly executed. It creates only .class file. In the above example, you can figure out the .class file in directory.

Run Java Programs 

You must use Java application launcher for executing java program. To do so, pass the class name as command line argument. You don’t want to use .java extension with java application launcher.

Java Foo



/*
This is a simple java program
*/ 
The above statement is an optional part of Java source program. Many modern languages support this 'comment' feature. It's nothing; it's used for documentation purposes. Compiler ignores the content of the comment. A comment describes the operation of the program to anyone who is reading its sources code.

class Foo {

This line has class keyword, which used to declare a class. New class is being defined; Foo is an identifier that is the name of class. The entire class definition will be between the opening curly braces ( { ) and the closing curly brace ( } ).

public static void main(String args[ ])

All Java applications start execution by calling main() . here public keyword is an access modifier. This allows you to control the visibility of class members. The keyword static allows main() to be called without creating an object of class. void simply says that, main() method does not return anything. String args[] declares a parameter named args, which is an array of instances of the class String.

System.out.println("Welcome to Java Programming Language");

This line outputs the string 'Welcome to Java Programming Language' .followed by a new line on the screen. The output generated by using a built-in method 'println()' println() method can be used to print data or information on screen. 



Java Tutorial:Getting Started with Java Program Java Tutorial:Getting Started with Java Program Reviewed by Nikin on 06 January Rating: 5
Powered by Blogger.