First step towards java - First java program



What happens when we include the header file? After we include the header file, the C/C+ compiler goes to the standard library  and searches for the header file there. When It finds the header file, it copies the entire header file content into the program where the include statement is written. Thus, after copying code from the header file, the program size may become.
 They have been copied physically into our program from the header file. Thus, our program size increases unnecessarily, wasting memory and processor time.
A similar but a more efficient  mechanism, available in case of Java,  is that of importing classes.First, we should decide which classes are needed in our program. Generally, programmers are interested in two things:
1) Using the classes by creating objects in them.
2) Using the methods (functions) of the classes.
In Java, methods are available in classes or interfaces. What is an interface? An interface is similar to a class that contains some methods. There is a lot of difference between an interface and a class, which we will discuss later. The main point to be kept in mind, at this stage, is that a class or an interface contains methods. A group of classes and interfaces are contained in a package. A package is a kind of directory that contains a group of related classes and interfaces and Java has several such packages in its library.
Java library
      |
Packages
       |
Classes and interfaces
                       |
Methods
If  programmer wants to use a  class,  then that class should be Imported into his program. If he wants to use a method, then that corresponding class or Interface should be imported into his program.
Whenever we want to import several classes of the same package, we need not write several import statements, as shown above; instead, we can write a single statement as:
Import java.lang.*;
Here,  * means all the classes and interfaces of that package, i.e, java. lang,  are imported into our program. In Import statement, the package name that we have written acts like a reference to the JVM to search for the classes there. JVM will not copy any code from the classes or packages. On the other hand, when a class name or a method name is used, JVM goes to the Java library, executes the code there, comes back, and substitutes the result in that place of the program. Thus, the Java program size will not be increased.
Q: What is the difference between  #i nclude and import statement?
A:  #include directive makes the compiler go to the C/C+  standard library and copy the code from the header files into the program. As a result  the program size increases, thus wasting memory and processor’s time.
import statement makes the JVM go to the Java standard library, execute the code there, and substitute the result into the program, Here, no code Is copied and hence no waste of memory or processor’s time. So, import is an efficient  mechanism  than #include.
First java program: Helloworld.java
public class Helloworld
{
                Public static void main(String args[])
{
                System.out.println(“Hello world”);
}
}

Let us discuss about above program.

After importing the classes into the program, the next step is to write a class. Since Java is purely an object-oriented programming language, we cannot write a Java program without having at least one class or object. So, it is mandatory that every Java program should have at least one class In it.
We should use class keyword for create class and then write the class name.  in the above program, Helloworld is the class name. A class code starts with a { and ends with a } . We know that a class or an object contains variables  and methods (functions). So we can create any number of variables and methods inside the class.
In our first program, we have written main() method. Why should we write main() method? Because, If main()  method  is not written In a Java program, JVM will not execute it.  main() is the starting point for JVM to start execution of a Java program. So main() method is compulsory method.
public static void main(Strlng args[])
Next to main() method’s name, we have written string args[]. Before discussing the main() method, let us first see how a method works. A method, generally, performs two functions.
1) It can accept some data from outside.   2) It can also return some result
Let us take sqrt () method. This method is used to calculate square root value of a given number.So, at the time of calling sqrt method, we should pass a number (e.g. 25) for which we want to calculate square root. Then, it calculates square root value and returns the result (e.g. 5) to us.
Similarly, main () method also accepts some data from us. For example, it accepts a group of strings, which is also called a string type array. This array is String args[].  which is written along with the main() method.
Here args[]  is the array name and It is of String type. This means that it can store a group of strings. Remember, this array can also store a group of numbers but in the form of strings only. The values passed to main () method are called arguments. These arguments are stored into args[] array, so the name args[]  is  generally used for it.
A method can return some result. If we want the method to return the result in form of an integer, then we should write int  before the method name. Similarly, to get the result in string form or as a single character, we can write string or char before it, respectively. If a method is not meant to return any value, then we should write void before that method’s name.  void means no value.
main () method does not return any value, so void should be written before that method’s name. A method is executed only when it is called. But,  how to call a method?  Methods are called using 2 steps in Java. They are
1.       Create an object to the class to which the method belongs. Syntax for creating object is
Classname objectname = new Classname();
2.       Then the method should be called using the objectname.methodname().
Since main() method exists in the class Helloworld; to call main()method, we should first of all create an object to Helloworld class, something like this:
Helloworld obj = new Helloworld();
Then call the main () method as:  obj.main();
We have to write the above code inside the  main() method, then jvm will execute this statement and create the object.
Class Helloworld
{
                Public static void main(String args[])
{
Helloworld obj = new Helloworld() ;  //object creation
}
}
by looking at the code, we can understand that an object could be created only alter calling the main () method. But for calling the main () method, first of all we require an object . Now, how is it possible to create an object before calling the main() method?  So,  we should call the main() method without creating an object. Such methods are called static methods and should be declared as static.
Static  methods are the methods, which can be called and executed without creating the objects. Since we want to call main() method without using an object, we should declare main() method as static. Then, how is the main() method called and executed? The answer is by using the classname.methodname(). JVM calls main() method using its class name as Hellworld.main() at the time of running the program.
JVM is a program written by JavaSoft people (Java development team and team)  and main() is the method written by us. Since, main() method should be available to the JVM, It should be declared as public. If we don’t declare main() method as public, then it doesn’t make itself available to JVM and JVM cannot execute It.
So, the  main() method should always be written as shown here:
public static void main(String args[])
If at all we want to make any changes, we can interchange public and static and write it as follows:
static  public void main(String args[])
Q:  what happens if String args[] is not written main() method?
A: When main() method is written without Srring args[]  as:
public static void main()
the code will compile but JVM cannot run the code because it cannot recognize the main() method as the method from where it should start execution of the Java program. Remember JVM always looks for main () method with string type array as parameter.
Execution of program:  save the program with .java extension.
Then go to command prompt, and execute the following commonds.
C:\> javac Helloworld.java
C:\>java Helloworld
Output: Hello world

Let us discuss one more program.
//multiplication of two numbers
class multiplication
{
                public static void main(String args[])
                {
                                //variables
                                int  x,y;
                //storing values into variables
                x=50;
                y=100;
                int  z= x * y;
                System.out.println(“multiplication of x and y is :”+z);
                }
}

C:\> javac Multiplication.java
C:\> java Multiplication
multiplication of x and y is: 5000
here we are using ‘+’ to join the string “multiplication of x and y is:” and the numerical variable z.  the reason is that the print() method cannot display more than one value. So if two values, like a string and a numeric value need to be displayed  using a single print() method,  we should combine them using a ‘+’ operator.
Q: What is the difference between print () and println() method?
A:Both methods are used to display the results the monitor. print () method displays the result and then retains the cursor in the same line, next to the end of the result. println() displays the result and then throws the cursor to the next line.
Let us discuss backslash codes.
       Backslash code
meaning
\n
Next line
\t
Horizontal tab
\r
Enter key
\b
Back space
\f
Form feed
\\
Displays \
\”
Displays “
\’
Displays ‘

Example:
class sample
{             
                public static void main(String args[])
                {
                                String  x=”teach”,y=”you”, z=”well”;
                                System.our.println(x+”\t”+y);
                                System.our.println(y+”\n”+y);
                                System.our.print(“:”+z);
                                System.our.println();
                                System.our.println(”teach\\you\”+z);
                }
}
C:\>javac sample.java
C:\>java sample
teach          you
teach teach
:well
teach\you”well

No comments:

Post a Comment

Genuine websites to earn money.

If you are interested in PTC sites then this article is for you. I have personally tried many of the sites and found that the best thing ...