Jump to content
Rs Bots Pro

[JAVA] Beginners Guide To Java by RSBP

Recommended Posts

Hello everyone, i love to help out our community and as such i am willing to teach all users on the forum if they want, how to use java. I will be starting with the most basic form of java you could imagine and day by day progress to more and more advanced techniques. Below find a list of the tutorials so far:

 

Lesson 1 - Getting Started - Post 1

 

Lesson 2 - Introduction - Post 2

 

Lesson 3 - Variables - Post 3

 

 

Lesson 1

Getting Started.

 

In this tutorial we will be downloading the java development kit and a program called netbeans.These two things are going to allow you to jump right in and start learning Java.So the first thing you want to do is use a search engine such as google. Search for JDK

the first result should be something along the lines of Java SE Downloads @ oracle.com click on that link. You will see the below:

 

jdkpicturefw.png

 

You want to click on the one that says NetBeans. ( JDK + Netbeans ) After clicking on that image/sqaure box you will come to a download page. Cick on the radio circle button that read "Accept License Agreement " then click on the appropriate installer for your operating system. Once its finished downloading its the usual installer, choose the location and accept the terms an conditions.  So once you have that installed and you know it set up correctly you can move onto the next step.

 

We will need to set up something called Environment Variables.  The way were going to do this is by clicking on Start then right click on computer to bring up a menu and then click on Properties. Once a new window opens up your going to click on 'Advanced System Settings' on the left side of your new window usually the bottom option in a list of 4 or 5. Another window around half the size of the last one will open and be called System Properties. If you on windows XP you will be on the first Tab called computer name and will need to click on the tab named Advanced. Anyone on xp or above should already be on the advanced tab. So click on a button near the bottom of the advanced tab called ' environment variables '  This will open an even smaller new window. What were doing here is providing java with the path to where it was installed, this basically means that when you run a java program it knows where to find the compiler and other nice things java can do.

 

So now leaving what we just opened aside for a moment dont close any of the windows. Click on start and click computer again. and go to the location i specify now: Computer >local disk > program files > Java > JDK 1.7? > bin. now make sure you click on program files not the x86 one and make sure if you have multiple jdk installs that you click on the latest version. If you have windows 7 you can right click on the address bar at the top and click ' copy address as text '  if you don't have windows 7 right click on any file in the bin folder and click on properties. In the general tab you will see the location of the file. You want to highlight this and copy it by either highlighting it then right clicking and selecting copy or use the short cut after highlighting it Ctrl+C

Once you do that you will want to go back to your Environment variables window we opened just a moment ago and click on the New button. ( the top one ). In the variable name box type PATH in capitals. Then right click in the box called Variable Name and select paste to put in the location of the bin folder. You can then hit ok, ok, ok to close all three windows.

 

So now that's all done we are going to test the installation of java and your Environment variables. Click on start once more and open up notepad (can be found under the Accessories folder ) Once its opened copy and paste the code below directly into notepad. Don't worry about whats going on in the code at all you will learn all this in the tutorials to come.

 

public class Test {	public static void main (String [] args) {		System.out.println("Hello World!");	}}

 

So now you should have this in your notepad file? great. Click on File at the top and select Save As. In the file name box type "Test.java" Yes do include the " " the parentheses are to make sure that the file is saved as a .java file and not as a .txt file. Just save it to your desktop for now so you can find it easily. So now you should have a little rectangle on your desktop with Test.java below it. Excellent! Now on to the next part, Press on the keyboard the windows key + R this will being up the run. Type into the open box 'cmd' without the single ' ' ' ' '  on each end. Now a big black box will come up named the command prompt. The last line in the box should say C:UsersYourUserName where YourUserName is your... you got it. We need to change the directory from your name to the desktop so type ' cd desktop ' you will see the directory change to your desktop. Now type ' javac Test.java ' the cursor will flash for a few seconds and then just show the directory once more if it has compiled correctly. What the compiler does is turn our code into a language the computer can understand and use. You will notice you have two files on your desktop now one called Test.java and now another called Test.class if you want you can open that in notepad and you will see all the weird symbols the computer uses. So now we have our .class file we can now run our program by click back onto the black cmd window and typing in ' java Test ' don't forget the capital T. We can see it now prints out Hello World! if you don't see this either because you changed the text or you get any errors or something like java does not recognized i recommend you go back and make sure you path variable is correct and your JDK is fully installed and if you get error messages such as cannot find symbol you should make sure you copied the code above exactly as i typed it with capital letters in the right place and symbols in the correct place.

 

