Skip to main content

Your First Java Program: Hello World

#Topic4


Previous Topic (2&3) we have discussed about how to get started with Java Programming with different tools or techniques. If you have not gone through that topic, click this link What is the software’s needed to start JAVA programming Or How to Install Eclipse and Get Started with Java Programming.

In this section we will discuss about coding part! .Whether you are an experienced programmer or not, this topic is intended for everyone who wishes to learn the Java programming language from ground level.

Let's go over the Hello world program, which we have already written and executed in our previous topic using notepad as well as eclipse, it’s just display the message "Hello, World!" on the screen.

public class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello, World!");

  } }
 
We break the process of programming in Java into three steps, we have already covered these are all topic in our last section please refer that(please click above link).
1. Create the program by typing it into a text editor /Eclipse and saving it to a file named, say, HelloWorld.java

2. Compile it by typing "javac MyProgram.java" in the terminal window. (If you are using Eclipse IDE skip this part!) 

3.Execute (or run) it by typing "java MyProgram" in the terminal window.  (If you are using Eclipse IDE you can click run button )
 
A program is nothing more than a sequence of characters, like a sentence, a paragraph, or a poem. To create one, we need only define that sequence characters using a text editor in the same way as we do for email.
 HelloWorld.java is an example program. Type these characters into your eclipse class file named HelloWorld.java.

If you typed in the program correctly, you should not see any error messages. Otherwise, go back and make sure you typed in the program exactly as it appears above. If everything is fine, you should see the response on your screen "Hello, World!" as an output. Your screen will usually be the console window.
Let us examine the program by keywords.

class: It is an inbuilt keyword , indicates class definition.
- Always must be in lowercase.
class name: It is identifies the character of the class.
Ex : HelloWorld.

Java Naming conventions or rules for specify the java class name:
  • Class name should reflect the purpose of the class.
  • If it is not possible to name the purpose of the class name clearly with a single noun, add more words using Camel casing (like CircleArea). Adjectives are best suitable as extra words as they naturally give more information about the noun.
  • Always start the class name with capital letter.
  • Class name should not contain any space.
  • Class name should not begin with integer.
  • Class name should not exceed more than 256 characters.
  • Do not uses underscore to separate multiple words in a Java class name

{:     It indicates beginning of each block.

public: It is an access specifiers. Should be in lowercase & can be accessed anywhere else in the program.

static: static component allocates memory only during run time. All static components are class level explicit instance does not require executing them. We will learn it later.

void: void keyword can be provided by only for method , those methods does not return any value.

main: main is a method and available in the java.lang.package ,It default package in core java, main represents start-up of the program.

String[] args: main method accepts arguments by String[] always , it indicates parameter with String data type. It also used for command line argument. 

System.out.println() : output statement in java used print statement.

}:     It indicates end of each block.


Note: If source file name and class name is different after compilation it generates .class file with the specified class name. Suppose there are more than 100 source file , each source file have different class name. In this case after compilation of all source files, it is difficult to match source file name with class name. So must be class name(main class) and file name should be same.

Top 10 Java Interview Questions On main() Method

1. Can we interchange the static and public in main method signature?
Yes, we can interchange the static and public keyword in main method signature.Consider the following example,
1.) 
package com.techgeek.shivanandabloghouse;
public class Demo1 {
    public static void main(String[] args) {
 System.out.println("https://shivanandarai.blogspot.in");
    } }

 
2.) 
package com.techgeek.shivanandabloghouse;
 public class Demo1 {
  static public void main(String[] args) { System.out.println("https://shivanandarai.blogspot.in");
    } }
 

 2. Can we change the parameter name for the Main method?
Yes, we can change parameter name for method.
Legal main method signature :
public static void main(String args[])
public static void main(String a[])
public static void main(String[] args)
public static void main(String[] a)

3. Can we define a class without main method?
No, you can’t run java class without main method.

4. Can main() method take an argument other than string array?
No, argument of main() method must be string array. But, from the introduction of var args you can pass var args of string type as an argument to main() method. Again, var args are nothing but the arrays.


