Java Basics [2] - Writing Your First Program

Discussion in 'Guides' started by SuF, Sep 20, 2013.

Java Basics [2] - Writing Your First Program
  1. Unread #1 - Sep 20, 2013 at 11:48 AM
  2. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    Java Basics [2] - Writing Your First Program

    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!
     
  3. Unread #2 - Jan 21, 2014 at 10:16 PM
  4. SocialDude
    Joined:
    Nov 6, 2013
    Posts:
    220
    Referrals:
    0
    Sythe Gold:
    0
    Two Factor Authentication User

    SocialDude Active Member

    Java Basics [2] - Writing Your First Program

    Great share man, we should definitely focus on coding/scripting on Sythe a little more.
     
  5. Unread #3 - Jan 22, 2014 at 10:36 PM
  6. BaLlSooHardd
    Joined:
    Sep 19, 2013
    Posts:
    103
    Referrals:
    0
    Sythe Gold:
    0

    BaLlSooHardd Active Member
    Banned

    Java Basics [2] - Writing Your First Program

    great guide.
     
  7. Unread #4 - Jun 5, 2014 at 8:59 PM
  8. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    Java Basics [2] - Writing Your First Program

    bump.
     
  9. Unread #5 - Jun 6, 2014 at 7:23 PM
  10. iJava
    Joined:
    Nov 21, 2011
    Posts:
    1,197
    Referrals:
    11
    Sythe Gold:
    485
    Discord Unique ID:
    220055593568829441

    iJava .Previously known as RSGoldRush
    $200 USD Donor New

    Java Basics [2] - Writing Your First Program

    Good guide, well explained.
     
  11. Unread #6 - Jun 9, 2014 at 7:16 PM
  12. RocketPower
    Joined:
    May 18, 2013
    Posts:
    843
    Referrals:
    0
    Sythe Gold:
    0

    RocketPower WARROCKPS.COM
    Banned

    Java Basics [2] - Writing Your First Program

    thank you for the contribution
     
  13. Unread #7 - Jun 10, 2014 at 9:22 PM
  14. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    Java Basics [2] - Writing Your First Program

    Glad to help.
     
  15. Unread #8 - Jun 22, 2014 at 2:10 PM
  16. LilZoo
    Joined:
    Jun 22, 2014
    Posts:
    344
    Referrals:
    0
    Sythe Gold:
    2

    LilZoo Forum Addict

    Java Basics [2] - Writing Your First Program

    Not bad , helped me , thanks !
     
  17. Unread #9 - Jul 26, 2014 at 4:33 PM
  18. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    Java Basics [2] - Writing Your First Program

    Thanks!
     
  19. Unread #10 - Sep 14, 2014 at 2:00 PM
  20. Mindaugas
    Joined:
    Aug 22, 2014
    Posts:
    298
    Referrals:
    2
    Sythe Gold:
    10
    Two Factor Authentication User Tier 1 Prizebox

    Mindaugas Forum Addict
    $25 USD Donor New

    Java Basics [2] - Writing Your First Program

    Thanks for sharing, great guide! ;)
     
  21. Unread #11 - Sep 15, 2014 at 9:24 PM
  22. Hideindark
    Joined:
    Jul 20, 2014
    Posts:
    74
    Referrals:
    0
    Sythe Gold:
    0
    Two Factor Authentication User Christmas 2013

    Hideindark Member

    Java Basics [2] - Writing Your First Program

    Thanks for the guide! Will be sure to try it out. :)
     
  23. Unread #12 - Sep 19, 2014 at 10:17 PM
  24. Pain
    Joined:
    Jun 3, 2014
    Posts:
    51,976
    Referrals:
    11
    Sythe Gold:
    4,836

    Pain Formerly known as Divine
    Banned

    Java Basics [2] - Writing Your First Program

    Using this+ other ref's to try to try to start my developing career :).


    Thanks!
     
  25. Unread #13 - Oct 7, 2014 at 9:28 PM
  26. sav99
    Joined:
    Oct 7, 2014
    Posts:
    4
    Referrals:
    0
    Sythe Gold:
    0

    sav99 Newcomer

    Java Basics [2] - Writing Your First Program

    nice guide , well explained
     
  27. Unread #14 - Dec 10, 2014 at 9:11 PM
  28. RS3GoldMart
    Joined:
    Nov 28, 2013
    Posts:
    3,820
    Referrals:
    1
    Sythe Gold:
    942
    Discord Unique ID:
    697842166294249584

    RS3GoldMart GTA V ONLINE | PUBG | DOTA2 | Runescape

    Java Basics [2] - Writing Your First Program

    Nice guide for newbies..
     
  29. Unread #15 - Oct 28, 2015 at 2:51 PM
  30. Timms
    Joined:
    Oct 27, 2015
    Posts:
    139
    Referrals:
    0
    Sythe Gold:
    0

    Timms Active Member
    Banned

    Java Basics [2] - Writing Your First Program

    Will continue reading later on this evening when i'm back but nice guide so far, thanks :)
     
< How to Create, and Verify a PlayerAuctions Account | -= Onebip - Send/Accept/Collecct Money with ease! =- >

Users viewing this thread
1 guest


 
 
Adblock breaks this site