Java Tutorial #1 — “Hello World”, Simple i/o


Topics Discussed: In the most abstract fashion possible, I explain what we need to include and write to output text to the screen using java.

Source Code Available Here


In this video we talk a little bit about the structure of a normal java file.
Essentially, every java file (while we’re learning) will start with the words:

public class *filename*{

}//end class anything after // is considered a comment

The basic idea behind this line is that the compiler needs to know the name of the class in which things are being executed. We’ll talk a bit more about what the public means in this statement.


public static void main(String args[]){
//contents of main

}

The primary confusion from this statement is that it’s a LOT of different stuff all in one place. An access modifier, static declaration, command line arguments and other stuff. For now the only thing that you need to know about this statement is that it denotes where the main part of your program will be executing. There will be times when we operate outside of ‘main’, but that won’t be for some time.


Including and setting up Scanner:


import java.util.Scanner; // at the top of the program
Scanner input = new Scanner(System.in);

This code includes an external library called “Scanner” which is used to take in input from our user. The code where we say ‘new’ allows us to use the word input as a way to take in user data. We’ll talk more about how this works a lot later in the tutorials.


Declaring variables are done like this:


Type Name;
(examples)
int itemCost;//an integer with the name itemCost
char c; // A character with the name "c"-- 
//mind you, this character CAN store characters other than the letter C, as C is JUST a name

Variables are declared to store some kind of data, and the type of data they can store is based on how they are declared. About I mentioned ints, which store integers, or ‘non-decimal numbers’.


Outputting data in java is pretty easy. We use the following code:


System.out.println(a); // This would print out the variable a
System.out.println("Hello World"); // This would output the words "Hello World" exactly as they appear in the quotation marks.
System.out.println("Hello World" + a);//This would output Hello World followed by the contents of a

With that being said, we’ll talk a bit more about the syntax of outputting more later on in the series.


Lastly, we’re going to talk about commenting:


// This is a single line comment, anything you put after this is treated as a 'note' 
/*
this is inside a multi - line comment
* as is this many people put a * at the beginning of each line in a 
multi-line comment to show that it's a comment to readers of the code
The end of this line ends the multi line comment */

Comments are notes that you and readers of your code can use to better understand the functions being carried out by your code are. I highly recommend commenting code heavily.

Leave a Reply

Your email address will not be published. Required fields are marked *