Java Programming guide : 1 -- The Beginning
By cyberpwn
Welcome
Hello, and welcome to your first java programming tutorial! This tutorial expects that you are an absolute beginner, and have no idea what you are doing. This tutorial series will go over the basics of java, then will slowly incline on the scale of complexity. All of the tutorials can also be viewed at cyberpwn.com/articles/java. You may also view them here, by clicking the next, or back buttons at the bottom of this tutorial.
In this segment we will:
- Create a developer folder, to organize all of our files
- Install the necessary software for clean java development
- Launch the editor, and overview the components needed for development
- Create our first project, and code a few simple lines
- Run it, and show, how it works
Creating a development workapace
Creating a development workspace sounds hard, but actually, its very simple. All you need to do, is create a folder in your documents folder. Then just create the folders in the ordered steps below. Remember that this step isn't required, but its purpose is to organize your development environment.
- Navigate to your Documents folder
- Create a new folder called "Development", then open it
- Create a new folder called "Java", then open it
- Create two folders called "Eclipse", and "JDK", then open Eclipse
- Create two folders, called "Raw", and "Workspace"
Example Folder Tree
Documents
-- > Development
- - - - -- > Java
- - - - - - - - -- > Eclipse
- - - - - - - - - - - - -- > Raw
- - - - - - - - - - - - -- > Workspace
- - - - - - - - -- > JDK
Downloading and installing
All of the files below that are required for download, are compatible with windows, mac, linux, and solaris. Please follow the instructions step by step below.
- Go to http://www.oracle.com/technetwork/java/javase/downloads/jdk-7u2-download-1377129.html
- Agree to the terms, and select the package, that meets your operating systems needs. If you dont know what you need, just download the X86
- Make sure the file is saved to your "/Documents/Development/Java/JDK" folder
- Once the download is complete, execute the installer, and make sure the install folder is set to JDK
- After that, go to http://www.eclipse.org/downloads/
- Choose eclipse for java ee developers, with your correct os. Save it to your "/Documents/Development/Java/Eclipse/Raw" folder
- Once downloaded, extract the eclipse zip file to your "/Documents/Development/Eclipse/Raw"
- Lastly. open eclipse, and choose your workspace to be set to "/Documents/Development/Java/Eclipse/Workspace"
- Close eclipse, once you see the welcome screen
Your first project
To create your first java project. oepn eclipse (in the raw folder). Click the x button to exit the welcome screen, and you should see a bunch of different components. Simply click file > new > java project. If it isnt there, click other, than click on the folder java > java project.
In the new java project wizard, type in a name, then click finish to create the new project. If you look on the left hand side you will see that your project is listed in the package explorer. Click the arrow on your project folder to drop down all of the contents of your project folder. You can see that you have two items.
- an "src" folder to hold all of your java code
- and the java liabrary
The src folder holds all of your java code. The java liabrary is all of your code references to.
To begin, right click the src folder, then click new > class. A java class is a group of code that is related to the class. Applications are made up of classes. When you fill out the name, make sure that the name starts with an uppercase letter. Every word after that starts with an upper case. Look below for some examples
Correct
- MyClass
- Java
- ThisIsMyFirstClass
Incorrect
- myClass
- java
- myJavaClass
Once you have named your java class, click Finish, and you will see that it automaticly opens with the following code allready written.
What you should see
public class YourClassName {
}What the code means
The "public" tag is a declaration to the whole project that its public. Public means that all of the other classes can access its code. The "class" tag is a declaration that the next tag will be the name of the class. The tag "YourClassName" should be replaced by what you named uyour class when you created it.
WARNING: Your class name should match the file name its in.
The main method
When you run your program, java needs to know where to start. See if you have for example 13 classes, java needs to look for the main method. A method is a block of code in a spacific class. For example if you had a class representing your dog. The class would have multiple blocks of code in it.
For example our dog class would have methods like bark, sit, play, and shake. When another method or class calls that class it will call a spasific method, or function. So the main method is the first method that is "called" automaticly at the start of your program.
Coding the main method
Inside of the cirly brackets of your class type the following code
public static void main(String[] args) {
}So overall you should see:
public class YourClassName {
public static void main(String[] args) {
}
}Explaining
Ok so we declaired our main method, and put it inside of the brackets. Brackets allways open AND then close. If they dont close, you will have an error. There are now a new set of brackets, on the main method. Inside these brackets, code will be executed at the start of your program.
The last line
See if you run the program right now, nothing would happen, because nothing is in the main method. So we are going to print a line on the console.
System.out.println("hello");So overall your program should look exactly like this:
public class YourClassName {
public sataic void main(String[] args) {
System.out.println("hello");
}
}Finnally
Now click the green play button at the top of your toolbar, and look below, and you will see "hello" in the console (No quotation marks in console)
So what the code did was:
- Look for a main method in the class brackets
- Execute the code in the main method brackets
- Print out the string hello
Summary
So what we did in this segmant was
- Identify what java is
- Install all of the components
- Create our first project
- Create our first class
- Code our first main method
- Code one line to print in the console
- Run the code
Up next
- Explain how the syntax
- Explain variables
- Create different types of variables
- Plug the variables in to the output
- Modify a variable
You can view the next tutorial, by going to http://cyberpwn.com/articles/java/2, or by going to http://cyberpwn.hubpages.com/...
The set of tutorials can be found at http://cyberpwn.com/articles/java You may also donate at http://cyberpan.com/donate
Thanks
Thankyou for reading this tutorial, i hope you liked it, and i hope you up vode it, or even make a comment!
If you have any questions please add them to the comments, or email me! by going to http://cyberpwn.com/contact
Links
- Cyberpwn :: Home
My website: tons of articles! - Eclipse - The Eclipse Foundation open source community website.
This is the eclipse website
Feedback Pool
Did i explain enough, or should i explain more?
See results without votingComments
Really interesting, me have just started a brand new java tutorial hub as well, come and read it :O
yeah just keep checkin back im working on the website as i type!
CyberPWN
Thanks
Then I will continue with the Eclipse download and installation.
nope you are doing fine
Registering isnt required
the jdk can be installed anywhere
eclipse can be run anywhere
I just do that for organization... it doesnt matter... you dont have to start over
CyberPWM
I put the install in the JDK but I let the installer use its default location.
I have four Java programs now.
Java(TM)7 Update 2 -- 99.11MB
Java (TM) SE Development Kit 7 Update 2 -- 137MB
JavaFX 2.0.2 -- 18.32MB
JavaFX 2.0.2 SDK -- 63.66MB
I assume that I should uninstall the first two of them.
And reinstall to the JDK directory.
Is that True?
Also as part of the install process, there was a registration page, that sets up an Oracle account. I assume that this is optional as you didn't mention it.
I have stopped at this point, I didn't get to the Eclipse download.
I have the directory structure setup, and the installer in the JDK. Should I uninstall the default location installation, and start over?
Thanks
Thanks
cyberpwn 4 months ago
cool! ill check it out when i get a chance!