Page 1 :
Overview of JAVA Language, Java is a versatile language and hence we can make so many types of applications out of it. We can create, regular standalone applications, web applets and enterprise solutions as well. Our task is to learn how to, create java standalone programs. The following are the steps to create standalone programs in java., 1. Write the source code – source code means the instructions that we write in Java language., 2. Java compiler – we must use the java compiler javac to compile the source code and convert it to, java bytecode., 3. Use the java interpreter to run or execute the java bytecode., , Source, code, , Java compiler, , Bytecode, Java interpreter, , Output, , Let us now write a simple java program and then go for a line by line analysis., class FirstProgram, {, public static void main(String args[]), {, System.out.println("This is my first Java program.");, }
Page 2 :
}, The above program could be the simplest of java programs but still it is more than enough for us to learn, some of the concepts to start writing programs in java. Java being an object oriented language everything, must be inside a class and there must be a main class ( a class that has main() method in it). We must, remember one of the most important point that when we are saving a java program we must name it, according to the name of the class that has the main() method in it. We will discuss in detail on this later, on. But for the moment just keep in mind that we must name the file same as the name of the class and, the file extension must be .java. Let us now discuss the program line by line., class FirstProgram – This is the first line of the program. Here we are using the keyword or special word, called class and telling java that we are creating a class with a certain name. Remember the keyword must, be written in small letters. The class keyword is followed by the className. Few points to remember over, here that class names are usually started with a capital letter, this is just a convention and not a rule. There, must not be any spaces in a className. Following the className there is an opening curly bracker ‘{‘. This, indicates that form now on whatever we write is going to part of the class. If you checkout the program, that there is also a closing curly bracket ‘}’ that corresponds to the opening bracket. For every open bracket, there must be a close bracket., public static void main(String args[]) – This line perhaps the most important line in the whole program., There are lots of keywords (words that are solely meant for a programming language to use and not any, other purpose meant for the user. In other words, keywords cannot be used as name of class or variable, or anything else) that have their own purpose. In a java program there can as many classes as required by, the programmar but there is only one class with the main() method in it. The main() method is the starting, point of the program, the java interpreter uses the main() method to begin the execution of a program., public: This keyword is used to make the main method public which means this method can be, called and accessed by any other class. We will discuss about the access specifiers later on., static: The static keyword makes the main() accessible to the whole class and not to a part of the, class. main() method must be declared as static as the interpreter uses this method before any object of, the class is created. We will discuss about this in later chapters., void: The term void means nothing. Over here it means that main() method is not going to return, any value., Within the main() method a parameter args[] array is being passed as String type. This is used when, command line arguments are passed., System.out.println("This is my first Java program."); - This is the line that shows us the output. System is, the name of a class that has an object named out and println() is a method in the class. The println() method, is used to provide output. To output a String or a text on the screen it must be enclosed within “” double, inverted commas. The output is displayed and then a newline character is appended at the end of it. At last, we can see a ‘;’ semicolon which means the end of the statement, this is similar to a fullstop at the end of, a statement. This ; semicolon indicates that the statement is complete.
Page 3 :
Compiling and running: After the program is written now it must be saved as a java source file and to do, that save the file with extension .java. Following this open up terminal or a command prompt and browse, to file location and then use the following command to compile the program., javac FirstProgram.java – This is the command to compile a program. If any error is there in writing the, program it will shown if not then the prompt will return, which means the program has compiled, successfully. Next we must use the java interpreter to run the program., java FirstProgram – This command runs or executes the java program and gives the intended output. The, command syntax is java Name_of_the_program. This calls up the object file that has been created after, compiling.