Flag This Hub

Java Programming guide : 1 -- The Beginning

By


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:

  1. Create a developer folder, to organize all of our files
  2. Install the necessary software for clean java development
  3. Launch the editor, and overview the components needed for development
  4. Create our first project, and code a few simple lines
  5. 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.

  1. Navigate to your Documents folder
  2. Create a new folder called "Development", then open it
  3. Create a new folder called "Java", then open it
  4. Create two folders called "Eclipse", and "JDK", then open Eclipse
  5. 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.

  1. Go to http://www.oracle.com/technetwork/java/javase/downloads/jdk-7u2-download-1377129.html
  2. 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
  3. Make sure the file is saved to your "/Documents/Development/Java/JDK" folder
  4. Once the download is complete, execute the installer, and make sure the install folder is set to JDK
  5. After that, go to http://www.eclipse.org/downloads/
  6. Choose eclipse for java ee developers, with your correct os. Save it to your "/Documents/Development/Java/Eclipse/Raw" folder
  7. Once downloaded, extract the eclipse zip file to your "/Documents/Development/Eclipse/Raw"
  8. Lastly. open eclipse, and choose your workspace to be set to "/Documents/Development/Java/Eclipse/Workspace"
  9. 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:

  1. Look for a main method in the class brackets
  2. Execute the code in the main method brackets
  3. Print out the string hello

Summary

So what we did in this segmant was

  1. Identify what java is
  2. Install all of the components
  3. Create our first project
  4. Create our first class
  5. Code our first main method
  6. Code one line to print in the console
  7. 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

Feedback Pool

Did i explain enough, or should i explain more?

  • No you did fine. I understood it all
  • I understood it, but not everything
  • No you need to explain more
See results without voting

Comments

cyberpwn 4 months ago

cool! ill check it out when i get a chance!

Island Tropical 4 months ago

Really interesting, me have just started a brand new java tutorial hub as well, come and read it :O

cyberpwn 4 months ago

yeah just keep checkin back im working on the website as i type!

ib radmasters 4 months ago

Thanks

I got to Hello.

I didn't find anything in the

http://cyberpwn.com/articles/java/

link.

ib radmasters 4 months ago

CyberPWN

Thanks

Then I will continue with the Eclipse download and installation.

cyberpwn 4 months ago

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

ib radmasters 4 months ago

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

Submit a Comment
Members and Guests

Sign in or sign up and post using a hubpages account.



    Like this Hub?
    Please wait working