We're going to talk about
objects tomorrow. But today we're just going to deal with a special object: String.
A string is an object, but it's used soooo often for soooo many things that Java has special rules for using them. In fact, it makes them look a lot like primitives.
So today we're going to write a program that's pretty much the first program every programmer being taught programming after 1970 has done.
First, let's remember how we
declare a variable:
String myString;
I've declared a String object.
Now, as I said, for most objects this part would look different, but for Strings - initializing it will look pretty much the same as it did for primitives.
myString = "Hello World!";
So now I've
assigned "Hello World!" to myString.
You might wonder why it's called a string. A string object is actually a bunch of char (short for "character") primitives that have been 'strung' together. It's much easier to work with a String than a bunch of individual letters. Imagine if we had to say:
char char1 = 'H'
char char2 = 'e'
char char3 = 'l'
And, in fact, that IS similar to what we had to do back in the old days of programming! Be thankful that's no longer the case! :D
Alright, so now we have the phrase "Hello World!" stored in Java code. But how do I display that to the world? How do I access that data?
1. Open up your Eclipse program. Just press "Ok" when it asks you to select a workspace.
2. You're probably on some sort of welcome screen. Find and click the button (should look like a bent arrow) that says "Workbench". It's probably in the upper-right hand corner.
3. With any luck, you're on a screen that looks like:
4. Select the "File" menu, then hover over "New", then select "New Java Project"
5. Type in a name for the project folder, maybe "Rebels Stuff". Then click "finished" at the bottom. You can ignore the rest of the settings.
6. The project folder should have appeared on the left side of the screen (as in the screenshot above) now. Right click it and say "New" then "Class" (we'll talk more about classes later). It should be the fourth one down.
7. Name the class "HelloWorld" - you can ignore the rest of the settings and just click "Finished". Make sure you didn't put a space between the words in the name or it won't work.
Now things should look like this:
Alright, fantastic!
Now, you know that a semicolon ends a line of code.
Now you need to know that curly braces ( '{' and '}' )enclose
sections of code.
We'll talk more exactly about when you to use those later.
So, between the two existing curly braces, copy and paste this line in:
public static void main(String[] args){
That line is quoted on Urban Dictionary.com as "The most annoying line a beginner Java programmer has to type in each one of their programs." and they're right :)
Now, you'll notice that line ends with a curly brace. That means you need to close it.
Fortunately, you can just press "enter" now and Eclipse should add the closing curly brace for you - since Eclipse is cool like that.
Make sure things look like this now:
Now declare and initialize a String saying "Hello World!". You can look back up above if you need help doing that, or you can just scroll down and look at the next screenshot:
You'll notice that 'myString' (or whatever you chose to call it) is underlined in yellow. That's an Eclipse warning. It's Eclipse telling us (in this case), "You just declared and initialized a variable... but you aren't using it anywhere? That's a waste of space. What's up?"
So let's use it.
Here's another piece of code you can just copy and paste - you don't need to understand it perfectly yet:
System.out.println();
That's how we tell Java to print something to our console. You'll see the console in a moment.
Now, put that line of code in your program - BELOW where you initialized your string.
Between the parenthesis in that line of code, put the name of your string.
You'll notice the yellow warning line went away. Because now we're using that variable.
Try to finish that step on your own, but if you have trouble then you can click here for a solution:
[Solution]
Now, finally, go up to the "Run" menu (far to the right of "File") and press "Run" - the first option in the dropdown.
Or you can just hold ctrl and press F11 on your keyboard.
It's possible that this screen will come up, maybe it won't:
If so, just select "Java Application" and press "Ok".
If everything's gone right. This should show up at the bottom of your Eclipse window!
Hello Java!