Skip to main content

The first simple program in java

The first program in java

Program is, to print "Hello World" on the screen

               After discussing the history of java, we are going to discuss the first simple program in java, which prints "Hello World!" on the screen of the computer. Java file is saved with the ".java" extension. Let's start with the commands, which are used to compile and run the java program on Ubuntu.


Compilation: 

Java program file is compiled by using the following command on Ubuntu:
javac file_name.java 

What happens after compiling the java file?      It creates the new file with the same name and .class extension which contains the bytecode version of the program. It also checks the syntax as per java language. It shows errors if any. After successful compilation, we have to run the .class file which contains the byte-code.


Run: 

TO run the .class file, use the following command for Ubuntu:
java file_name

 After entering this command, we will get the output on the screen.



Code for Hello World Program:


Output:





Let's see the code line by line with the explanation.
---------------------------------------------------------------------------------------------------------------
  •  The first line contains:
/* Hello world program in java */

 This is the comment, which is used to provide the information about program. Comments are used in programs for the documentation purpose.
---------------------------------------------------------------------------------------------------------------
  •  The Third line contains:
class example{

This line contains the keyword class, example is the identifier which is the name of the class. The name of the class should be as same as the name of the file. The class definition contains its members inside the curly braces {}.
---------------------------------------------------------------------------------------------------------------
  • The fourth line contains: 
    public static void main(String args[]){

This line contains the main() method. Every program begins with a call to the main() method. The public keyword is an access specifier. We will discuss access specifiers in detail later. The keyword static allows the main( ) to be called without having to instantiate a particular instance of the class. This is necessary since the main() is called before creating any object. The void is the return type of the main() method. This calls to the compiler that, main() method does not return any value. String args[ ] declares a parameter named args, which is an array of instances of the class String
---------------------------------------------------------------------------------------------------------------
  • The fifth line contains:
System.out.println("Hello World!");

This line shows the output Hello World!, followed by the new line. If we use the print instead of println, it does not follow by a new line. We will discuss the difference between both of them in the next post.
---------------------------------------------------------------------------------------------------------------
The next two lines complete the curly braces of class and the main method. This is the small explanation of the simple hello world program. If you have any doubt then comment on it, I will try to solve it.

Thanks.

Comments

Popular posts from this blog

Identifiers

Identifiers:                                         A name in java program is called identifier, which can be used for identification purposes. It can be a method name, variable name, class name, or label name . Rules for defining java identifiers: The only allowed characters in java identifiers are a - z, A - Z, 0 - 9, $, and _. If we are using other characters, we will get a compile-time error. Identifiers can't start with digits. Java identifiers are case sensitive. Of course, java is treated as case sensitive programming language. There is no limit for java identifiers but it is not recommended to take too lengthy identifiers. We can't use reserved words as identifiers. All predefined java class name and interface name we can use as identifiers.                     e.g.                         int String = 888 ;                         int Runnable = 999 ;                         Even this is valid, but it is not good programming practice. Examples Que 1) How many identi

Literals

Literals:                 Any constant value which can be assigned to the variable is called literal.     1 . Integer Literals:                                        For integral data types ( byte, short, int, long ), we can specify a literal value in the following base...                         a) Decimal literal (base 10):                                                     Allowed digits are 0 to 9.                                                                e.g.  int x = 10;                         b) Octal literal (base 8):                                                 Allowed digits are 0 to 7.                                             Literal should be prefixed with '0' (Zero).                                                       e.g.   int x = 010;                         c) Hexa decimal literal (base 16):                                                   Allowed digits are 0 to 9, a to f. For extra digits (a to f), we can use both lowercase and uppercase c

Reserved words

Reserved Words:                                    In java, some words are reserved to represent some meaning or functionality such types of words are called reserved words. Data type related keywords:                     1. byte                         2. short                         3. int                         5. long                         6. float                         7. double                         8. char Flow control-related keywords:                     1. if                         2. else                         3. switch                         4. case                         5. default                         6. do                         7. while                         8. for                         9. continue                         10. return                         11. break Modifiers related keywords:                     1. public                         2. private                         3. protected                         4. static