Sie sind auf Seite 1von 21

How to Install Java

Introduction
We're going to learn how to easily download Java for free, and then install Java. Have you ever given up on trying to program because you couldn't figure out how to set it up? How do you know what version of Java to download off of Oracle's Java website? What is an SDK, what is J2EE, what is an API? You can learn all of these terms, but if you want to start with Java right away and get it running, then all of the jargon gets in the way. Im going to say this upfront: This is for absolute beginners. If you dont know how to program at all, thats perfect! Those who do know how and want to learn Javaquickly can also benefit greatly from this tutorial. Im going to show you how to program with Java as effectively as possible and as quickly as possible. Were going to bypass all of the road blocks by learning to install Java and get it working, today.

Installation/Setup
Java can be written with any text editor, as Java is a compiled language. This is a detail were going to skip, because understanding this will not help us to get Java working, nor even tell us how to program in it. We want to install Java first, and that's exactly what we're going to do. What I recommend is downloading Eclipse. Eclipse is much more than a simple text editor, and the version I recommend that you get comes with Java's Development Kit already so theres little setup. Just follow these easy steps: Note: If you get stuck at any time, use the form below to contact me and I'll be glad to assist you as soon as possible! I'm here to help you and I firmly believe that anyone can do this! 1. Go download the latest Java runtime environment This will install Java on your machine. Get the version you need. Most of you probably have Windows, so you'll want to choose one of the Windows downloads. If you happen to have Solaris or Linux, you'd have to get one of those. For those of you on a Mac, guess what? You already have Java! So you can skip this step.

I chose the offline installation, because I think that is easier. Make sure to select run when the download box pops up. Then follow the instructions for downloading the JRE (Java Runtime Environment). 2. Download Eclipse Classic 3.4 (or whatever version exists as of today) Follow the link to go to the Eclipse downloads page. Make sure you select Eclipse classic.