Thats it for this tutorial, in the next lesson we will be doing an introduction to netbeans and going over the very basics of java.

 

Like this post if you followed it fine or liked what you read so far.

 

i will post the next one tomorrow smile.png

Edited by Rs Bots Pro
  • Like 7
Link to comment
Share on other sites

Lesson 2

Introduction

 

In this tutorial i will be explaining the basics of netbeans and its directory structure which it creates when we compile java applications. We will also be briefly touching on some concepts about java that you need to know. So first open up netbeans. Once its opened were going to make a new project. We can do this by pressing either the new project button which is orange in colour and found under the Edit tab at the top left of the window. Or you can press File then select new project. Once the new window opens we can select from the left column Java, and from the right window Java Application. Press next to move on, we now need to give the project a name and a location, i would recommend something memorable and a location such as my documents or your desktop. You will want to un-check the bottom check-box which says Create Main Class we don't need this as i will be explaining this at a later date. Then hit finish to make up your first project folder.

 

once your window has closed back to the main window you will see you project has been created in the column on the left with the name you gave it. You can see it is in bold. if you have multiple projects you main one will be in bold so when you press compile that is the one that will be used. Next to your project name you will see a little box with a + in a square box if you click that it will show all the files inside your project. In the source files folder you wont see anything as we haven't made any yet. In the other folder called libraries will be the JDK you installed earlier, this contains all the various different files java needs that have already been made by the creators of java themselves.

 

So to begin you want to click on the default package which is in grey and select other near the bottom of the menu. A new window will open. From the categories column select java and in the right File Type column select Java Main Class. This will hold the main method which i will explain in just a moment. Hit next to go to the next page where you need to name your Main class. I would name it Tutorial. Note it is important to use good java conventions whilst naming classes. What i mean by this is to Start each word with a Capital letter and no spaces between so 'Tutorial' or 'ThisIsMyTutorial' you can have letter, number or underscore in the name but no spaces and the classes cannoy begin with a number; letters or underscores to start is fine.

 

Now if you open up your default package on the left you will see that your main class has been created for you and if you click on it you will see it open on the right side in the main window. Now you should abit of generated code which netbeans has kindly already pre-made for you, you can highlight all this and delete it.

 

So now i am quickly going to go over the files that netbeans will generate for you when you create a new package, project, classes etc. If you go to the place where you created your project folder for example i saved mine on my desktop for easy access you will see in the main folder 3 other folders and 2 files. Now these hold all the different folders that your project needs to run. If you open up the src folder you will see the java main class we just created. So if you ever need to find your source files in the folder outside of netbeans you now know where they are.

 

So now we shall begin to learn some java! the very first thing you should now is that all our code goes inside of classes. A simple analogy of this would be to look at a class as a folder. All the code must be stored within this folder so that it knows where to find it. Well how do we make a class. Type the word class into the main window and you will see it changes to a blue colour this is because class is a keyword in java , next we need to give it a name this is always the thing java will look for after you type class. A very important thing to know right now is that the name you give must be the same EXACTLY to the file name you gave your class. So from above it would be ' class Tutorial ' note the capital T. These rules regarding class names will change slightly as we learn more but for now just remember it must be the same as the file name. You will see that we have a red circle next to what we have typed so far and that's because we need to open and close our class. We do this by using pointy brackets: {  When you see them being used it will be like this:

 

class Tutorial {}

When you hover over one of them in in netbeans it will highlight its partner if there is one. So now we have made a main class and named it Tutorial and we have opened and closed brackets. We refer to the area between brackets as inside of a certain area or method. Next we need to give the program a starting point else it could start anywhere. This starting point is represented by something called the main method. You might recognize it from the last tutorial and we will go over it in more detail very soon. Every single main class will have a main method with a few exceptions, it will look something like this:

 

class Tutorial {	public static void main(String [] args) {			}}

 

