- Joined
- Jan 21, 2007
- Sythe Gold
- 1,234
- Messages
- 14,211
SuF has reported a post.
Reason:
Forum: Computing
Assigned Moderators: Shoop
Posted by: SuF
Original Content:
Reason:
Post: Java Basics - Your First ProgramCan someone rename the title "Java Basics [2] - Writing Your First Program"
+freemodlog
Forum: Computing
Assigned Moderators: Shoop
Posted by: SuF
Original Content:
Java Basics [2] - Writing Your First Program
By SuF
Before reading this guide, make sure you can do the following things:
- Java Development Kit (JDK) installed and configured (guide upcoming)
- Compile and run a Java program (guide upcoming)
Writing Java programs requires quite a bit of boilerplate, code that must be there but does not actually do anything. As you learn more Java you will learn what all of this code means and why it is there but for now just think of it as a template for your programs.
To begin, copy and paste the following code into a text editor or IDE:
Code:public class FirstProgram { public static void main(String[] args) { //Your code will go in here. } }
Let's take a look at this code line by line. In the first line "public class FirstProgram" the most important thing is the class name, in this case "FirstProgram". This name must be the same as the filename that you save your program as, with the ".java" extension. So, in this case your file name must be "FirstProgram.java". Please note that Java is case sensitive so "firstprogram.java" is different than "FirstProgram.java". This is not limited to the file names. Everything in Java is case sensitive so be sure that you type everything exactly as you see it.
The second line "{" is an opening brace. Every opening brace must have a closing brace and in this case the closing brace in on the last line. I have indented the code so that it is easy to tell which opening brace goes with which closing brace. However, this is not required and the above code could also be written as:
Code:public class FirstProgram{public static void main(String[] args){System.out.println("My first program!");}}
To Java, this code is exactly the same as the above code. Java ignores whitespace, which is any character that you can not see such as spaces, tabs, or line breaks. As a programmer, you add in whitespace so that your code becomes easier for humans to read.
The third line "public static void main(String[] args)" is what Java calls a method, called "main", which is simply a block of code. Do not worry about what this means right now but know that for now all of your code will go within the braces of this method.
The fourth line is the opening brace of this method and line six has the closing brace. Line five is a comment, which is there purely for you as the programmer to read. Java ignores comments when it runs and they serve only the purpose of documenting your code. There are two types of comments in Java, a single line comment and a multi-line comment. The comment on line five, "//Your code will go in here." is a single line comment which means that everything to the right of the "//" will be ignored. The "//" is what defines the comment and these can be placed wherever you want like:
Code:... public static void main(String[] args)//This is another valid comment { //Your code will go in here. }//So is this ...
You can also use comments to comment out lines of code in case you need to remove them but want to have a record of them like:
Code:public class FirstProgram { //public static void main(String[] args) //{ //Your code will go in here. //} }
Now, to Java the main method is no longer there as Java completely ignores comments. The second type of comment is a block comment. Block comments start with "/*" and end with a "*/". Everything between the opening "/*" and the closing "*/" is considered a comment and is ignored by Java. This comments can span multiple lines like:
Code:/* This is a comment that spans multiple lines. This is all ignored by Java. And is very easy to do! */ public class FirstProgram { public static void main(String[] args) { //Your code will go in here. } }
Back to what you originally placed in your editor, if you compile and run it, it does nothing. This is because what we have so far is simply boilerplate as stated before. To make it do something we have to add some code to the "main" method. Start by writing out this line within the "main" method (within it's braces):
Code:System.out.println("");
This is a method call which means we are asking Java to go out and find the method "println" and run the code that is contained within it. This method is apart of the Java API which is provided by Java. It contains vast amounts of already written classes and methods for you to use. The "println" method prints a line of text we specify through a parameter, to the console. Do not worry about what a parameter is just yet. We place the text that we want to have printed within the quotes that are within the parentheses. Your message can be any text you want and you can make it as long as you want. After you place your text in, it should look something like:
Code:System.out.println("Hello World!");
and your entire program should look something like:
Code:public class FirstProgram { public static void main(String[] args) { //Your code will go in here. System.out.println("Hello World!"); } }
Your program is now complete and can be compiled and run! My program will output:
Code:Hello World!
Congratulations on writing your first Java program! It does not do much but even getting it to compile can be quite a feat!
Thanks for reading!


