The classic is the 4th choice in the screenshot, although this may have changed. When you get to the next page, you will be asked to choose a mirror. This is so you can choose a download location that is closest to where you live. Just pick one, and this time hit save (not run!) to place the file somewhere on your computer. 3. Unzip the zip file to wherever youd like. You can actually just right click the file and select Extract Here. That will put the Eclipse folder into the current folder you're in. If you don't see an option to Extract Here, it's possible you don't have unzipping software. Download WinRAR off of the Internet (it's free) or go get your favorite unzipping software and install it. 4. Thats it! You didn't even have to run an installer if you already had unzipping software! That was easy, wasnt it? Now were going to open Eclipse, so go into the folder you unzipped, and start-up Eclipse (It looks like a purple circle). If you get a message about your Java Runtime Environment, you need to complete step 1 again. Once thats done, youll notice that a box pops up asking you to set the location of your workspace.

Now, this may seem confusing, but its actually simple. A workspace is a folder that contains your Java projects. Hit browse, create a folder to use as the place to put your Java projects, and hit okay. Eclipse will continue to load, and when its done, you should see a bluish screen with some circles everywhere.

Locate the circle that has an arrow in it. It should say go to workbench when you highlight the circle. Click it, and youll be taken to a screen that looks like this:

There are lots of features that Eclipse comes with to help aid with developing Java programs, but were not going to go into all of that today. That's how to install Java! You now have all the tools you need to start creating Java programs. In the next tutorial I'll walk you through creating your first Java program using Eclipse.

Java Hello World - Your First Program


Introduction
We're going to start off by coding the traditional Java Hello World program! Java programs are very simple to write once Eclipse is started up and you know how to work it. Turns out using Eclipse is simple, and we're going to walk through writing our first Java program this way. Note: We're using Eclipse for these Java tutorials. If you've never heard of Eclipse, then you need to look back at the first tutorial. It's so easy to set up that it's worth it to use it as our Java editor with these tutorials.

Create A New Project


Because we're using Eclipse, we need to create a Java project before we can begin coding. A Java project is just a bunch of Java files and other files needed for your program to work. Every time you want to work on something new, I recommend creating a new project for it, as it's a good habit to have. So, if you haven't already, go start up Eclipse, and hit ok when the Workspace Launcher box appears. Right click on the left side panel, go to new, and select Java Project. (If you don't see that immediately, just hit Project... and you'll see more project types. Java Project should be at the very top, but if not, go to the folder that says Java, and you should see Java Project in there.)

Great! Now we have a bunch of options to set for our new Java Hello World project. All of these options can quickly become overwhelming, so once again we won't go into any details. Name your new project Hello World, leave everything else how it was, and then hit finish.

The form will close and you should see your new project for your Java Hello World program on the side panel.

Create a New Class


If you expand your new project by clicking the plus sign next to the folder, you'll see a folder called src and also a JRE System Library. Again, don't worry about these things. Right click on the src folder, go to new, and select Class. All Java files are the same as class files, so that's usually the only option you'll ever select.

Now you have yet another form to fill. That's ok; just give this new file a name. We're going to call it HelloWorld, so type in HelloWorld (no spaces!) where it asks for the name. Java files should always start with a capital letter as the first letter and should have no spaces, so remember that. Also, check the box that says public static void main(String[] args), because you'll need that. Then hit finish.

Writing the Java Hello World Program


Now you'll see your newly created file, and you'll see that some of the work has been done for you. Let's take a quick look at what it is you're seeing.

public class HelloWorld is the beginning of your class file. Notice it has the same name as the file itself. The name, including the capitalized letters, must match the file name in Java or it won't work. Eclipse does this work for you of course. Helloworld is not the same as HelloWorld, because they are not exactly the same. The second HelloWorld has a capitalized W, the first one does not. public static void main(String[] args) is called the main method. I'm not going to discuss what methods are right now, but without this statement, you cannot run your program. So remember that! You'll notice two open brackets like this one in front of two of the lines of code: { and then further down you see two closing brackets that looks like: } Anything you write inside the inner two brackets belong to the main method, and anything inside of the outer brackets belongs to the class. All methods and classes have opening and closing brackets, but we'll touch on that some other time. You'll see "extra code" in different colors. Those are comments. Comments help you to remember what the code does and helps others who might read your code to understand what the code does. Nothing is worse than writing code and then coming to it later and forgetting what it all does!

See the above? This is a multiple line comment and can be ignored. You may delete this if you wish.

The line above is a single line comment, and it can also be ignored. You may also delete this line if you wish. Now we're going to make the screen say hello to the world. To do this, make sure you're typing inside of the main method, meaning inside the open and close brackets belonging to the main method. Just write the new line under public static void main, because that would be considered to be inside the main method brackets. Add this line:

Your full Java Hello World code should look like this:

If something is wrong with what you typed, Eclipse will automatically underline the problems in red. If you get this, make sure everything is spelt correctly and that you are using correct capitalization. When there are no longer any red underlines, right click the text screen (anywhere in the box where you wrote your code), go to Run As, and select JavaApplication. If it tells you that you need to save the file, save it, and then watch it run. In the little console at the bottom of the screen, you should see it display Hello world! That means that the program works, and you've now written your first program in Java! Congratulations!

I put a red border around the output screen. That is where Java displays its output in Eclipse. Congratulations, you have just finished your first Java program! Now that everything is set up and works, you have enough tools at your disposal to create highly sophisticated programs. Yes our Java Hello World program was simple, but it is important for you to see what Java code looks like. The following tutorials will teach you all of the cool features Java has to offer, and of course if you have any questions please check out the forums and/or shoot me an email and I'll be glad to help! The next lesson will cover what the line you inserted does as well as other cool commands. Remember if you have any questions, comments, or concerns, feel free to contact us. Until next time...

Java Output
Introduction
Java output is where your journey takes you next if you've been following the tutorial. Computer programs are wonderful at making calculations, but what's the point if you can't see them? Luckily, Java output is easy to figure out. You're going to learn the different ways of displaying results on the screen. In the previous tutorial you created a simple program that displayed hello world on the screen. This is great, however we'd probably like Java to be able to do more for us than say hello. Let's start by discussing exactly what we coded.

System.out.println

It only took this one line to make it display hello world, but let's look at the specifics. System.out.println() is really a piece of code that someone else already wrote. There is other code that already exists within Java that has been written for this line of code to work. You don't have to figure out how it displays whatever is inside the parenthesis, that's just how it works. By the way:

This puts a blank line on the screen, just as if you had hit enter to skip to a new line. It prints out whatever is inside the parenthesis, and if nothing is inside, it prints out nothing. We put "Hello world!" in quotes inside of the parenthesis. This is because all words, phrases, or sentences are what we call Strings in Java. All Strings need quotes. "Hello" <<- GOOD Hello <<- BAD "Hello <<- BAD Hello" <<- BAD

Java Output Tricks


There are some tricks you can do when outputting strings. For example you can add strings together, like this:

That is the same as:

The only difference here is that the string is split into two strings, and then added together (concatenated) with a plus sign. That's all there is to it, and you can break up a string into as many pieces as you want. Try experimenting with that for a bit, as well. What if you don't want to put a new line when you're done printing out what you want? Java has a way of handling that, too.

This does the same thing but does not go to a new line when you're done. Notice how I did not leave the parenthesis empty. I had to put quotes in there because using just print instead of println requires that you put SOMETHING inside of the parenthesis. And here's another good point, two quotes right next to each other with nothing in them, not even spaces, is the same as nothing. You might as well have not written any code, because the code effectively does nothing. Try these two lines of code:

Instead of Java placing these words on two different lines, Hello and world! Are both placed on the same line, so it reads exactly the same as what you have done before. Why is this useful you ask? You could just write "Hello world!" the way you've done it before, so why split it up? Here's an example. Sometimes, you may not know what you want to output. Let's say you know you want to output the world hello, but you also want to output something else on the same line. Let's pretend you do not know what that something else is yet, but you know it's based on certain conditions. Something like this:

So that part is done, but maybe you want to output the name of the user and place it right next to the hello. You may not know the user's name yet, but you want to at least output hello. If we used println, the second part of this string would be on a separate line, and that wouldn't be what we wanted. (We haven't talked about how to get input from a user, but we will get to that in a later tutorial). That's all there is to making Java output easily onto the screen! You can modify your hello world program or create a new one and experiment with displaying different strings on the screen. Get comfortable adding semicolons at the end of each line of code, because it's extremely important not to forget them. Remember, no semicolons after lines of code that have open brackets!

Java Input - Using Java Scanner


Introduction
Note: If you're looking for Java Scanner help, click here. The page contains some common questions about them and a question form where you can ask your own questions about Scanners in Java. Otherwise, for a Scanner example scroll down near the bottom of the page. Otherwise, simply follow along with the tutorial. The screen shots throughout are of the Eclipse IDE. If you don't have Eclipse, I highly recommend downloading it! Want to get more tips and more personalized help on Java Scanner and other Java topics? Need help with common problems found on quizzes, tests, or other exams? Click here to sign up for the Fresh Cup Of Java Newsletter, a monthly e-zine full of sample programs, general tips, and other Java related materials, free of charge! A Java Scanner is the fastest, easiest way to get input from a user in Java. By this point you should be able display some sort of output onto the screen. You should also be able to store data inside of variables. Both of these concepts are great, but what's the point of a program that has no interaction with a user? For example, what good is a computer game if you can't control any of it? What we need is input, and Java has plenty of ways of accepting input. However, we'll be using a Java Scanner for this tutorial. We're also going to deal with the simplest kind, text input. This means that we'll be inputting from the keyboard. Note: Looking for Java Scanner help? Check out these frequently asked Scanner questions!

Getting Input
So, first thing's first, we're going to create a new class (a new Java file). If you already have a Java project in Eclipse, then all you have to do is right click on the src folder and hit new class. If you do not have a Java project, you will need to create one. If you don't know what I'm talking about, you really need to read this tutorial. It will teach you everything you need to know to get started with writing a program. Call your new class Inputs. Remember, the class must start with a capital letter. Next, make sure to checkmark the box that adds public static void main(String[] args). If you do not, that's ok, but you'll have to add it in manually. When you've entered the name for your new class, hit finish and you should see this:

There may also be some comments in there that are green and blue (mine did not have those), and you can delete those if you want or leave them alone. Now we're going to be doing all of our work inside of main. To make sure you're up and running, add a print line inside of the main as shown below (remember, this means in between the opening and closing curly brackets that belong to the main):

You should know how to do this basic output by now. Make sure that your program runs. You should have seen Inputs on the bottom panel of the screen. Okay, so now onto inputting. First of all you will need to use a Java Scanner that will get this input for you. It acts like a variable but it's not one of the basic types that were talked about in the previous tutorial. Add this line into the main:

It is EXTREMELY important that you get the capitalization correct, or this will not work. You will also notice that Eclipse has underlined Scanner in red, as shown in the next screenshot. That means that Eclipse sees this as an error. That is okay, because the code is actually missing something important.

To fix this, right click on the main screen, go down to where it says Source, then select Organize Imports. This will import everything that your program is missing. If a box pops up asking you to choose a type to import, choose java.util.Scanner. This is the correct import for the Java Scanner, as opposed to some other Scanner that might exist. For me it was the first option.

You'll notice that the following line was inserted after you organized your imports:

The Java Scanner class is like any class you create, except it was created for you. Since Java already comes with it, it had to be imported. If you don't want to use the shortcut for importing classes, you can always just type in the import statement manually at the top of the page before the public class line. More on this for a future article though. What we need to know for this tutorial is that we have a variable called scan, and it is a Scanner. Just like int num = 3; means that num is an int, Scanner scan = new Scanner(System.in); means that scan is a Scanner. It's pretty simple. Don't worry too much about what it equals, it just means that it will be getting our input. Notice how it looks awfully similar to System.out but with an "in" instead. Just because we have this variable "scan" that will take input does not mean that's all we need to be able to get some input. We have to make it accept some input. To do this, put this line of code right under the line where you create the variable scan:

This will receive the input of the next line of text someone types into the keyboard. It's pretty simple. Now, how can we use the Java Scanner to receive the line of text from a user? Well, we'll need to use a variable. If you're not up to speed on these, you'll need to go take a look at the previous tutorial. Okay, so now we're going to create a String variable, and we're going to make it be equal to the input we get. Change the line you just wrote to:

Keep in mind that the names of my variables are my preference. You could easily just do:

It's up to you to choose what to name your variables, but in general you should try to make them as descriptive as possible while keeping them short. Okay, so now the input someone types in will be stored in your String variable from the Java Scanner. You can use that variable to now output back the line of text. The program will just echo whatever is typed in. You should be able to output the string on your own, because you should have learned how to already. Go ahead and write the code to do this. If you did it correctly, you wrote this under the previous line of code:

That will print the input you received using scan.nextLine();. Go ahead and try running the program. When it runs, you'll have to type in the input yourself. To do this, go to the bottom panel where you normally receive your output, and type on the first blank line you see (if stuff is printed there, you need to go down to the first blank line and begin typing). When you hit enter, Java should print out exactly what you typed. Neat stuff. Note: nextLine() will read lines one at a time, including white spaces! If you want to skip all white space, use next() instead. Next() will read up to the first white space and then stop. If you do next() again, it will skip over the white space and continue reading the next set of characters and stop at the next white space, etc.

scan.nextLine(); will input all the text that was typed up to the point where the user hits the enter key. It's a quick and easy way to get input and to store it into a String.

Other Inputs
There are other ways of getting input. We can use scan.nextInt(); to get an integer from the user and store it in an int variable. Like this:

Now num has the integer that the user typed. If the user types in something other than an integer, the program will crash, so you must be careful. There are ways of dealing with these kinds of errors but as with a lot of details, it's beyond the scope of this tutorial. To print this num variable you do as you normally would for any int variable.

Using Both Input And Output


Now that we can do both input and output, let's make a little addition program that makes full use of the Java Scanner class. The program will ask the user to type in a number, ask the user to type in a second number, and then display the addition of the two numbers. You can create a new Java class or you can just delete everything inside of the main method you're working with now(everything in between main's two curly brackets). If you're making a new file, name the class whatever you want, as long as the first letter is capitalized. To begin, we must ask the user to type in the first number to add. This means you will output a question for the user onto the screen. You know how to do this, so do it. Next, we will have the user input the number. Remember how to do this? First, we need to create the Scanner variable:

Remember to right click on the main page, select source, and then select organize imports. Next you need to create an int variable to store the first input.

Now repeat the process to ask for the second number. Remember to create a new int variable and to call it differently than the other integer variable you created. Last, do the addition, and output the result. Do you remember how? I'll skip some lines before spoiling the answer.

If you do not remember how to do all of this, I suggest rereading the previous tutorials as it is VITAL to get these basic concepts. Notice how I did not create two Scanner variables; you only need one to do all inputs. Also notice the last line of code above. Remember our trick of adding strings together? Well, that's an example of adding a string and a number together. The variable has no quotes because it is a variable. But wait? Isn't num3 an int? Yes, and remember we do not need quotes to print out ints, so this would work anyway. Easy and useful isn't it? The Java Scanner can do nextDouble() instead of just nextInt(); if you want decimal numbers. It also has a lot of other options that I won't discuss here, but keep that in mind. Oh, one last thing, don't try to scan text with nextLine(); AFTER using nextInt() with the same scanner! It doesn't work well with Java Scanner, and many Java developers opt to just use another Scanner for integers. You can call these scanners scan1 and scan2 if you want. So there you have it, that's how you get input using a Java Scanner. I strongly suggest playing around with what you learned and try to make your own little program that accepts user input. In the next tutorial we'll learn how to make decisions with the user's input.

If you have any questions, comments, or concerns, feel free to contact us.

Read more: http://www.java-made-easy.com/java-scanner.html#ixzz1vgmCVT2k Read more: http://www.java-made-easy.com/java-output.html#ixzz1vgXso8Dl

Das könnte Ihnen auch gefallen