PDA

View Full Version : Java Basics: Part I



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

Brandon
06-13-2013, 04:25 AM
Hmm.. I'm a bit picky with almost every language tutorial and here's why for this specific one:

1. static isn't just for belonging to the class but rather its to keep a function or an allocated region in memory for the lifetime/duration of the program.

Now programmers use it for accessing things outside of their regular scope.. Aka Object scope in this case. But that's not what it necessarily is for. Its there to keep that function in memory for the duration of the entire program and because of that, you can access it from anywhere. Basically a side effect or just an added on extra feature to it.. However there are downsides:

Static functions cause your program to eat up more memory than non-static ones which are cleaned from the stack and the result returned to the caller.

It's probably too much to mention for a Hello World tutorial but you are teaching static keyword definition so it'd be nice to add what it really does/is for.


2. It would be nice if you put that { means begin and } means end and that they specify scope. But I guess that's not important for hello world so you can ignore this (it'd be nice though).


The reason I point these out is because its good to know from the start and rarely any tutorials mention this. This way, you don't get messed up later on. For example, a Ton of java programmers still think things are passed by reference; such as arrays for example.. But they aren't; they're pass by reference value.. but meh ;)

Nice tutorial though. Just needs a bit of organizing/cleaning up. Try using the Highlight tags instead of Code tags. Do: and [/ Highlight] without the spaces.

[Highlight=Java]
public static void NKNTutorial() {
}

Kasi
06-13-2013, 11:08 PM
Now, if I use:
System.out.println(myMethod);
This will say Hello World


would that really work tho? ;)
i'm just kidding, good beginner tut.