Java One-Oh-One

Java is a high-level programming language that has been around for two decades and is still going strong. It is object-oriented, is platform independent, has wonderful documentation and community support, is free, and is everywhere – the moment you install Java, a screen generally comes up which states ‘3 billion devices run Java’.

Yes, you must find the ancient scriptures of Sun Micro systems to learn a language, a language which, unknown to many, continues to flourish in the twenty-first century, a language so precious its priceless (in more ways than one), a language not lost in the sands of time.

Or, instead of finding those scriptures, you may read this blog post, which shall unravel the deep, dark mysteries which surround this language, the language called Java.

All right, enough adventure talk, let’s begin already.

We’re going to go through some basic programming concepts in Java, namely variables, operators, selection, iteration and jump statements. If you’re new to programming, you’d want to read this post with hardcore determination, while if you’ve already done some non-Java programming in the past, I’d recommend you quickly skim through this post to get a feel of the language. And if you’re a Java developer, how about you go through this post and let me know about errors, if any? 🙂 Email manasdtrivedi@gmail.com

I’ve used the idea of ‘learning by example’ in this post. If you don’t understand the theory of a section, say, increment/decrement operators, then go through the example code for it, look at the output, and try to figure out the reasons for said output. Since Java is quite close to the way we speak in English, the code is generally pretty clear. But yes, if you indeed have queries, you’re welcome to drop an email.

Variables

Variables are named ‘boxes’ of a certain type, which hold values of that type. For example, a fruit box called fruitBox1 will hold a fruit in it, say a mango, or a jackfruit. Similarly, an integer variable called num will store an integer in it, say 7 or -9 or 0 or 17395.

The way we declare and initialize variables in Java is as shown:

datatype variableName = value;

So, an integer variable called ‘num’ is declared and initialized with the value 3 as:

int num = 3;

Here, ‘int’ is the datatype (i.e. the type of the variable), ‘num’ is the name of the variable, and ‘3’ is the value put into variable ‘num’. In simple terms, int num = 3 means we’re putting the value 3 into an integer box (integer variable) called ‘num’.

Note that the ‘=’ sign doesn’t mean equality i.e. int num = 3 doesn’t mean ‘num is equal to 3’. Rather, we’re inserting 3 into the box called ‘num’. You can perhaps imagine replacing the ‘=’ sign with a ‘<-’ (leftward arrow) to reinforce this idea.

A variable in Java can have any name, as long as it isn’t a keyword, doesn’t begin with a digit, and is composed of only alphabets, digits, underscore and/or dollar sign.

There are 9 types of variables in Java:

1. Byte variables can store values from -128 to 127:

byte var1 = 25;

2. Short variables can store values from -32768 to 32767:

short var_2 = 5678;

3. Integer variables can store values from -2147483648 to 2147483647:

int var_3 = 9999999;

4. Long variables can store values from -9223372036854775808 to 9223372036854775807:

Postfix form: Follows ‘use-then-change’ rule, i.e. operand++ or operand– returns the current value of operand and then increments/decrements operand. Note that the ‘changed’ value isn’t returned, the value before changing is.

Example:

int a = 2, b = 2;

int c = a++;

int d = b--;

After execution of the above statements, a will store value 3, c will store value 2, b will store value 1, d will store value 2.

3. Relational operators:

Here and beyond, op1 means first operand, and op2 means second operand.

Selection statements

Selection statements allow us to choose a set of instructions for execution based on an expression’s truth value.

1. if statement:

Statements inside the if block get executed only if the condition evaluates to true. A ‘block’ is denoted by curly brackets i.e. ‘{’ and ‘}’. All statements present inside the curly brackets are a part of one ‘block’ of statements.

if…else statement: Statements inside the if block get executed if the condition evaluates to true, else the statements inside the else block get executed.

2. switch statement:

This statement tests the value of an expression against a list of integers or characters. Upon finding a match, the statements associated with that integer or character is executed. If no match is found, the statements associated with the default label (if exists) is executed. It is generally a good idea to have a default.

Iteration statements

These statements (called ‘loops’) are used to execute a set of instructions (the body) repeatedly as long as the value of an expression is true. These statements generally have four parts: the initialization (where the control variable is initialized), the expression (whose truth value decides whether the body will be executed), the updation (which updates the control variable), and the body of the loop.

1.for loop:

The first three elements of the loop (initialization, expression, updation) are gathered at the top of the loop (generally). The expression is first checked, then the body is executed if the expression is true.

Here, x is first initialized as 1. The expression x <= 2 is checked. It is true, so the body is executed. Now, the updation x++ takes place. x is now 2. The expression x <= 2 is checked. It is true, so the body is executed. Updation x++ takes place once again. x is now 3. The expression x <= 2 is checked. It is false. So, the loop is exited.

The output window displays the following output:

Value of x is 1.

Value of x is 2.

We can also skip one or more of the three elements at the top of the for loop. For example, for(; x <= 2; x++) {…} is perfectly legal, as long as x is declared and initialized somewhere above the loop. Note that although the initialization expression was removed, the semicolon wasn’t!

2. while loop:

It is pretty similar to an if statement in the sense that the body of the loop is executed if the expression evaluates to true. The initialization of the loop control variable (if any) is somewhere above the loop, and the updation (if any) is generally a part of the body.

Here, k is initialized to 2. The condition k > 0 is checked. It is true, so the body is executed. In the body, the updation k– occurs. k becomes 1. The condition k > 0 is checked again. It is true, so the body is executed. k– occurs, k becomes 0. Now the condition k > 0 is checked, which returns false. So, the loop is exited.

The output window displays the following output:

k is greater than 0.

k is greater than 0.

k is now 0.

3. do-while loop:

Here, the loop control expression is evaluated after the body is run, and its truth then determines whether the body is to be executed again. The initialization (if any) is above the loop, and the updation (if any) is generally a part of the body.

Here, x is initialized to 1. The condition x <= 3 is evaluated to be true. Hence, we enter the body. x is not 2, hence the continue statement isn’t executed. ‘x is now 1.’ is printed. Updation takes place, x becomes 2. The condition x <= 3 is evaluated. It is true. So, we enter the body again. This time x is indeed 2, hence the continue statement is executed. The rest of the body isn’t executed, we directly jump to the next iteration. Updation takes place, x becomes 3. Condition x <= 3 is yet again true. We enter the body once again. Since x isn’t 2, the continue statement isn’t executed. “x is now 3” is printed. Updation: x becomes 4. Now the condition x <= 3 is false, hence we exit the loop.

The output window shows the following output:

x is now 1.

x is now 3.

Reached this far? Congratulations! You now have a very sound knowledge of the basics of Java, which we’ll require soon.

Soon?

Yes, I’m going to write another blog post with deals with creating interactive Java applications which have components like buttons, textfields, and checkboxes.