The teachings of basic, proper Java - Printable Version +- Support Forums (https://www.supportforums.net) +-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87) +--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18) +---- Forum: Java Programming (https://www.supportforums.net/forumdisplay.php?fid=22) +---- Thread: The teachings of basic, proper Java (/showthread.php?tid=4949) |
The teachings of basic, proper Java - tsgh mike - 02-21-2010 The teachings of basic, proper Java Normally I wouldn't mess around and would just get to it. But I believe at least an introduction is necessary. So here goes. Hello Joe, my name is Blake and I'm posting this tutorial to teach you how to understand and write in the Java programming language. It wont be easy, so don't expect it to be. This tutorial will not teach you how to compile or set up a private server, this tutorial will teach you the basics of Java, nothing less, nothing more. So, where do we begin? Variables In Java, there are certain things that you can create called variables. Technically, a variable is an object that can hold a specific type of data. In other words, a variable is something that can hold a value. Here are the types common of variables, what they stand for, and the type of value they can hold: Code: int - integer - whole numbers under 2.17 billion in size This is a very common variable. It stands for integer, and can hold values of whole numbers less than 2.17 billion (this includes negatives), but cannot hold decimal values. If you try to make an integer bigger than 2.17 billion, your program will crash. long This is the exact same as an int, except it can hold larger values than 2.17 billion (on Windows machines). It cannot hold decimal values. double The double is the exact same as a long, except it can hold decimal values. boolean This is a helpful little variable, although it can only hold two values. The boolean cannot hold numbers, it can only hold two values: true and false. String The String is a special type of variable. It can hold multiple characters to form a series of words or a sentence. So how do we use these variables? In Java, any variable must be explicitly declared. Because of this, Java is sometimes considered a strongly typed language. Don't worry about that though. Just know that before you want to use a variable, you must tell Java that it exists. To do this, we declare our variable. Declaring a variable looks like this: Code: accesslevel type name; The access level is how your variable can be accessed. By default, other packages cannot see or use a variable unless you set the access level properly. A package is a folder that contains related Java classes. A Java "class" is a Java file. Most private servers don't use packaging, so you don't need to be too worried about the access level. Common access levels are as so: Code: public public The access level public means that other packages can see it, hence the term public. private The direct opposite of public, other packages cannot see a variable with access type of private You do not have to specify an access level for your variables. By default, variables are private and cannot be accessed by other packages unless you declare them as public. Type: The type in our variable declaration is the type of variable we are declaring: int, long, boolean, double, etc. Name: Name is simply the name you give to the variable you are declaring. By Java conventions, your variables should start with a lowercase letter, and any other words in your name must start with an upper case letter. The name cannot contain spaces. An example of a variable declaration: Code: public int myInteger; A statement in Java is basically anything you are coding. Anything from declaring a variable to doing advanced math is either a statement or a series of statements. The variable declaration above is a statement. Statements must end with a semicolon unless you are using a code block. A code block is simply, a block of code, starting with { and ending with }. Therefore, for every {, there must be a } to end the statement. Conditional Statements A conditional statement is a statement that will execute only when a certain condition is met. These statements are also called "if" statements. This is the layout for an if statement: Code: if(arguement) Code: int myInteger; Statement one: declaring myInteger. Statement two: setting myInteger to 1. Statement three: this is our conditional statement, it therefore includes the "System.out.println("Yay!")", but only executes it if myInteger is 1. Statement 4: printing "myInteger is equal to one!" into our console. System.out.println(String) prints text to the console. What do you think the output is of this program? Here is what it will say: Code: Yay! Code: int myInteger; Code: myInteger is equal to one! Here is an example of the above code, with a code block instead: Code: int myInteger; Nothing. We have successfully bunched two actions into our code block, and because our myInteger variable is 2, the if statement never executes because it only executes if myInteger is 1. You might have noticed the "=" and "==" characters in the above statements. These are called operators, which brings me to the next section... Operators An operator is something that performs a calculation. There are many operators, all meant for specific calculations. Here is a list of the common ones, and their purpose. Code: + ~ Addition I wont go into detail about these operators for the sake of time, you can figure out what they do by looking at the list above. Now that I've gone over basic conditional statements and variables, lets go onto methods. Methods Simply put, a method is a chunk of code that performs a calculation, and returns a value based on the calculation it makes. Its not as complicated as it seems. A method is just like a variable, it is declared in the same way. The key difference is that a method must have a code block, while a statement doesn't have to. A method is a holder for statements. Here are a few types of methods: Code: void void No return type. int Returns an integer. long Returns a large integer. String Returns a String value. boolean Returns a boolean value double Returns an integer value with decimal points supported, or a double. You may have noticed that the methods return the value that their brother variables can hold. If you made this connection, good job. If not, that's okay. Declaring a method is just like declaring a variable, but you add a code block. Here's the way you declare methods: Code: accesslevel type name(parameters) To make a method execute, we have to call the method by its name and its parameters. Here is the layout of calling a method: Code: name(parameters); Code: public static void main(String[] args) Line 1: our main method which Java calls when it starts the file. Line 2: opening brace for our main method code block Line 3: printing to the console window by calling doAddition(int, int) method Line 4: closing brace for our main method code block Line 5: declaration of our doAddidion method which is the double type, so we can do addition with not only whole numbers but also decimals Line 6: opening brace for our doAddition method code block Line 7: creating a new integer called solution, then setting solution to one + two. Line 8: returning solution, the value that was calculated based on the parameters given Line 9: closing brace for our doAddition method code block The output of the above program looks like this: Code: The value of 5.5 + 5.5 is equal to 11 Why do this when we can just do variable = number + number? Well sometimes we want to do more advanced calculations than just addition, such as the quadratic formula or distance formula. Methods are advanced, custom calculations. About the void. A void is a method with no return type, therefore it does not return anything. The void simply executes the statements within it and finishes whenever it is called. Classes A class, technically, is an object in object-oriented programming. Simply put, a class is usually a Java file that holds methods. This is a slightly more advanced concept than we have previously talked about. A class is declared just like a method, except you must put 'class' as the type. All of your code must be contained within the code block of a class. Here is what a normal Java program looks like: Code: public class HelloWorld When running once compiled, it will say Code: Hello World! Lets move on. Classes can be (here comes a big word) instantiated, or created an instance of. Creating an instance of a class from another class tells that class to run relative to the class that is creating an instance of it. Its complicated, you may have to read that sentence a few times to understand. Basically, you can call a class like you can call a method. But instead of calling the class, you must create an instance of the class. Once you create an instance of the class, you can use almost any method or variable that is inside the class. Here is how you instance a class: Code: classname variablename = new classname(parameters); By default, any class you create has no parameters, so you do not include any parameters in the instance of the class. This is how to create an instance of a class using parameters.The class we are going to instantiate Code: public class SayHello Line 2: opening brace for our SayHello class Line 3: declaring a new string, naming it 'text' Line 4: white space Line 5: constructor. Notice the parameters, we are creating a new variable inside the constructor. Line 6: opening brace of the code block for our constructor Line 7: setting the String text in the outside of our constructor to the parameter that we received when the class was instantiated. Line 8: closing our constructor code block Line 9: white space Line 10: main method called by Java when the program starts Line 11: opening brace for our main method code block Line 12: printing to the console the value of the 'text' String Line 13: closing our main method code block Line 14: closing our SayHello class code block. Instancing the class Code: SayHello mySayHelloClassVariable = new SayHello("Hello, World!"); Code: Hello, World! RE: The teachings of basic, proper Java - Project Evolution - 02-22-2010 Very nicely written tutorial, thanks for sharing. (02-21-2010, 10:43 PM)tsgh mike Wrote: I mentioned before that a method returns a value. Every method returns a value except for a void. Lets go over what they return. I think you got confused here, and its a common mistake either novice Java programmers make, or programmers who dont look at bytecode make. Beleive it or not, void is also return a type! Even though we dont include the keyword 'return' in it, it doesnt mean it doesnt return something. If it didnt return anything, how would it return data? Take a look at the following class and bytecode: Code: class MethodByteCode { Code: Compiled from "MethodByteCode.java" As you can see here, Code: 8: return Also, the term 'void' is a return type(which you just learned). And in the sense your reffering it as, its called a method. I hope you learned something form my own tutorial. ;) RE: The teachings of basic, proper Java - tsgh mike - 02-23-2010 Yea i came up with this pretty fast, i am going to fix it up soon. |