Okay. I assume you've read and understand the topic i posted about what you need to do before you even attempt any of my tutorials.
If you havent, Please read it
here so i dont have to worry about useless errors -_-
______________________________________________________________________
Here we will start
First, Understanding the layout of the program. I use DrJava(if you do not have it, re-read the above text -_-
When you have it open, You will see your main options on the top, The most you'll use is (obviously) New, Open, Save, Close, and the more complicated buttons are Compile, reset, and run.
On the left, You should see "(Untitled)", Thats where the files you have open are going to show up. You'll use this alot more as we go on.
To the right of that, you see where you're codes are going to be. This is the main element of the program.
Directly under that, you see the console, This is where you're programs outputs are going to show up. And, to the right of that, you'll see where you can choose what JDK to use(if you install more than one type of JDK).
______________________________________________________________________
Now that you understand the program, lets start coding~!
We'll make a simple hello world file.
Just to warn you all, Java is EXTREMELY Case sensitive,...
Soooo.
Start out by naming your class. Your filename will be the same as the class name for MOST of your files.
- Code:
-
class HelloWorld{
As you see, I've also put an opening bracket at the end to open the main function. You will only be using one function for now, Once we get more complicated, you'll be using more.
So, Now, we initiate the main function by typing
- Code:
-
public static void main(String[] args) {
Honestly, i have no clue why its so long, its just the way i was taught.
JUST TO CATCH YOU UP IF YOU'RE LOST, You should currently have this.
- Code:
-
class HelloWorld{
public static void main(String[] args){
Now, you've opened the main function and now its time to tell the computer what the function does.
Type:
- Code:
-
System.out.println("Hello World!");
println stands for print on a new line, as aposed to System.out.print("text");.
"print" just prints every character in the quotes.
SOooooooooooooooo
Now, you should have
- Code:
-
class HelloWorld{
public static void main(String[] args){
System.out.println("Hello World!");
From there, we're basically done, all we need to do now is close everything(Our brackets).
Since we have only 2 open, 'class helloworld{' and 'public static void main(String[] args){', So, To close those, all we have to do is put closing brackets '}'.
Put 1 "}" and press enter, Then put another and press enter. You' should have this so far:
- Code:
-
class HelloWorld{
public static void main(String[] args){
System.out.println("Hello World!");
}
}
NOW, Its time to see what you've just made in action!
Look for the "Compile" button, When you locate it, Press it.
It should tell you that you need to save the file, Save it wherever you want as long as you can remember where it is.
Now, Press RUN and look down at either the interactions tab, or the console tab.
.
You should see "Welcome to DrJava. Working directory is
> java HelloWorld
Hello World!" If you're looking at the interactions tab.
And just "Hello World!" if you're looking at the console tab.
_________________________________________________________________________________
Congratulations!
You've just made your first java program!
~Zachy