NKN
06-13-2013, 03:43 AM
Welcome everyone to my newest tutorial, Java Basics. In this tutorial, we'll be starting off nice and simple. So without further ado let's get started!
Table of contents
Java
IDE
Hello World
Fundamentals
Closing Note
Java
Java is a well-known programming language. It's object-oriented. To get started in developing Java, you'll need to get the JDK, not the JRE.(Java Development Kit.)
A download can be found here (http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html)
IDE
There are many different IDE's you can use, a few being IntelliJ, Eclipse, and Netbeans. I prefer to use IntelliJ. Basically, an IDE is a program that you write the code in, that can also compile it. (In this case, from our .java files to our .class files we require.) Use of an IDE is recommended, but not necessary. If you choose to not use an IDE, you may use notepad. Save your code as File.java, and compile with the Javac command. Throughout this tutorial I will assume you are using an IDE.
Hello World
Any and every programming tutorial starts with this, did you think mine would be different?
Here we have a basic Java Hello World program(I expect you to write out all of the examples from this tutorial, you learn much, much faster.):
public class HelloWorld {
public static void main(String[] args){
System.out.println("Hello World!");
}
}
Now, let me break it down.
Fundamentals
public class HelloWorld
Here, you are making a new class, called HelloWorld. Your .java file must be the same name as this class. This is basically a container for your code.
public static void main(String[] args){
Alright, the public tag, means classes outside of HelloWorld can access this. It means it's not locked to that one class.
The static tag means that this method belongs to the class, and not the object.(Here comes OOP)
Static methods can only use static data, and can only call static methods.
Static methods can be accessed directly through the class, and does not need an object. I.E, ClassName.StaticMethod
Static methods are always in memory from runtime. Static tags cause more memory usage, so use it sparingly.
The void tag is the return type. If you're familiar with Pascal scripting, it's much like a function. Void means it doesn't return anything. If you replaced that with String, you would have to return a String.
Let's start with an example.
public void myMethod(){
}
This doesn't return anything. So if I was to do this:
System.out.println(myMethod());
I would get an error.
On the other hand, if I was to:
public String myMethod(){
}
This would be an error, as I don't have a return method, I need to make it like:
public String myMethod(){
return "Hello World";
}
Now, if I use:
System.out.println(myMethod);
This will say Hello World
See what I mean?
main - This is the name of the method. A method is a small procedure like thing that holds code, that you can execute by calling said method.
So what's special with main is, it is always the first method to be called when the game is loaded. It's automatically called by your system(If the correct parameters are in place.)
(String[] args)
These are parameters. I can explain best with an example.
public myMethod(int i){
}
Now, instead of being able to call the method like:
myMethod();
I have to call it like this:
myMethod(1);//Any number from -2.147 billion to 2.147 billion can go in the parenthesis.
Sub-direction! The // is a comment, anything behind a comment will be ignored by the compiler. This is for the programmer to read. You can use // for single line comments, or you can do
/*Write your comment
over multiple lines*/
With the /* tag starting the comment, and the */ ending it.
the parameter is basically a variable that you input into the method for use inside it. So for our main method, this is supplied when you run the program.
Say you run it on the command prompt like so:
java HelloWorld
This will run out program
But if we do this:
java HelloWorld hello
The arg parameter, from the main method, is now filled with the word hello. Anything after the HelloWorld is in the method.
{}(Brackets)
These things basically encase code. If you have one, you have to have an equal amount of the other. It tells the code where to group up, be it in classes or just methods.
The { means begin, and the } means end. These brackets specify the scope of the code.
System.out.println
This is a method inside the System class. It tells what ever the parameter is, and print it to the debug box.
Closing Note
I know I wasn't exactly clear on everything, but that should be enough to get you started and sort of understand the basics of the Hello World program, and some more core functions of Java. I'll cover more in the second part to this tutorial.
If you want anything clarified, just leave me a PM or a post, same goes if anything is incorrect.
Thanks guys!
~NKN
Table of contents
Java
IDE
Hello World
Fundamentals
Closing Note
Java
Java is a well-known programming language. It's object-oriented. To get started in developing Java, you'll need to get the JDK, not the JRE.(Java Development Kit.)
A download can be found here (http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html)
IDE
There are many different IDE's you can use, a few being IntelliJ, Eclipse, and Netbeans. I prefer to use IntelliJ. Basically, an IDE is a program that you write the code in, that can also compile it. (In this case, from our .java files to our .class files we require.) Use of an IDE is recommended, but not necessary. If you choose to not use an IDE, you may use notepad. Save your code as File.java, and compile with the Javac command. Throughout this tutorial I will assume you are using an IDE.
Hello World
Any and every programming tutorial starts with this, did you think mine would be different?
Here we have a basic Java Hello World program(I expect you to write out all of the examples from this tutorial, you learn much, much faster.):
public class HelloWorld {
public static void main(String[] args){
System.out.println("Hello World!");
}
}
Now, let me break it down.
Fundamentals
public class HelloWorld
Here, you are making a new class, called HelloWorld. Your .java file must be the same name as this class. This is basically a container for your code.
public static void main(String[] args){
Alright, the public tag, means classes outside of HelloWorld can access this. It means it's not locked to that one class.
The static tag means that this method belongs to the class, and not the object.(Here comes OOP)
Static methods can only use static data, and can only call static methods.
Static methods can be accessed directly through the class, and does not need an object. I.E, ClassName.StaticMethod
Static methods are always in memory from runtime. Static tags cause more memory usage, so use it sparingly.
The void tag is the return type. If you're familiar with Pascal scripting, it's much like a function. Void means it doesn't return anything. If you replaced that with String, you would have to return a String.
Let's start with an example.
public void myMethod(){
}
This doesn't return anything. So if I was to do this:
System.out.println(myMethod());
I would get an error.
On the other hand, if I was to:
public String myMethod(){
}
This would be an error, as I don't have a return method, I need to make it like:
public String myMethod(){
return "Hello World";
}
Now, if I use:
System.out.println(myMethod);
This will say Hello World
See what I mean?
main - This is the name of the method. A method is a small procedure like thing that holds code, that you can execute by calling said method.
So what's special with main is, it is always the first method to be called when the game is loaded. It's automatically called by your system(If the correct parameters are in place.)
(String[] args)
These are parameters. I can explain best with an example.
public myMethod(int i){
}
Now, instead of being able to call the method like:
myMethod();
I have to call it like this:
myMethod(1);//Any number from -2.147 billion to 2.147 billion can go in the parenthesis.
Sub-direction! The // is a comment, anything behind a comment will be ignored by the compiler. This is for the programmer to read. You can use // for single line comments, or you can do
/*Write your comment
over multiple lines*/
With the /* tag starting the comment, and the */ ending it.
the parameter is basically a variable that you input into the method for use inside it. So for our main method, this is supplied when you run the program.
Say you run it on the command prompt like so:
java HelloWorld
This will run out program
But if we do this:
java HelloWorld hello
The arg parameter, from the main method, is now filled with the word hello. Anything after the HelloWorld is in the method.
{}(Brackets)
These things basically encase code. If you have one, you have to have an equal amount of the other. It tells the code where to group up, be it in classes or just methods.
The { means begin, and the } means end. These brackets specify the scope of the code.
System.out.println
This is a method inside the System class. It tells what ever the parameter is, and print it to the debug box.
Closing Note
I know I wasn't exactly clear on everything, but that should be enough to get you started and sort of understand the basics of the Hello World program, and some more core functions of Java. I'll cover more in the second part to this tutorial.
If you want anything clarified, just leave me a PM or a post, same goes if anything is incorrect.
Thanks guys!
~NKN