My first Java Program, My First Blog
In this tutorial we assume that you have JAVA installed on your computer and have set the necessary environmental variables in order to compile your programs. If you need more information on this please visit Java.com or http://www.jibble.org/settingupjava.php to install the proper developer’s kit for your operating system.
With every programming language comes the much dreaded starting point in which the developer will have to come to terms with the inevitable "hello world program". In this brief example I will break this cliche a bit and show you how to do a "Hello, Google!" program. No this will not increase your search engine rankings or make you part of an elite developer's pack, but it will start you off with the basic fundamentals of the Java Programing language, and show you how to output a string to the console/terminal window of your operation system.
To begin we start with the basic code:
public class HelloGoogleCode {
public static void main (String[] args){
System.out.print("Hello, Google!");
}
}
The First Line:
Notice we start the program with the words "public" and "class" and follow that with a descriptive term to label our class “HelloGoogleCode”. The words public and class are considered to be Java Keywords (we will get into this in more depth later) .
In addition you will notice the first letter of the class name is UpperCase as well as the following words “Google” and Code”. This is a common naming convention in the Java programming language and will determine what you will name your file as. In this instance we would save the java file as “HelloGoogleCode.java”. It should also be noted that it is customary to label your class names as nouns.
The Second Line
In the second line of code we use the main method syntax .In the Java programming language this is read by the Java Virtual Machine (JVM) as the starting point to the program’s execution, and it is here where the main method is called (and any other methods you may have).
The Third Line
Finally in the third line this is where we output the text string “Hello, Google” to the console, using the “System.out” object and the “print” method. It is important to note the line System.out.print("Hello, Google!"); is considered to be a statement and must be followed by a semicolon.
Compile it!
To compile the above code in the windows environment, I would generally use a program like notepad++ (free) to write and save the code. Then open up the command line utility in windows, navigate to the directory when the file has been saved by using the change directory command i.e.
cd Desktop
javac HelloGoogleCode.Java
java HelloGoogleCode
The output, if compiled with no errors, should be:
Hello, Google!
Please note that this method will only work if you have Java installed on your machine and have properly set up the environmental variables on your system.
As you can see there is a lot going on in these couple lines of code, I will expand upon the basic fundamentals of JAVA and other OOP (Object Oriented Programming) principles in later tutorials. But for now I will leave you will this minor titbit and hope to bring you more tutorials very soon.
Labels: Java

0 Comments:
Post a Comment
<< Home