Basics of programming – Java Tutorial for beginner

In this tutorial you will learn the basics of programming that applies to most programming languages but we will write the code in java. We will use Netbeans IDE with Java SE support. You can get a copy of Netbeans IDE program at https://netbeans.org/downloads/8.2/, but first this requires java development kit to be installed which you can get here https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

I’m using Java 8 (any version later than 8 will do) and Netbeans IDE 8.2 (any that supports java 8 will do including version 11)

We will deal with 6 aspects of the java programming language but for this specific tutorial we will deal with Literals, Operators and Variables. All these aspects form the building blocks of the Java Virtual Machine which is the program designed to help make your program run on your computer.

Java Virtual Machine

This is a program that helps run your code and is part of the java development kit. I won’t explain much but what you need to know is that this program is required to run your program and it should be setup after you installed the java development kit. More on this on the advanced tutorials.

Variables

A variable is a container that store data in a computer program. This is directly similar to variables mathematics algebra. A variable can store a value of a certain type of data and each variable must have a unique name within its scope. I’ll explain about scope later. For this tutorial we will deal with 5 variable types, java has more variable types but we won’t go into those.

Lets go through the variable types in the order of their sizes (remember a variable is a container to store data):

  • boolean – can only be one of two values which is either a true or a false.
  • char – a single character such as ‘n’, ‘D’ or even a number like ‘5’
  • int – an integer number which is a whole number like 2, 23 or 512
  • double – a fractional number such as 0.4, 5.0 or 27.2
  • Object – a structure that can contain other data types such as listed above (details on this type is on its own tutorial about object oriented programming)

Those are all the types of data we need to create a program but understand that a variable is a container and a container has a size. Therefore you cannot fit a cat into your normal lunch box but you can fit your lunch into your cat house and that is how variables work. You must also note that even though water in a glass is small compared to your dog house you can’t meaningfully store the water in your dog house. This is to introduce three important rules about variables that I wish I knew from college:

  1. A variable can only be of one type, a glass was meant to hold water and is good at that but a glass cant really hold a loaf of bread and you can change a glass into a bread bin.
  2. A variable has a size based on its type. Which means a glass can only hold water equivalent to its size. If you have a bowl you can’t fit its contents into a glass yet the contents of a glass can be easily stored in a bowl this leads to the third rule.
  3. A variable of a bigger size can store data of a smaller type of variable but of the same kind.

Thats it about variables. Practically variables are written in java as {TYPE} {NAME}. That is each variable must have a specific type and a name. now the types are those discussed above and the name can be anything you want as long as the name sticks to java rules. I won’t go deep into that but a name cannot have spaces or other characters other than an underscore and cannot start with a number. examples for each type above are below. (Please note all data types are written in lower case except for Object data type)

boolean testPassed
char initial
int number_of_people
double WalletAMOUNT
Object person

Literals

A literal is an actual piece of data of a specific type. The types are exactly as those discussed above for variables. Literals are actual pieces of data and as you may have guessed, we store literals inside variables. If a variable example is a glass then a literal would be water. I will list a way to write literals for the five data types below

  • boolean – is written simply as either true or false
  • char – is written within a pair of single quotes like 'd' or '5'
  • int – is written simply as 52 or 1024
  • double – is written as 0.85 or 9.0
  • Object – Does not have a literal except for special object types which i’ll explain at a later stage.

Operators

Operators are like actions that the computer must do and of course this actions are taken on literals and variables. Actions are the glue for instructions. By combining these three (Variables, Literals and Operators) we end up having statements and expression. A statement in java is one complete instruction that a java program can perform. An expression is a calculation(evaluation or arithmetic) within a statement. All statements in java must be terminated by a semi-colon character ; . Java defines a set of rules on what cuts as statement and what does not, if java is not satisfied that one of your statements is complete it will not work then your program simply won’t run (or compile, more on this later).

A program for now is simply a collection of statements and we will write each statement on a new line. This is not a rule but its for readability purposes. Another important aspect to understand is that white space doesn’t matter as long as its between operators and other operators/variables or literals but its not allowed to put spaces between the words of a variable/operator.

The most important operator in java is the assignment operator. Its function is to copy the contents on the right to whatever is on the left and in most cases a variable because its a container and can store data. The symbol used is an equal character = for example: to assign the value true to the boolean variable above we will use boolean testPassed = true; This means that testPassed will contain the data true. Now i’ll quickly list other operators, a quick description and an example.

Arithmetic operators can only be used on number types which is int and double. They form expressions that return a number data type. + for addition, – for subtraction, / for division and * for multiplication.

Comparison operators are used to compare and they form expressions that return a boolean value. == for equals to, > for greater than, < for less than, <= for less or equal to, >= for greater or equal to, != for not equal to.

Logical operators are used for logical comparison in a similar manner as logic gates. ! for negation (that is if the variable was true if will become false or vise versa), && for AND, || for OR

Practical

Now lets put all the above into practice. Open your Netbeans IDE and create a new ‘Java Application’ project and name it whatever you want

Never mind all the stuff you see inside the file BasicTutorial.java but the only place we should be concerned about is the place written ‘code application logic here’. This is where we are going to code and its inside the two curly brackets {}. Here you can write as many lines of codes as you need and remove the entire line starting with // TODO

First we are going to try and declare three variables (containers) for our program using the following code:

int soccerMatch1;
int soccerMatch2;
int totalScore;

These is called declaring variables and these statements simple create the variables for use later in the program. All variables must be declared in a similar manner before use. It is like announcing the java that we need three containers to store whole numbers. Java cannot read minds and thus must be told before hand of all containers else it won’t know what to do.

Next we will perform some calculations by adding some more statements. We are going to add the two soccer match values and store it inside totalScore.

totalScore = soccerMatch1 + soccerMatch2;

Netbeans will underline the soccer match variables and if you mouse over them, a messages will say these variables may have not been initialized, This is a message from Java indicating that the variables are empty. It is impossible to try and add two empty variables thats because variables are not added together, they are simply containers but what is added is the values inside of them. So we need to put values inside the variables using literals we learnt above. We add code before the addition statement but after the declaration of the three variables

soccerMatch1 = 5;
soccerMatch2 = 7;

Now netbeans should remove the underline meaning all is well, Then to test that the program really works we use this statement after all other statements to display the value of totalScore

System.out.println(totalScore);

I’ll explain what the statement truly mean under Objects for now any variable between the brackets will have its contents displayed or expression will have its resulting value displayed. The program now looks like below

Now click the play button on the toolbar (Run Project)

The result of the program should be displayed on the output panel.

Share Button

Leave a Reply

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