Adblock breaks this site

Basic java tutorial.

Discussion in 'Programming General' started by Ramen, Mar 13, 2009.

  1. Ramen

    Ramen Active Member

    Joined:
    Jan 20, 2006
    Posts:
    138
    Referrals:
    0
    Sythe Gold:
    0
    Basic java tutorial.

    I see people constantly requesting other peoples help due to an inability to program on their own, so I figured I'd lay down the basics of Java. If you already know java, no need for you to continue, this will bore the hell out of you. For beginners, pay close attention and read this carefully to fully understand.

    What is Java?
    Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities. Java applications are typically compiled to bytecode that can run on any Java virtual machine (JVM) regardless of computer architecture. (source : http://en.wikipedia.org/wiki/Java_(programming_language))

    What will you benefit from this tutorial
    If you pay close attention and read this fully, you should be able to learn the basics of Java programming. Enough at least to begin working on one of the public RuneScape private servers sources, anyways.

    Alright! Enough of this, let's begin.

    Part 1: New file.

    To create a new file, you must ALWAYS name the file the same name you see in the first line of coding. For example, if the file was named "NewClass.java", the first line of coding (besides imports), would look something like this:

    Code:
    public class NewClass {
    Anywhere beneath this is where you can put any source coding. Declare variables, methods, etc. below this point. Here is an example class file:

    Code:
    public class NewClass {
    	
    	public int exampleInteger = 0;
    	public boolean exampleBoolean = false;
    	public String exampleString = "This is a line of text.";
    	public char exampleCharacter = 'a';
    	public String[] exampleStringArray = {"First line of text.","Second line of text."};
    	
    	public void exampleVoid1() {
    		exampleVoid2(true);
    	}
    	
    	public void exampleVoid2(boolean boolean_) {
    		if (boolean_) {
    			System.out.println("The variable 'boolean_' is"+boolean_+".");
    		} else {
    			return;
    		}
    	}
    	
    	public boolean exampleBoolean2() {
    		if (exampleBoolean) {
    			return true;
    		}
    		return false;
    	}
    	
    	public static void main(String args[]) {
    		// Main program execution
    	}
    }
    
    Part 2: Breaking it down.

    In the above you will see five different variables. You should see an integer, boolean, string, character, and string array. Let's take a look at what each of those actually does.

    Integer:
    An integer is a whole number which value is less than or equal to the Java max integer (a bit over two billion). It can not be a decimal, and is declared as "int".

    Boolean:
    A boolean is a basic on-off switch. It can only be true or false. It is declared as "boolean".

    String:
    A string is generally a phrase and is contained in quotation marks. It is declared as "String".

    Character:
    A character is an alphabetical or numerical character. For example, the letter A would be enclosed in single quotation marks such as 'a'. It is declared as "char".

    Arrays:
    An array is a little trickier than the others simply because it can return more than one value. Let's take a closer look at the String[] in the example class.

    Code:
    	public String[] exampleStringArray = {"First line of text.","Second line of text."};
    When creating an array (of any type), it is enclosed in brackets and the information on the inside is separated by commas. The first piece of data an array ALWAYS starts at zero, not 1. For example, if you were to call upon exampleStringArray[0], it would return "First line of text.", just as exampleStringArray[1] would return "Second line of text.".

    Voids:
    A void is generally a block of coding where several commands are executed inside it. You can do just about anything inside of a void. A void can also be declared with an argument. (See exampleVoid2 in the example class).

    Executing a class file:
    To have an executable class file, it always has to contain this block of code:

    Code:
    	public static void main(String args[]) {
    		// Do any arguments here.
    	}
    Where the commented area (The text after the "//") is where you would put any coding required to boot up the program. For example, to make a program state "Example program." in the debug window, the main void would look like this:

    Code:
    	public static void main(String args[]) {
    		System.out.println("Example program.");
    	}
    Another key factor when programming is that every block of code where you add a "{", you ALWAYS have to have a "}" later on to close the block.

    This tutorial has been a basic example of declarations, making a basic class file, executing a class file, and making blocks of code inside.

    Further information

    Say good bye to notepad! Get an IDE. Some good ones are Netbeans, Eclipse, JCreator, and many others.

    Is you java up to date? http://www.java.com/en/download/index.jsp

    Do you have the Java Development Kit (JDK) required to start java programming? http://java.sun.com/javase/downloads/index.jsp

    I hope this tutorial will help some people interested in beginning java programming. It's simple, but it should clear up what's what in those files you look through.

    Happy programming.
     
  2. 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
    Basic java tutorial.

    Decent tutorial, but.... Give credit to wikipedia for the bit you stole......
     
  3. hampe-92

    hampe-92 Forum Addict

    Joined:
    Jul 10, 2008
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0
    Basic java tutorial.

    lol Copy & paste the first lines...
    but anyway.. it's a litle help to th STPF-project...
    but you left out one important part, almost every line of code shall end with a semicolon ( ; )
     
  4. Ramen

    Ramen Active Member

    Joined:
    Jan 20, 2006
    Posts:
    138
    Referrals:
    0
    Sythe Gold:
    0
    Basic java tutorial.

    Thanks for the replies.

    @SuF : Cheers for that, totally forgot to include that.

    @hampe-92: You make a valid point; thank you for the contribution.
     
  5. 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
    Basic java tutorial.

    Statements end with a semicolon... And BTW... Its not arguments in the main method, its statements.......... :O
     
  6. Ramen

    Ramen Active Member

    Joined:
    Jan 20, 2006
    Posts:
    138
    Referrals:
    0
    Sythe Gold:
    0
    Basic java tutorial.

    To clarify, you need to deal with any arguments when starting up the program.

    Code:
    public static void main(String[] [b]args[/b]) {
    ...
    }
    The String array "args" contains the start up arguments. Any other information may be non-public declarations, statements, debugging, etc.
     
  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
    Basic java tutorial.

    thats if you use the args... and you deal with the arguments with statements...
     
  8. &#64327;&#64303;&#64323;

    &#64327;&#64303;&#64323; Member
    Banned

    Joined:
    Mar 12, 2009
    Posts:
    97
    Referrals:
    0
    Sythe Gold:
    0
    Basic java tutorial.

    nice guide, you should post more guides in the user education section. gl with outher guides
     
  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
    Basic java tutorial.

    Programming guides go in the programming forum... I am trying to get the programming section in the ue removed... :eek:
     
  10. jeppa

    jeppa Newcomer

    Joined:
    Mar 17, 2009
    Posts:
    20
    Referrals:
    0
    Sythe Gold:
    0
    Basic java tutorial.

    nice guide
     
  11. XxJamesPoisonxX

    XxJamesPoisonxX Member

    Joined:
    Mar 11, 2009
    Posts:
    38
    Referrals:
    1
    Sythe Gold:
    0
    Basic java tutorial.

    Thanks =]

    I didn't know any of this and yet i was still going inside my files for my rs2 server and figured things out...

    Not saying im like pro but i can do the nooby things on my server lol
     
< More Help needed by Esuom... | Final Exam Project Ideas >


 
 
Adblock breaks this site