5. Can we change return type of main() method?
No, the return type of main() method must be void only. Any other type is not acceptable.
6. Why main() method must be static?
  •  main() method must be static.
  • If main() is allowed to be non-static, then while calling the main method JVM has to instantiate it’s class.
  • While instantiating it has to call constructor of that class. There will be an ambiguity if constructor of that class takes an argument.
          Note: constructor will discuss upcoming topics .

7. Can We Declare main() Method As Non-Static?
  • No, main() method must be declared as static so that JVM can call main() method without instantiating it’s class.
  • If you remove ‘static’ from main() method signature, compilation will be successful but program fails at run time.
        Note: again static will discuss in detail upcoming topics .

8. Can We Overload main() method?
  • Yes, We can overload main() method. A Java class can have any number of main() methods. But to run the java class, class should have main()
  • method with signature as “public static void main(String[] args)”. If you do any modification to this signature, compilation will be successful.
  • But, you can’t run the java program. You will get run time error as main method not found.
        Note: Overload topic will discuss upcoming topics.

9. Can we declare main() method as private or protected or with no access modifier?
  • No, main() method must be public. You can’t define main() method as private or protected or with no access modifier.
  • This is because to make the main() method accessible to JVM. If you define main() method other than public, compilation will be successful but you will get run time error as no main method found.
10. Can we override main in Java ?
No , you cannot override main method in Java, Why because main is static method and in Java static method is bonded during compile time and you can not override static method in Java.
Note: Override topic will cover upcoming topics.


11. Can we make main final in Java? 
you can make main method final in Java. JVM has no issue with that. Unlike any final method you cannot override main in Java.
Note: final keyword will discuss upcoming topics.



Next week I will post about(#Topic-5) What are the Variables and Data types using in Java?
 
If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Twitter or Facebook. Thank you! - Shivananda Rai

Comments










  1. Good one , you have explained in detail and easy to understand...

    ReplyDelete

Post a Comment

Popular posts from this blog

How to Install Eclipse and Get Started with Java Programming

  #Topic-3 How to Install Eclipse and Get Started with Java Programming. Last topic we have discussed about Java Development Kit (JDK) tool and how to write, compile & run (execute) a java program using notepad and command prompt.If you not go through that ,click this link What is the software’s needed to start JAVA programming . So let’s see How to Install Eclipse and Get Started with Java Programming.   What is Eclipse? Eclipse is famous for Java Integrated Development Environment (IDE), Eclipse is an open-source Integrated Development Environment (IDE). Eclipse is popular for Java application development (Java SE and Java EE) and Android apps. It also supports C/C++, PHP, Python, Perl, and other web project developments via extensible plug-ins. Eclipse is cross-platform and runs under Windows, Linux and Mac OS. The Java Development Tools (JDT) project provides a plug-in that allows Eclipse to be used as a Java IDE, PyDev is a plugin that allows Ecli...

What is the software’s needed to start JAVA programming

#Topic-2 Last week we have discussed about some basic topics like how to start java , what is the use etc , If you are very new to Java programming then click this link Java Tutorials for Beginners. What is the software’s needed to start JAVA programming. If you are new to Java programming and wish to learn it right now by doing some hands-on practice, you have visited the right place. This tutorial will help you what is the software’s needed to write your first Java program, Throughout this tutorial, you will learn fundamental concepts and steps which are necessary for every Java fresher.  In order to write and run a Java program, you need below software / editor: 1. Java Development Kit (JDK) 2. Command prompt 3. Note pad OR 4.IDEs : These IDEs offer a variety of features, like: building Java applications, Testing, debugging, code inspections, code assistance, visual GUI builder and code editor, and more. While you can find several Java IDEs or integrated developmen...

Variables and Data Types in Java

#Topic-5 Previous topic (4) we have discussed about how to start coding! , we have gone through our first Java Program line by line. If you have not gone through that topic, click this link  Your First Java Program: Hello World In this section we will discuss about variable , Data types , Literals and many more! Variable Variable is name of reserved area allocated in memory . In other words, it is a name of memory location . It is a combination of "vary + able" that means its value can be changed.   Example:   int value= 10 ; //Here value is variable float a= 10.50 ; //Here a is variable   There are three major types of variables in java: local variable instance variable static variable 1) Local Variable : A variable which is declared inside the method is called local variable. 2) Instance Variable : A variable which is declared inside the class but outside the method, is called instance variable . It is not declare...