Java Lesson 01 – String basics
Creating a new Project
note: everything about programming is case sensitive make sure to use the right case.
In eclipse at the top right click File -> New -> Java Project. You will now have a New Java Project wizard. Fill in the Project name: Lesson 1 and press finish. Is that it? What about the other options? Don’t worry about the other options for now. It’s all fine. You now have created your first project.
Ok so now you might be wondering thats nice and all, but what is it for? A project is the shell of your programm. Its the container of your class files. In it self it does nothing. Its mostly made to make your improve your workflow and keep things organized. Lets continue into making this project a runnable program.
Go into your Lesson 1 project and right click the src(source) folder -> New -> Class. This will open up a New Java Class wizard. Fill in the name: Main and put a tick infront of public static void main(String[] args). When you’ve done that press Finish.
Inside your src folder should now be a (default package) and inside that a Main.java. For now you can ignore the (default package) we’ll go into packages later on you can ignore that for now. Open the Main.java and you should see:
public class Main { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub } }
Everything that starts with // or is inbetween /** and */ are comments, remove these for now. Now than lets starts simple and add to the main:
String s = "Hello World!"; System.out.println(s);
Ok let me explain this piece of code. String s = “Hello World!”; is how you define a variable. String is the variable type, s is the variable name and “Hello World!” is the variable value. And in java you have to end every line of code with a semicolon. Strings are used to store text in, values of a String variable have to be inbetween double quotes. The s is a variable name you can basically call it whatever you want as long as its not a known variable type, something like String String = “Hello World!” would give you errors also variable names have to start with a letter and may only contain letters, numbers or underscores, so no spaces or starting with numbers. There are more variable type, we’ll get into them later.
The System.out.println(); is javas way to print stuff to the console. In the above sample we will be printing the String s. Its not really something you need to understand, just know how to use it. Its mostly used to debug your code.
Now lets teach you how to run this. Before running your code you always need to save your code, use ctrl + s. You should save quite often, because after you save it recompiles internally and shows you if you have any errors. What happens if you run your code is that it compiles and runs your code. Eclipse does this all automatically for you.
There are multiple ways to run your code:
- Right click Main.java -> Run As -> Java Application
- Select the Main.java and hit the Green circle with an arrow inside in the eclipse toolbar
- Select the Main.java and hit ctrl + F11
- Select the Main.java and go to Run -> Run in the eclipse top menu bar.
After you’ve run it, your console log should dsiplay that is was <terminated> and there should be Hello World! in the log. Congratulations you have now created your first program.
Playing with strings
This bit is where you get to try out code yourself. What do you expect the results of the following pieces or code are? Be sure to try them out one by one.
System.out.println("Hello World!");
String s = "Hello "; String s2 = "World!"; String s3 = s + s2; System.out.println(s3);
String s = "Hello "; String s2 = "World!"; System.out.println(s + s2);
String s = "Hello"; String s2 = "World"; String s3 = s + " " + s2 + "!"; System.out.println(s3);
String s = "Hello"; String s2 = s + "World"; System.out.println(s2);
Besides System.out.println there is also System.out.print which prints everything on the same line.
String s = "Hello "; String s2 = "World!"; System.out.print(s); System.out.print(s2);
System.out.print("Hello "); System.out.print("World!");
Assignement
This assignement is optional. You can move on to the next Lesson if you want.
Assignment: Create me some ASCII Art. Before you ask me what ASCII Art is do some research yourself, it’s pretty easy to find out.
Warning: Try not to use a \ in your string it will give you errors. If you want a \ you will have to do a double \\. Ill explain why that happens some other time.
Download
If you want my project with the Hello World and my ASCII Art download it here.
Unzip it somewhere on your pc, not in your workspace. In eclipse: File -> Import -> General -> Existing project into workspace -> Browse to where you stored the Project -> Tick copy into your workspace -> Press finish.
You can now delete where you stored the project.