OBJECT ORIENTED PROGRAMMING CIS 3100

Suppose if you are entering your program in notepad

This preview shows 35 out of 38 pages.

Suppose if you are entering your program in notepad, then save this file as Welcome.java Compiling the program After we have written our program we need to compile and run the program. For that we need to use the compiler called javac which is provided by java. Go to the command prompt and type the file name as shown here. c;\>javac Welcome.java The javac compiler will create a class file called Welcome.class that contains only bytecodes. These bytecodes have to be interpreted by a Java Virtual Machine(JVM) that will convert the bytecodes into machine codes. Once we successfully compiled the program, we need to run the program in order to get the output. So this can be done by the java interpreter called java. In the command line type as shown here. c:\>java Welcome
Image of page 35

Subscribe to view the full document.

So the output will be displayed as Welcome to Java-Sample!!! Let us closely examine each part of the program. /* This is a sample java program Save this file as Welcome.java */ This is called comment. This is for us to enter the comments about the program for our own convenience. The contents of a comment will be ignored by the compiler. Actually java supports three styles of comments. The above one is called multi-line comment which may contain several lines. This type of comment must begin with /* and end with */. The next line of the code in a program is class Welcome { The word class is a keyword to define a new class and Welcome is a name of the class. The class definition must begins with opening curly brace ({) and ends with closing curly brace (}). The rest of the things defined inside these braces are called member of the class. And note that all the program activities are defined inside the class. // A java program will start from here. This is another type of comment. This is called single line comment starts with // and ends with end of the line. Generally we use it for brief comments. The next line of the code in a program is public static void main(String args[]) This line begins with main method as like functions or subroutines in other languages. The program will start execute by calling this main
Image of page 36
method. Let us see briefly about the other attributes declared in main method. However we are going to discuss in detail about this in later chapters. The keyword public is an access specifier. The keyword static is a kind of modifier. The keyword void means that the method main() does not return any value. As we had seen before all the java program will start execute by calling the main method. If we want to pass any information to a method will be received by the variables declared within the parenthesis is called parameters. In a main() method there is only one parameter ,String args[] . args[] is a name of the parameter that is an array of the objects of data type String. String store sequences of characters and args will receive the command line arguments.
Image of page 37

Subscribe to view the full document.

Image of page 38
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern