How to Java: E1 (Hello World)

Jan 4, 2016 11:54 PM
Jan 5, 2016 10:50 PM
635875226543146975.jpg

This my first tutorial within the HowToJava series, hope this is the correct way to write it...(helpful advice is encouraged)

------------

In this tutorial, I'll be discussing how to install the Java Development Kit (JDK), and an IDE to run it.

Downloading the JDK

First go to this website JavaSE Downloads and click the option that the arrow indicates (in picture)

635875182735803240.jpg

Scroll down to the box that says Java SE Development Kit 8u66

Next click Agree then select the JDK for your OS (Linux, OSX, Windows, etc.) and download it

635875186270491204.jpg

Follow the steps for installing the JDK

--------------------------

Installing Eclipse IDE

The next step is to install an IDE for programming in Java.

For this tutorial, I'll be using Eclipse IDE

635875189264084373.jpg

Click the one thats in the big banner

Select the right one for your OS (for Windows/Linux, check to see if your OS is 32 or 64bit)

You are free to use an IDE of your choice/preference (Atom, Netbeans, Eclipse, etc.)

Let it install, then follow the steps to set it up.

Now that its installed, run it.

(If you already know how to create new projects in EclipseIDE, you can skip this step)

But for those who don't know, please read below:

Some Java-related information

  • Java projects have a .java as their file extension, and depending on your OS, they may have a big red J as their file icon.
  • Java is a cross-platform programming language ("write anywhere, run anywhere"). Meaning you can build a java program on windows, and have it run on Linux, and vice versa.

Back to the tutorial:

Creating a new Java Project

635875196500804283.jpg

Click that icon (circled in red) to create a new Java project

Find the folder named Java and select Java Project

635875196964710640.jpg

Next, you'll find a screen like this (see image below)

635875196236899826.jpg

Name it whatever you want to, but for the purpose of this tutorial, I'll name it NBExample (Null-byte example)

Make sure you have JavaSE 1.8 selected (if you didn't thats okay. This current tutorial won't require anything too fancy)

Java tends to be backwards compatible (most of the time)

Next, click Finish

-----

Create a Package

The next thing to do is to open the NBExample Java folder from the side of the EclipseIDE panel. And right-click on Src then New then package

635875202703928197.jpg

Next name the package whatever you want to (but for this, I'll name it nbexample), and click Finish

635875204246428889.jpg

right-click Package, and select New then click Class

635875206162053895.jpg

The Class file is where the code goes

635875207328147018.jpg

Click Finish

Now, you have a file window with some basic stuff

635875208895803089.jpg

You have keywords in highlighted purple coloring, and normal stuff in normal black text. (except that String is a variable type (its for sentences))

Just about every project has a main method (without one, the Java project cannot run)

Explanation of what the highlighted words mean:

package = basically says where the file is in which sub-folder. (I'm not sure if I make sense, or confused someone.) You can only have one package per file.

public = Says that this class (aka file) is public and can be accessed by other classes within the same Java project

class = states that code within the class' brackets are in the file.

Static = means that the variable or function is shared between all instances of that class as it belongs to the type, not the actual objects themselves

void = Means that the method does not return anything.

{ } = Java does not care about indentation of code (its purely for the viewing pleasure of the programmer/reader). However, to keep things together, Java requires that all classes, methods, loops, (and sometimes if/else statements) have them.

; = The semicolon tells Java "This is the end of this statement". Without it, Java will keep going to the next line(s) until it finds a Semicolon, then determine whether it is proper syntax or not.

On a side note, semicolons enable programmers to be trolls and one-line their Java code, making it near impossible for other people to read. (it doesn't slow the Java Virtual Machine's compile time, since the JVM reads code in a line anyways)

// = Put these before stuff to comment them (one line)

/** */ or /* */ = multiple line comments (adding two ** will tell Java that the comment is a Javadoc, and will appear as a textbox if you hover your mouse over it, it'll display what you wrote in that Javadoc comment)

Other things, I'll explain in later tutorials.

Lets start by getting the project to display Hello World

To do this,

write System.out.println("Hello World") within the main method of the java class. (the one that says public static main(String args){ })

635875218489866265.jpg

Another way to write it (if you're like me, and don't like having to write System.out.println over and over), is to write import static java.lang.System.*

And yes, System has to be capitalized for it to work

635875221802677963.jpg

import = allows Java to reference other classes without having to declare the full name every time.

static = though not all imported classes are static, Java.lang.System is. If you don't write it, Java will complain. (i.e. errors will show up)

* = tells the import to import everything within that class (meaning it imports System.out, System.in, etc.); It also works as a multiplication sign.

Now click Run and hit Ok when the pop-up appears, asking which file you want to run

635875222739865446.jpg

Your end result should look something like this

635875223474397469.jpg

Congrats,

You've created and ran your first Java project!

Code written from this tutorial can be found on Github

(Next tutorial is being worked on)

I'll try to upload a new tutorial every week or so (but because I'm finishing up school work, the 1Tutorial per week thing may not prove true... So I'll try to upload one once every 2 weeks)

I would love to get some feedback on this first tutorial.

How was it? too short? too long? too much info all at once?

Thanks

Comments

No Comments Exist

Be the first, drop a comment!