Adblock breaks this site

Java Basics [2] - Writing Your First Program

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

  1. SuF

    SuF Legend
    Pirate Retired Global Moderator

    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
    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!
     
  2. SocialDude

    SocialDude Active Member

    Joined:
    Nov 6, 2013
    Posts:
    220
    Referrals:
    0
    Sythe Gold:
    0
    Two Factor Authentication User
    Java Basics [2] - Writing Your First Program

    Great share man, we should definitely focus on coding/scripting on Sythe a little more.
     
  3. BaLlSooHardd

    BaLlSooHardd Active Member
    Banned

    Joined:
    Sep 19, 2013
    Posts:
    103
    Referrals:
    0
    Sythe Gold:
    0
    Java Basics [2] - Writing Your First Program

    great guide.
     
  4. SuF

    SuF Legend
    Pirate Retired Global Moderator

    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
    Java Basics [2] - Writing Your First Program

    bump.
     
  5. iJava

    iJava .Previously known as RSGoldRush
    $200 USD Donor New

    Joined:
    Nov 21, 2011
    Posts:
    1,197
    Referrals:
    11
    Sythe Gold:
    485
    Discord Unique ID:
    220055593568829441
    Java Basics [2] - Writing Your First Program

    Good guide, well explained.
     
  6. RocketPower

    RocketPower WARROCKPS.COM
    Banned

    Joined:
    May 18, 2013
    Posts:
    843
    Referrals:
    0
    Sythe Gold:
    0
    Java Basics [2] - Writing Your First Program

    thank you for the contribution
     
  7. SuF

    SuF Legend
    Pirate Retired Global Moderator

    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
    Java Basics [2] - Writing Your First Program

    Glad to help.
     
  8. LilZoo

    LilZoo Forum Addict

    Joined:
    Jun 22, 2014
    Posts:
    344
    Referrals:
    0
    Sythe Gold:
    2
    Java Basics [2] - Writing Your First Program

    Not bad , helped me , thanks !
     
  9. SuF

    SuF Legend
    Pirate Retired Global Moderator

    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
    Java Basics [2] - Writing Your First Program

    Thanks!
     
  10. Mindaugas

    Mindaugas Forum Addict
    $25 USD Donor New

    Joined:
    Aug 22, 2014
    Posts:
    298
    Referrals:
    2
    Sythe Gold:
    10
    Two Factor Authentication User Tier 1 Prizebox
    Java Basics [2] - Writing Your First Program

    Thanks for sharing, great guide! ;)
     
  11. Hideindark

    Hideindark Member

    Joined:
    Jul 20, 2014
    Posts:
    74
    Referrals:
    0
    Sythe Gold:
    0
    Two Factor Authentication User Christmas 2013
    Java Basics [2] - Writing Your First Program

    Thanks for the guide! Will be sure to try it out. :)
     
  12. Pain

    Pain Formerly known as Divine
    Banned

    Joined:
    Jun 3, 2014
    Posts:
    51,976
    Referrals:
    11
    Sythe Gold:
    4,836
    Java Basics [2] - Writing Your First Program

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


    Thanks!
     
  13. sav99

    sav99 Newcomer

    Joined:
    Oct 7, 2014
    Posts:
    4
    Referrals:
    0
    Sythe Gold:
    0
    Java Basics [2] - Writing Your First Program

    nice guide , well explained
     
  14. RS3GoldMart

    RS3GoldMart GTA V ONLINE | PUBG | DOTA2 | Runescape

    Joined:
    Nov 28, 2013
    Posts:
    3,820
    Referrals:
    1
    Sythe Gold:
    942
    Discord Unique ID:
    697842166294249584
    Java Basics [2] - Writing Your First Program

    Nice guide for newbies..
     
  15. Timms

    Timms Active Member
    Banned

    Joined:
    Oct 27, 2015
    Posts:
    139
    Referrals:
    0
    Sythe Gold:
    0
    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! =- >


 
 
Adblock breaks this site