now when you look at the line of code i added you may think it looks really confusing and you want to quit java and thats that. Well don't it really isn't that hard at all. All's you need to know for now is that the code inside the public static void main is the code that will be executed first. Now were just going to make the most basic program possible by printing something out to the screen. Go ahead and type out the code below by yourself and make sure everything is in the right place. It's very important you learn to type it out yourself and not copy and paste.

 

class Tutorial {	public static void main(String [] args) {		System.out.println("Hello World!");	}}

by now i hope you have had a go at typing out the code in the box above. Lets just take a look at some of the key things you should of put in that a lot of people miss out. Make sure there is a capital S for System, make sure to use a semi colon ; after the closing bracket. A short note on semi colons. Every line of code in java that is in between those spikey brackets should have a semi colon at the end of it ( that means if your not declaring a class or method with spikey brackets ) Also make sure your using double quotes either side of Hello World. Most new java users will find remembering public static void main... and the rest of that line the hardest part. So now you have your first and very basic java program. You should be very proud of yourself!

 

For now you don't need to know how any of the above works. I would recommend that you memorize that second line public static void main into your head, everything about it. If you can crack that as soon as possible you will be all good. I will end the tutorial by getting you to run your first program and to do that you click on the green arrow shape at the top of the screen underneath profile and team tabs. You can also press on the keyboard Shirt+F6. To see the output click on the word output at the bottom left of your window.

 

That's it for now in the next tutorial tomorrow i will be teaching you about the basics of variables.

 

If you liked this post please Like it using the green button to the right of this sentence smile.png

Edited by Rs Bots Pro
  • Like 4
Link to comment
Share on other sites

Lesson 3

Variables

 

Hello welcome back to my tutorials if you made it this far your doing ok, now it really starts! Ok so in this lesson we are going to look at the basics of variables. The easiest way to think about a variable is by using the matchbox analogy. So imagine in your head to matchboxes, one with a label saying name and the other saying age. Each box represents a variable. So what happens when we slide out each matchbox? Each box has a value inside of it. The matchbox called name has a value of 'rs bots pro' inside of it and the age box has a value of '21' inside of it. The point of this analogy is to show the label of the variable can be a generic thing such as a name but whats inside the matchbox can be anything you want. That is going to be the easiest way you will learn what a variable is.

 

So now were going to put that analogy into practice. If you have a look at the code below you will see how we declare variables, but first we need to learn something very important as it will help later on. Something you will hopefully use alot after i teach you this is something called a comment. There lines of code that dont get used by the compiler, there ignored. The point of them is to remind you about what you have written and to help out others should they be looking through your work. So the way you do comments in java is my simply putting two forward slashes // and then your note. An example would be:

 

class Tutorial {	public static void main(String [] args) {		// this is a comment!		}}

So now you know how to use a comment i will show you how to declare variables. So the first thing we need to write for a variable is the variable type and much like we did with the keyword class we gave it a name we need to specify the type of variable which is usually a built in java keyword and then we give it a name, however its not called a name its called an identifier. so you will have:

 

class Tutorial {	public static void main(String [] args) {		// Variable-Type, identifier;		}}

notice the semi colon on the end of the line where i have commented, those three things are technically all that is required. An optional step when declaring our variables is to give it a value. We do this by putting the assignment operator which is just a fancy saying for the equals symbol, then we can give it an appropriate value. So now lets put all this into practice by declaring a variable. First the variable type, the most basic and most used variables in java are those that hold numbers, these come in many different forms, theirs about six of seven different types for java numbers. The easiest one to think of is an integer. An integer is basically a whole number, it doesn't have any decimal points if you give it any decimal points it will either remove them or give you an error. So the way we declare an integer variable in java is by typing 'int' if you go ahead and type it into netbeans below the public static part you will see it lights up in blue as its another keyword. So int is our variable type and integer. Next we need to give it an identifier or a name so for now i shall call it 'a' like so:

 

class Tutorial {	public static void main(String [] args) {		// Variable-type, identifier; assignment operator, value			int a	}}

Its probably bad programming practice to call it something like 'a' because we don't know what that means.  But for the sake of ease im just going to use 'a' so now that we have the variable type and a name you can put your semi colon in. You will now see that our variable has successfully been declared and is sitting waiting to be used. So this optional step is to give it a value, we do this by pressing space after the identifier then typing the equals sign, another space and then the value followed by your semi colon. example:

 

 

class Tutorial {	public static void main(String [] args) {		// Variable-type, identifier; assignment operator, value			int a = 25;	}}

