The Complete, Absolute, Definitively Total, and Totally Definitive Java Tutorial; Chapters 1-3

Discussion in 'Resources' started by Maurdekye, Feb 7, 2014.


Did this thread give you a good headstart towards learning to program in Java?

  1. It was great, I learned a whole bunch!

  2. It helped me brush up on some things i'd forgotten.

    0 vote(s)
  3. It was a bit convoluted, but I got the general gist.

    0 vote(s)
  4. It went really fast, and I barely understood most of it.

    0 vote(s)
  5. I'm still trying to wrap my head around the first chapter.

Thread Status:
Not open for further replies.
  1. Offline


    - - Prologue - -

    This is a Java tutorial for the idiots, for the noobs, for the inexperienced, for the "What's an 'if' statement?" people, for Anyone and Everyone who desperately needs to learn Java. I see so many people who want to learn to create their own plugins, but fall short NOT just because they need to learn Bukkit, but because they need to learn Java. And just as many times have I seen (and participated in) redirecting those people towards sites, docs, anything that could get these people out of our hair, simply because we assumed that knowing Java was a requirement to making plugins. Well, unfortunately, it seems that is no longer the case. That is why I am here to teach you, all of you, the very, very, very, VERY basics of using Java. Read on, and discover, for here; only the right people meet. And by that, I mean people who need to learn to program. Let's begin!

    - - Chapter One: Getting your IDE - -

    What is an IDE? It's an Integrated Development Environment. Basically, it's where you write code; your workshop. Notepad is an IDE for writing text files, Minecraft is an IDE for architectural design. Think of it however you like. When writing Java, however, you have to get a little more in-depth with what IDE you use. There are many available: NetBeans, Notepad++, IntelliJ, etc., but the one I will be using this tutorial with is Eclipse. Eclipse one of the more popular IDEs for writing Java code in, and very versatile. Click the link below to download and install Eclipse;

    Download the standard version for your system, which is usually at the top (preferably the 64-bit one), and unzip the folder when it finishes downloading. Extract the contents to a folder on your desktop, and then double-click the purple- circle looking thing to start up Eclipse.

    Note: From here on out, i'll be using Eclipse on a 64-bit Windows machine. If you're not using windows, then too bad; try to follow along as best as you can.

    The first time you start eclipse up, it should ask you to establish your default workspace. Just click 'Okay', whatever it has should be absolutely fine. Eclipse's main window should then open up to display a couple icons. Go to the top right and click 'Go to Workspace', or whatever similar may appear there. Now we can continue to real development.
    So you've done it. You downloaded your IDE and started it up, to reveal a blank-ish page. Your current view should be populated by four main blank sections. The one on the right is your Outline. This isn't all that important right now, especially not to you, so you can just ignore it. The thing on the bottom is the console output. That'll be important later, but just leave it alone for now too. The biggest blank area in the center is the Editor Window. This is by far the most important part of your IDE, simple because it's where you type your code. You can't type anything there right now, but we'll get to that soon enough. And finally, the leftmost blank section is called the Project Explorer; that's where all your files and things are stored. We'll be using this right now; right click on it, and select New > Project..., and in the resulting menu that appears, double-click Java Project. It should be the first thing on the menu. You'll then be brought to another window, where you can name your project. Give it any name you like, then click Finish. A small folder by the name of your project should appear in the Project Explorer. Double-click it to open it, and a couple things should be inside. Ignore everything but the 'src' folder, that's what we're looking for. Right click that, and select New > Package; should be a brown square looking sort of thing. Again, name it whatever you like, but conventionally they have lowercase names. Your new package should appear in your 'src' folder; right click that, and click New > Class, which alternately looks like a green circle with a 'c' in it. In the menu for naming your new class, you'll want to first make the first letter capital (convention), but before clicking 'Finish', go down to 'Which method stubs would you like to create?' and make sure the box in front of 'public static void main(String args[])' is checked. Then you can click 'Finish'. Upon doing that, the text editor in the center should become available to you, and we can finally start writing code.

    - - Chapter Two: Basic Programming Theory & Print Statements - -

    Assuming you followed every step correctly, your window should look something like this;


    Don't worry if any small details are different, all that matters is that the text editor looks the same way. If so, delete the green line (it's unnecessary), and we can begin coding in it's place. Also, make sure that your cursor is indented the same amount as the green line was; it'll help you understand the code better. Now, I can explain what a print statement is.

    1. System.out.print();

    What is a print statement? Simply, it is a line of code that you can add that will show you information. As your first test, type out the line 'System.out.print(5);' where your cursor rests. Congrats, you've just created an incredibly simple program! Crtl+S to save your wonderful program. On the top left, there should be a green circle with a white triangle that says 'Run' when you hover over it. Hit that button, and see what happens.

    Well? What happened? Nothing much? Figures. But wait, what's that? Remember the bottom white bit I told you about earlier? Check it again, you should see something different there;

    That's correct, there is now a '5' inside the white area. This is the result of your program, a single 5; printed down in the output area. That's what System.out.print() does; it prints things. Try putting other numbers in place of the 5 in your print statement, and see what happens. They should all print down in the console when you press Run. If you're feeling fancy, put some plus and minus signs in there with more than one number, and suddenly you've got Java doing simple math for you! Hooray, something functional, finally. And it only took this long to program.

    Well, numbers are fun, but what about words, and sentences, and phrases, even paragraphs? Well, what about them? Try typing some in, in place of the numbers in the print statement. What's this? They're all underlined in red, like misspelled words! But you're sure you spelled them correctly, so how can this be? The answer, is of course, datatypes.

    In any programming language, information is organized into different datatypes. There are many, many many types of data in Java, but a few are the most basic. These are the ones i'll be talking about. The first datatype which you'll have learned is 'int'.

    1. 53 + 27 - 983 / 10

    What is an int? It's a number; an integer, to be exact. Java recognizes 'int's as any normal number without a decimal. All of the examples listed above are ints, including -4000, 320, 987, and 623492435. You can add, subtract, and multiply them to your heart's content, but in dividing them, you'll see some of the flaws of ints. If you tried to use division up there while you were testing out your numbers, you might have noticed that the console didn't print out the actual answer to your problem; if you put 7/2 in there, it would have printed 3, when you expected it to print 3.5. This act of removing extra information past the decimal point is called Truncation. Generally, it is to be avoided, simply because of the loss of information, and i'll describe ways to avoid it later. But for now, just know that an int can never have decimal points. Unless you put them there. But at that point, it is no longer an int; it is a double.

    1. 0.999 / 28.2 + 55.0 + 3.1415

    Datatype #2: double, short for double-precision floating point integer, is another type of number. Don't worry about what all that name mumbo-jumbo means, because all you need to know is that it's a number that has decimal points. Up to a limit, of course (around 23 digits past the decimal point, or so). This is how you can divide with precision, collect data, and emulate percentages. If you need something precise, use a double. To specify a double apart from a lowly int, simply add a decimal point, and a digit. For example, 20 is an int, while 20.0 is a double. Java will process division correctly if at least one of the numbers in the equation is a double. For example, 9/4.0 will give the answer 2.25, as will 9.0/4. But if you just put 9/4, you'll still only get 2, due to that truncation we talked about earlier. But enough about numbers, let's go back to out original problem; how to print out words.

    1. "Hello world, my name's Rick."

    What you see above is a string of characters, or as it's more commonly known, a String. The quotes you see aren't just for show either. Strings contain any number of words, letters, sentences or anything else similar, all packed within a neat set of double quotes. In almost any programming language, you have to distinguish Strings of text by surrounding them in quotations; that's how Java knows it's a String, and not something entirely different. How about you try this out now? Put quotes around the text you had in your print statement from earlier, and the red underlines should disappear. Hit Run, and see what happens. Did it work this time, because it should've; you should see the text you entered into the statement printed clearly into the console.

    1. true, false

    Here's one final datatype, and by far one of the simplest and most important of them all; boolean. Named after George Boole, the creator of the concept, a boolean is simply either true or false. That's it. There are only two possible values for this datatype, but it's still one of the most important datatypes in Java, mainly because of it's simplicity. It's importance will become apparent once we reach 'if' statements, but until then, just sit tight and keep booleans in the back of your mind.

    The most basic concept behind programming is that it occurs in steps, going from top to bottom. Every line is a new instruction, and each instruction does something different, all towards an intended purpose. Java, unlike humans, doesn't real separate lines by their vertical separation, but rather, by the semicolons in between them. You'll notice that in the print statement I gave you to write, there's a semicolon at the end. Every line in Java must end in a semicolon, that's how Java tells one instruction from another. You can have multiple instructions per line if you really want, but they must all be separated by semicolons. The only exception to this rule is that any line ending in curly brackets does not have to have a semicolon on the end; you'll notice that the line above your print statement doesn't have a semicolon after it.

    Right now, you have a single instruction running into your program; your print statement. This is a simple instruction that, on a basic level, prints info to the console. We're going to make another. Move your cursor to the end of your statement, and hit enter. Then, begin by typing out a second print statement, exactly the same as the one prior. You can put whatever you want in this one as well, but make sure you put something. It might look something like this;


    After you're done typing, imagine what you think will happen. Will it print your first statement on the first line, and your second statement a line below that? Will it print your first one, pause, and then erase it and print your second one? Well, why don't we find out - press the Run button, and watch what happens.

    Now, I bet you didn't expect that at all - it just glued them together! Not even a space or a comma between them or anything. This happens because although you told Java to print your second statement to the console, you didn't tell it anything about where to print it. So Java just assumed to keep printing on the same line. That's not very pretty, but there is something that can be done about it. Change your first print statement to this;

    1. System.out.println();

    and then hit Run. Tadaa! Your messages are on separate lines now! This is because using 'System.out.println();' instead of 'System.out.print();' gave Java a little extra instruction to process at he end of your first line; the instruction to start typing on a new one. From now on, if you want to print messages, I suggest you use 'System.out.println();' instead, as it will print every message on a separate line.

    Note: By now, you've probably been running your programs a lot, and wondering if there's an easier way than hitting that annoying green button in the corner each time. Well good news, there is! If you simply press F11 on your keyboard, it'll run the program without you even having to move your mouse. Convenience!

    Add as many println statements as you desire, if you really feel like it you can create a big ASCII banner out of them. Eventually though, you might want to do something different, say, print something like this to the console;

    Five plus three equals 8
    but you want it to calculate the answer for you. So naturally, you'd do something like this;

    1. System.out.println("Five plus three equals 5+3");

    But what you'll find is that it prints out exactly that; it doesn't calculate the answer, it just prints 'Five plus three equals 5+3'. But you wanted it to calculate the answer for you! Well, you put it inside the quotes, so that makes it into a String. Java reads all strings literally, AKA as they appear, so it won't perform any special calculations on numbers you put inside strings. Well, the answer seems obvious from here, take the numbers out of the string.

    1. System.out.println("Five plus three is " 5+3);

    And at this point, you begin to get errors. Fixing these errors requires that you learn a new concept; contatenation. Concatenation is the act of putting strings together. Try it now, by adding a plus symbol between your string and the numbers in your print statement;

    1. System.out.println("Five plus three is " + 5+3);

    And almost magically, the errors have disappeared! Run your program, and see what happens;

    Five plus three is 53
    Hm. That's not right. You're certainly getting closer, but you're also pretty sure five plus three is not fifty-three. To explain this, you'll have to know what happens when you add a plus symbol in front of a string.

    Any single object (a number, or string) can only be of a single datatype at a time, as much as it seems like these numbers can morph between Strings and ints as you add and subtract them into the line. Normally, any one datatype represents exactly what it stands for; an int is an int is an int is an int. But, if you try to put two different datatypes together, say a String and an int, even though it seems like common sense to us humans, Java gets all flustered and has to decide what datatype they should both be. Obviously, it can't turn the String into a number (you can't add 5 to pie), so it has to turn the int into a string. It glues them together, and you get something like '5pie'. Now, let's apply this rule to your above statement. Java will see this input;

    1. "Five plus three is " + 5 + 3

    And try to concatenate the string and the first int together. The result would look like this;

    1. "Five plus three is 5" + 3

    Now, you might start to see the problem. Java now only has one int and one string to deal with, and so it simply concatenates them together, and you get the result we got previously;

    1. "Five plus three is 53"

    Now, you could implement something over complicated and way over your head to make Java interpret the line of information from right to left, but a much easier way to do it is to add some parentheses;

    1. "Five plus three is " + (5 + 3)

    When Java sees parentheses, it automatically goes to calculate everything within them before everything outside. This has it's obvious advantages, as when calculating the above statement, Java will, instead of looking at the String and one of the ints first, will look at the two ints together and add them to each other instead of the string. Then it will glue that to the string, producing this result;

    1. "Five plus three is " + 8
    2. "Five plus three is 8"

    Now, finally we have out correct answer. Put this in the println statement, and you should see that it works correctly now.

    That's all fun and games, but you've only been playing with mere print statements so far. In this following chapter, we get into the meat of programming; variables.

    - - Chapter Three: Variables - -

    Up until now you've been playing with simple words and numbers, and using them once - in your print statements. After that, they can't be accessed again, and they become nonexistent, and invisible. What if you had a number you calculated that you want to use in multiple other calculations? What if you have a constant that you want to easily access whenever you use a calculation, but you don't want to keep typing out, like pi or theta? Well, i'm here to tell you about variables. Variables are, in simplest terms, boxes with labels. Every variable has a name (it's label), and a value (whatever's in the box). In Java, you as well have to define the shape of the box so that it can fit different datatypes. A single variable can only contain one datatype once it's been created, so it would make sense that you have to tell Java what shape the box is when you create it. You can have int shaped boxes, double shaped boxes, String shaped boxes, boolean shaped boxes and many others too. But enough of the specifics; you came hear to learn Java, not to talk about it.

    To create, or define, a variable, you first type it's shape designation, then you type it's name, and then an equals sign. After the equals sign, you put the thing you want to put in your variable box; an int, a String, whatever you want, as long as it's datatype fits the shape of the box. And end it with a semicolon. Never forget the semicolon. Here's an example;

    1. int myvariable = 36;

    This variable is of the shape, or type, 'int', which means it can only hold ints. It's name is 'myvariable', which means that to look in the box, you have to reference it with this name. And finally, you have '= 36', which puts the int 36 in the box. Some more examples;

    1. String welcome_message = "Welcome to the program!";
    2. double PI = 3.14159265359;
    3. boolean isOnFire = true;

    These are all valid examples of variables. Do note that the names of variables cannot have spaces, nor can they start with a number. Other than that, you can give your variables any name you like, as long as you like. Don't make them too long, though; it'll only be to your detriment when you have to type them out again.

    Variables with things in them can act in the place of normal objects, like ints and Strings. Just put the name of a variable in it's place, and it'll substitute the variable with whatever is inside it. See this example below;

    1. String some_random_string = "fried rice";
    2. System.out.println("I'd love to eat some " + some_random_string + " tonight.");

    Running this should print out the following;

    I'd love to eat some fried rice tonight.
    Because Java substitutes 'some_random_string' for the String it represents, AKA "fried rice", Java prints it out as though it were three strings concatenated together, and gives you the previous result.

    You don't have to just give variables singular numbers when you define them either. You can put equations, concatenated strings, heck, you can even define variables with other variables. Here's a simple example below;

    1. double solution = 327.98 / 929.005;
    2. String solutionToPrint = "The solution is " + solution;
    3. System.out.println(solutionToPrint);

    If you were sharp, you may have noticed an alternate solution to a problem we recently dealt with. Remember the parentheses? If you define the answer in one variable, and then take that answer and glue it to a string, you've conveniently defined the order of operations that Java should take and inadvertently solved the problem of concatenating equations to strings. This is another great use for variables. Also notice that even though the word 'solution' was inside the string in the variable solutionToPrint, it wasn't substituted for the value of the variable solution. This is another example of Strings being literal.

    Variables carry specific values which are given to them when they are defined, but that doesn't mean that they can't ever change. If you want to change a variable, you do basically the same thing as when you define it; except, you don't have to give the datatype of the variable, because Java already knows that. Here's an example;

    1. int myInt = 56;
    2. System.out.println(myInt);
    3. myInt = 22;
    4. System.out.println(myInt);

    The first print statement, you would expect to print out 56, simply because that's what's stored in myInt. But the second print statement, you're not sure what it should print out. Will it be 56, or will it be 22? On the third line, the variable myInt is overwritten to carry the value 22 instead of 56, so it represents that number every line past that one, including the second print statement. So in that case, the second print statement would in fact print 22. So where did the 56 go? It disappeared, because it was deleted when the variable was assigned a new value. There's no way to get that 56 back after you've overwritten it. Aside from removing the line where you overwrite it, that is.

    One of the uses for this is not only setting variables to different values, but actually referencing the variable itself when redefining it. If you were to do something like this,

    1. int somevariable = 32;
    2. somevariable = somevariable + 1;

    It almost seems like some sort of unrealistic paradox, and that it would freeze and crash your computer! But it won't, and you don't have to worry about that. In fact, if you were to add a print statement after that line, you'd find that it changed the variable successfully, to a separate number; 33.

    First of all, why doesn't this crash my computer? And second of all, why did it give 33? I don't see a 33 anywhere, do you? Only a 32. Well, to answer your first question, the way you imagined this going was that Java would evaluate the left side equal to the right, and then the right side equal to the left, and the left equal to the right, and the right equal to the - you get the picture. And it would go on forever; at-infinitum. But it doesn't, because once again; Java doesn't think like you do. Instead, Java only evaluates the right side of the equation, completely ignoring the left. In fact, the left side can't even be an equation; it always, always must be a variable. Java evaluates the right side before doing anything else, and then simply applies it's result to the variable on the left. In this case, it saw 'somevariable + 1' on the right, for which it substituted somevariable for 32 (because that's what it represented at the time), and then added 1 to it. After tall that, it then set the value of somevariable to the result it got, and continued to the remainder of the program. The result is, of course, 1+32, which is 33. And there's the answer to your second question! It's certainly been a productive paragraph, hasn't it?

    Building on the concept of forced order-of-operations introduced a few paragraphs up, we can easily perform complicated mathematical problems one step at a time, making them much easier to read. Here's an example;

    1. double gravitational_constant = 9.81;
    2. double mass_object_one = 33;
    3. double mass_object_two = 26;
    4. double distance = 9;
    6. double base = distance ^ 2;
    7. double multiply = mass_object_one * mass_object_two;
    8. double step = multiply / base;
    9. double result = gravitational_constant * step;
    11. System.out.println(result);

    That right there was Newtons law for Universal Gravity. Normally a complicated formula, but made surprisingly easy to understand just by breaking it into separate lines. This is another great power of using variables.

    Now, let's say you have a simple problem, including two ints;

    1. int variable1 = 22;
    2. int variable2 = 7;

    You want to get a very specific answer from dividing them by each other, but you know that if you divide them as-is, your answer will be truncated and inaccurate. So, what do you do? Well, your first thought might be to change the datatype of one or more of the variables, but for the sake of our current problem let's assume we can't modify these variables in any way. Well, what now? Seems hopeless, right? Not unless we have casting!
    Casting is a simple operation you can use on an object, like a number or a letter, to turn it into another datatype. Not permanently, mind you, unless you do it while redefining the variable. A cast is very simple to perform; you just put the datatype you want to cast to in parentheses in front of the object you want to cast. Here's an example;

    1. System.out.println((int) 50.62);

    You can clearly see that the object inside that print statement is a double. However, we've added a cast in there to cast it to an int. This truncates the decimal information off the end of the number, and prints it as though it were an integer;

    We can use this to our advantage to solve our problem above. If you were to divide them normally, as below;

    1. System.out.println(variable1 / variable2);

    We should expect to get a truncated answer, because ints can't have decimal places. However, if we were to cast one of the variables to a double;

    1. System.out.println((double) variable1 / variable2);

    Then it would give more accurate information, because we aren't only dividing by ints anymore!
    Casting isn't ever really completely necessary, but it's very convenient, especially when dealing with a large variety of datatypes.

    That was a bit long, but I promise you, there is much more coming up. And it's going to be much better than variables.

    Click Here for Chapters 4-6 of the Complete, Absolute, Definitively Total and Totally Definitive Java Tutorial.

    -- More Coming Soon! --

    Chapters to write;
    • Loops
    • Methods
    • Objects
    • Classes

    If you think there needs to be more detail in a section or chapter, please let me know in the comments!
    Wizehh and ferrybig like this.
  2. Offline


    Awesome. I hope to see more chapters soon
  3. Offline


  4. Offline


    Garris0n I made this because so many people don't know how to teach programming. They throw too much at you too fast, and don't explain enough. This guide aims to remedy that.
  5. Offline


    But this guide is written by the people most qualified to write it.
  6. Offline


    Garris0n And THIS guide is written by the person who thinks those qualified to write one don't know what their audience is.
  7. Offline


    I'll give you credit for taking the (what seems to be) large amount of time to write this. The only thing is, people here on the Bukkit forums are not responsible to teach people who don't know Java the language. Instead, it's better to refer them to Oracle's documentation, as Garris0n said, and maybe recommend a good book.
  8. Offline


    The_Doctor_123 All I'm saying is a bukkit forums post is not going to be as in-depth and explanatory as the official documentation.
  9. Offline


    Garris0n And all i'm saying is that an in-depth and explanatory guide can't possibly be understood by the general mass.
  10. Offline


  11. Offline


    Thank you for uploading this resource. The people that aren't too lazy to look for this won't be asking stupid questions anymore :)
    LegitJava likes this.
  12. Offline

    Captain Dory

    Don't know why this is getting so much hate. When I started learning java, the docs were so scary I almost quit. It's people like these who make people realise that java isn't that hard once you understand what you want to use.
  13. Offline


    I like this tutorial, it's pretty basic but it's definitely informative, looking forward to more chapters!

    Got to agree with this. I know it's not the bukkit communities job to teach everyone java, but java is a rather broad language. I would be lost without a way back if it wasn't for so many of the helpful people on the bukkitdev IRC and forums. Thanks guys!
  14. Offline


    Very well made up, it looks really good. I think it has a good difficulty curve so that beginners can keep their attention while reading ;)
Thread Status:
Not open for further replies.

Share This Page