Writing your first programFrom WikiJavabuy this book
the articleThe fileA java program is basically one or more textual files containing the source code for the program. In this first example I'll guide you through writing a simple single file program. This is the traditional first program every developer does when starting: Hello World. The first thing to do is to create teh textual file that will contain your program. Java defines specific rules for the name of this file:
What does the second point mean? What is a class? Class is a fundamental concept in Object Oriented Programming (OOP). For the sake of simplicity in this example it's sufficient to say that a class contains a data structure and some methods that can manipulate the data structure. The way to define a class is using the keyword classfor example public class HelloWorld defines a public class called 'HelloWord'. Inside the classIn a Java file there must contain only one public class so it's automatic that the name of the file (seen the rules above) must be 'HelloWord.java'. Inside a class you can insert a data structure, in form of variables that contains values. And you can insert methods which are groups of commands that can be executed by the JVM and operate according to the given variables. Java defines a rule that a class to be executable directly from the JVM must contain a method called main that must be defined as follows: public static void main(String[] args) It must be 'public', 'static', it must return void and must get as parameter an array of String called 'args'. In sum these special keywords means that the main method must be executable even if the class is not instanciated, that it must be executable by any other method within the JVM and the Class Loader and that whoever calls the main method won't expect any return value at the end of the execution. The main method is the entry point of any Java program. In other words to be executed a java program must contain at least one main method inside a class and everytime the program is executed you must choose one main method to execute, because only one can be the entry point. CommentsOne last detail before going to the code, we can write comments in almost any point of our program. The comments can be used to explain what the program is doing or to exclude the execution of some parts of the code. Since the code is not always easy to understand from humans comments are very useful to help them grasp what the code does. Or in some other cases comments are useful to understand what's wrong with a program (home made debug), when it's not doing what it's supposed to. There are two forms of comments: The first starts with the string /* (forward slash followed by a star) and ends with the string */ (star followed by a forward slash). Java Ignores everything included within these two boundaries and we can write inside them whatever we want. the second form of comment starts with the string // (double forward slash) and ends at the end of the same line. Inside the methodInside the main method we can finally insert the commands. The commands can be of several types:
Integer a; Where you are creating a variable called 'a' and saying that this variable is of type Integer. You are not giving any value to 'a' so it's value it's initially a special value called 'null'.
a = 5; where you are giving to 'a' the value '5'.
if (a == 5) System.out.println("Hello World!!!"); Which calls the method 'System.out.println(String);' only if 'a' has value '5'. All the commands can contain calls to methods in several points of their body. Normally the commands are executed in the same order in which they are written and the only way to modify this order is to use one of the constructs for controlling the flow.
System.out.println("Hello World!!!"); This command calls the method 'System.out.println(String);' with parameter "Hello World!!!" which is a String. This command is looks like it's not listed in the previous list, but is in fact an assignment. The fact that 'System.out.println(String);' is defined in the APIs not to return any value makes useless to write a complete assignment in the form 'variableName = value'.
HelloWorld.java/** * Hello World * * @author Giulio */ public class HelloWorld { /** * * Prints on the default output a greeting to the world. * * @param args */ public static void main(String[] args) { System.out.println("Hello World!!!"); } } What to doTo create our first Java program we can just copy the above source code inside the 'HelloWord.java' file and save it in a directory, for example 'c:\javaPrograms'. The program is then ready to be compiled and executed
|