 remember that the int has to be a whole number, i have chosen 25. have a go at giving your integer a value now. Ok now let me explain more depth what the assignment operator does. It assigns the value on the right hand side to the value on the left hand side.  So when ever you use the equals sign its going to put the value on the right into the value on the left. the only limitation to this is the value on the left must always be a variable. You can however assign the value on the right to be something else other than a number. For example you can give a variable a value of another variable. This may seem confusing and you might be thinking why the f**k would you do that, all will become clear later.

 

Were now going to print out our variable using the println feature we saw in tutorials 1 and 2. Press enter once or twice after your semi colon and type out what you see below. no copy and pasting ;)

 

class Tutorial {	public static void main(String [] args) {		// Variable-type, identifier; assignment operator, value			int a = 25;		System.out.println(a);	}}

What that's going to do is run the lines of code we typed so, we need a variable and its an integer so it will be a whole number, a value has been assigned so put 25 into the variable called 'a' then were printing out something to the screen, we want to print out the value of our variable 'a' so we just typed the name of our variable into the brackets. NOTE theirs no parenthesis " "  so after checking you typed everything correctly press run in netbeans to run the program. You will see it prints out the number 25 and thats because  the value of 'a' is equal to 25. So what if you need something other than an integer? well for the rest of this tutorial im going to explain the different kinds of variables you can use for numbers in java. After this you will be ready for the next tutorial, mathematical operations in java. So im going to show you other various variable types. ooh tongue twister there lol. I'm also going to show you other ways we can assign variables in java.

 

So an identical way of declaring the int variable would be to remove the '= 25' and below on the next line type 'a = 25;' because we already declared a on the previous line. This is a slightly longer way but it gets my point across and shows you another method to assign a value later to a variable. So other variable number types. Each one takes up a different amount of space when they are created. This may not be entirely relevant to learning java but is still good to know. I shall go up in order of size and give a little comment afterwards.

 

note: all variables in java can be signed. this means they can be positive or negative.

 

So the smallest number variable type we can use is a byte. You probably already know what a byte is but its 8 bits.  It can store up to a value of 255.

byte b; // 8 bits

We then have a short variable. this is larger than a byte it can hold values of up to around 32,000 i believe. This short holds up to 16 bits of memory.

 

short s; // 16 bits

We then have our integer variables, this holds up 32 bits and can go up to some stupidly large number.

 

int i; // 32 bits

We then have the long variable which takes up 64 bits. This can hold some extremely large numbers, you wont need to use long very much at all. especially if your trying to conserve memory.

 

long l; // 64 bits

 

These are all the number variable types. Now i will go onto the decimal types.

 

The first one is a float, this variable is 32 bits.

float f; // 32bits

and then we have a double variable which takes up 64 bits.

 

double d; // 64 bits

the difference between the integer variables and the decimal variables in that the integer ones only store whole numbers where as float & double can hold numbers that end in a fraction such as pie 3.14 you can set an decimal variable as a whole number but i would just avoid confusion and use the integer variable types. Doubles can hold more numbers after the decimal place than a float, and the way we differentiate between the variables is to put a 'f' after the float value like '3.14f' or for a double we put 'd' so '3.14d' It's not needed however i only use this if java is giving me an error for it.
 I recommend you go over the types of integers above and have a go at printing some of the different values they can hold out using print line. It will help you remember the different kinds and when to use them.

 

Well i hope this tutorial made some sense to you and helped you on another step on our ladder learning java, in the next tutorial we will be learning about Strings and mathematical operators. In this tutorial we learn't about variable types, identifiers, the assignment operator and the values variables can be given along with the memory they use.

Edited by Rs Bots Pro
Link to comment
Share on other sites

SiletNight & Tracktix, thankyou for your comments they mean alot and inspire me to write more for you guys. I will release the next one tonight when i get home. There will be alot of tutorials to give you far more than enough knowledge to go ahead and make your own scripts dont worry! Alls i need is the support from our fantastic community!

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.


  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...