#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.
- 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.)
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");
} }
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.
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.
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.
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.
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
ReplyDeleteGood one , you have explained in detail and easy to understand...
Thanks
Delete