Friends lets start with our first Java program. This is going to be most simple program to print your name on screen.
Steps to follow:
1. First you need to create a class
2. Give your class a name example MyName
3. defining main() function as execution begins from main()
4. your code to display your name
This is how your class should look:
class class_name
{
main()
{
your code to display your name;
}
}
Program to print your name on screen:
class MyName <-------- Class Name
{
public static void main(String s[]) <-------- main() function
{
System.out.print("Nitin Kumar"); <------ printing command that will print Nitin Kumar
}
}
Output:
Nitin Kumar
Friends this program may look strange to you at first. Specially this line public static void main(String s[]) will confuse you most.
Friends as you know that execution of your program starts at main() function. So first JVM will find main() function in your program. After that your code inside main() will be executed.
Why we have used "public" before main() function?
Answer: public is a access specifier. There are 3 types of access specifier in Java i.e. public, private and protected. if we write private or protected before main() the JVM is not able to find main function and your program will not be executed.
private static void main(String s[]) is wrong as JVM will not be able to find main() function and your program will not be executed.
protected static void main(String s[]) is also wrong again JVM will not be able to find main() function and your program will not be executed.
public static void main(String s[]) is right as JVM can find the main() function because main() is now public.
That is why we have used public instead of private and protected.
Why we have use "static" before main() function ?
Answer: "static" is a keyword. To understand its working you have to learn how we call a method or function in our program.
Generally what happens is that when you create a method or say function you can use that method only by calling that method using object.
Example:
void function() <-------function name
{
System.out.println("I am a Function"); <------- this will print I am a Function
}
friends I have created a function named as function(). Now I can not use this function unless I call this function using Object. First I have to make an object and than using that object I can call this function.
So Function is of no use unless you call them using object.
1. creating a function
2. creating an object
3. using that object to call function()
These 3 steps are generally followed to use or to call a function.
Look at our program public static void main(String s[]) There is a function i.e. main() function. To use main function we need to call main() function. But as it require an object to call any method Java gave us another method of calling function without creating object. "static" keyword will tell the JVM that main() function is automatically called without using object. So by putting "static" keyword before any function or method that method will be called automatically by JVM with out using any object.
What is the use of "void" that is used before main() function ?
Answer: "void" is just return type. In our first program we are not returning any value so we put void before main() function. Note: you must provide a return type before any function or your function will not work.
Example:
void function1() <---------- this is a function named as function1() of return type void
{
System.out.println("My Name"); <------- this will print My Name on screen and there is no need to return any value
}
int function2() <---------- this is a function named as function2() of return type int
{
return 30/3;<--------- we must return any integer value as function is of int type
}
function3()<-------- this function has no return type it will show you a compile time error
{
System.out.println("I am wrong");<----- I am not going to execute as no return type is provided please write void before function3() to make me execute
}
So using void before main() function is because our main() function is not returning any value.
What the hell is "String s[]" in my first Java program ?
Answer: In Java everything we do like printing name or taking input from user is all by default is handled in the form of String. Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. The Java platform provides the String class to create and manipulate strings. Do not worry about it right now just always remember to write String s[] inside main() i.e. main(String s[]).
What is System.out.print() ????
Answer: This is used as a print command in Java. It will print everything that is inside " ". System.out is a class here and print() is a function. You will learn about them later on.
Friends this is just a beginning if you have any doubt please do write in comment. If you want to point out any mistake you are most welcome.
0 comments:
Post a Comment