Complete Java Course | Chapter 1 : Introduction

Part #1 : History & Evolution

1. Historical Context

As I have come to understand it, Programming can be described as writing a set of instructions for accomplishing a task, such that your computer can execute them. For example, writing the set of instructions for how to add two numbers. Now you can write those instructions in English, or in Java. Just like we are using English to communicate here, programming languages are used to communicate with computers.

Modern computers run on microprocessors that consist of billions of transistors, which act like electrical switches turning the electricity On or Off. We represent these states of electricity On & Off as Binary 1s and 0s respectively. This is why, Initial programming was done by manually entering binary machine instructions. As long as the programs were just a few hundred instructions long, this approach worked.

Example binary machine code

As programs grew in size, assembly language was invented so that programmers could deal with larger and increasingly complex programs by using symbolic representations of machine instructions.

Example Assembly language

As programs continued to grow in size and complexity in an increasingly computerized world, programmers naturally required more tools to be able to deal with this increasing complexity.

Thereby the 1960s gave birth to structured programming, languages such as C, invented by Dennis Ritchie represent this style of programming. Structured programming is a way of writing programs which is aimed at improving the clarity, readability and quality of the program by making use of structured control flow constructs such as : selection (if / else), repetition (for & while), code blocks and subroutines.

example c code

However, even with structured programming techniques, once a project reached a certain size, the complexity exceeded what a programmer could manage. Therefore, By Early 1980’s many projects were already pushing the structured approach past its limits.

To solve this problem, a new approach to programing was invented, called object oriented programming (OOP). Although OOP will be discussed in greater detail in Part #2, we briefly define it here :

OOP is a programming methodology that helps organize complex programs through the use of abstraction, inheritance, encapsulation and polymorphism.

2. Evolution of Java

C++ extended C by adding object oriented features and thereby enabled programmers to comprehend much larger programs. Invented by Bjarne Stroustrup, who initially called it “C with Classes”.

example C++ code

Although C++ blended the structured programming approach with object oriented methodology quite well, The need for a platform independent, portable language that could run on a variety of CPUs under different environments led to the creation of Java, which was conceived at sun microsystems in 1991. However, it was the emergence of Internet around the same time that propelled Java to the forefront of the programming world. As web too demanded portable programs.

As discussed, each innovation in language design was driven by the need to solve a fundamental problem, that preceding languages could not solve. Thus, we observe that programming languages evolve for two main reasons :

  • To adapt to changing environment and uses. For example, the dawn of internet.
  • To implement improvements in the art of programming.

Part #2 : Overview

Object oriented programming is at the core of Java, so it is best we begin by discussing its core aspects.

1. Object Oriented Principles

Abstraction : Abstraction is about hiding irrelevant details and persevering only relevant details as per a given situation.

For example, imagine you are shifting houses. You place all the electronics in a box, tape is down and write “electronic stuff” on top. Now, the packers & movers need not worry about what exactly is inside the box. All they need to know is, that the box contains electronics and that it needs to be handled with care.

This is the essence of abstraction. We use it in our daily lives and it also forms an essential part of object oriented programming.

Encapsulation : As the name suggests, encapsulation is like a protective wrapper that protects the code and data inside from outside interference and misuse. Continuing with the house shifting example. You can think of it as the box and tape that actually separates the items inside from the outside world.

Inheritance : Inheritance allows any object to acquire properties from its parent object. The object inherits its general attributes from its parent, while itself needing to define only those qualities that make it unique. For example, within the “electronic stuff” box, there might be a smaller box for wires. Further, you only need to mention “wires” on that box, “electronic wires” is not requires as that much information is inherited from the bigger box.

Polymorphism : Polymorphism allows us to use a general implementation of some object in various forms. For example, if you implement a stack in Java, you can use the same generic implementation of stack to create various stacks containing different types of values, like integers, decimal numbers or characters.

Extending the house shifting analogy, the same label can be used to mark different boxed. Each box has the same label taped onto it. You can write to labels it as “electronics” or “toys” or “utensils” and so on.

All object oriented languages provide mechanisms that allow us to implement the above discussed object oriented principles.

2. First Few Simple Programs

Let us start by writing and examining our first program, the HelloWorld program. I personally am using the Eclipse IDE to write my Java programs.

The program :

As its output, it simply prints the following : Can I Think?

Examining the HelloWorld Program :

* @author AviralJanveja
* This is a Simple Hello World JAVA Program!
* Usually the first one that programmers write.

Above is a multi-line comment. Comments are used to describe or explain the operation of the program to anyone who is reading the source code. Contents of the comment are ignored by the compiler.

Single-line comments can be added in this way : // This is a comment.

public class HelloWorld {....    }

The above line uses a classkeyword to declare that a new class is being defined. HelloWorldis an identifier that is the name of this class.

For now, we don’t worry about the details of a “class”, except to note that all program activity in Java occurs within a class.

The entire class definition, including all its members, will be between the curly braces {} .

public static void main(String[] args) {....    }

The above line begins the main() method. All Java applications begin execution by calling the main().

The Public keyword is an access specifier. It allows the programmer to control the visibility of class members : methods like main() and other data.

When a class member is specified as public , it means that it can be accessed by code outside the class in which it is declared. In this case, main() must be declared as public since it must be called by code outside of its class when the program execution begins.

The opposite of public is private which prevents a class member from being used by code defined outside of its class.

For now, don’t worry about the keywords static void string[] & args . These will be discussed it later chapters as required.

Just like class, all code of the method main()will be written within its opening & closing curly braces {} .

Finally, main() is the starting place for your program. A complex program will have dozens of classes, only one of which will need to have a main() method to get things started.

System.out.println("Can I Think?");

The above line outputs the string Can I Think? followed by a newline.

Briefly for now, System is a predefined class that provides access to the system. out is the output stream that is connected to the console screen. println displays the string which is passed to it, “Can I Think?” in this case.

The println statement, like all statements in Java ends with a semicolon ; . In this program, technically, this was the only statement as the rest was simply class and method declaration.

The Program :

The “variable” is one of the most fundamental concepts in programming. It is a named memory location that may be assigned a value by your program. The following program illustrates this concept :

When you run this program, you see the following output :

This is num: 100
The Value of num * 2 is 200

Examining the Variable Program :

int num;

Java requires that variables be declared before they are used.

This line declares a variable named num of type int that is integer. Java has several data types as we shall see, including int , char that is character and float for floating-point numbers.

This type variable-name is the general form of variable declaration in Java. You can also declare several variables of the same type together separated by commas, for example : int num, sum, diff;.

num = 100;

The above line assigns the value 100to num, via the assignment operator =.

System.out.println("This is num: " +num);

This line outputs the string This is num: 100. The + sign causes the value of num to be appended to the string “This is num:” that precedes it.

num = num * 2;

This line assigns num the value num times 2. Java like most other languages uses the * operator to indicate multiplication. After this line executes, num will contain the value 200.

System.out.print("The Value of num * 2 is ");  System.out.println(num);

The final two lines of the program are used to output the updated value of num as follows The value of num * 2 is 200. First, the built in print() method is used to display “The value of num * 2 is”. print() method is just like println() , except that it does not output a newline after each call.

Finally, notice that in the call to println(num) , num is used by itself. This illustrates that print()and println()can both be used to output values of Java’s built-in types.

3. Two Control Statements

Although we will cover control statements in detail later, here we make a brief introduction.

The If Statement

The if statement in Java has the following form :

if(condition) statement;

Here If conditionis true, then the statementis executed or else it is bypassed.

For example,

if (num < 100) System.out.println("num is less than 100");

In this case, If num contains a value that is less than 100, the conditional expression is true and thus println() will execute, otherwise it will be bypassed and you will see no output in the console window.

Here is a program, that illustrates the if statement :

The Output generated by this program in the console window will look as follows, Notice, how the last two println() statements are bypassed :

Figure 1

The For Loop

Loop statements are an important part of nearly every programming language, that allows us to repeat a certain statement or iterate over a section of our code a couple of times. We look at the for loop here.

The general form of for loop is as follows :

for (initialization, condition, iteration) statement;

initialization initializes a certain variable to an initial value of our choice. We call it the “loop control variable”.

condition refers to a conditional expression just like if statement, which needs to be true in order for the loop to continue iterating. The for loop continues to iterate as long as this condition is true.

iteration tells us how the value of our loop control variable updates with each iteration of the for loop.

The following program, illustrates the for loop. Make sure to read the commentswritten alongside code for better understanding.

As the comment in the end explains, In this example x is the loop control variable initialized to zero as shown x = 0.

x<10 is the condition that needs to be true for the loop to continue executing and x++ is Java’s increment operator ++ which is used to increase its operand’s value by one.

Using the increment operator is more efficient, therefore in professionally written Java programs, you will always see x++ used instead of something like

x = x + 1 ;

4. Blocks of Code

Java allows two or more statements to be grouped into blocks of code, also called code blocks. This is done by enclosing the statements between opening and closing curly braces {}.

Once a block of code is created, the statements inside the block form a logical unit, meaning one statement cannot execute without the other also executing. Meaning, the statements execute together.

For example, a block can be created for if statement :

if (x < y) { // begin of blockx = y;
y = 0;
} // end of block

Here, if x < y then both statements inside the block will execute together.

This is another example of block of code used with a for loop.

In this case, each time a loop iterates, all the three statements inside the block will be executed. This is shown by the output of the above program :

This is x:0This is y:20This is x:1This is y:19This is x:2This is y:18This is x:3This is y:17This is x:4This is y:16This is x:5This is y:15This is x:6This is y:14This is x:7This is y:13This is x:8This is y:12This is x:9This is y:11This is x:10This is y:10This is x:11This is y:9This is x:12This is y:8This is x:13This is y:7This is x:14This is y:6This is x:15This is y:5This is x:16This is y:4This is x:17This is y:3This is x:18This is y:2This is x:19This is y:1This is x:20This is y:0

You could try the above program in your system to verify for yourself.

5. Syntax of Java

After looking at the first few simple programs, let us formally look at the atomic elements of Java language.

Java programs are a collection of whitespaces, identifiers, literals, comments, operators, separators and keywords. We describe these briefly here :

  • Whitespace : Java is a freeform language, which means you don’t need to follow any special indentation rules. In Java, whitespace is a space, tab or newline.
  • Identifiers : Identifiers are used for class names, method names and variable names. It can be any sequence of uppercase / lowercase letters and numbers, however they cannot begin with numbers. Remember that Java is case sensitive, so “VALUE” is a different identifier than “value”.

For example, some valid Java identifiers are :

 MyName, myname, my_name, My_Name789.

Whereas, some invalid identifier names include

7name, my-name, my/name. 
  • Literals : A constant value in Java is created by using a literal representation of it. For example, 100is integer, 98.6 is a floating-point value, 'X' is a character constant and "This is a test" is a string. A literal can be used anywhere a value of its type is allowed.
  • Comments : As seen above already, Java allows single-line and multi-line comments. There is also a third type called a documentation comment. Which is used to produce an HTML file that documents your program, it looks as follows :
/** This is a documentation comment.*/
  • Separators : The most commonly used separator in Java is the semicolon ;, As you have seen, it is used to terminate statements. Some other separators are :
() - Parenthesis
{} - Braces
[] - Brackets
, - Comma
. - Period

Their use will get clear as we go along. As you might have noticed, we have already used most of the separators mentioned above in our example programs.

  • Java Keywords : We have referred to keywords above, like class int and so on. There are around 60 such keywords in Java which are reserved to perform a unique function like defining a data-type or defining a program control flow method. Therefore, these cannot be used as names for a variable, class or method.

End of Chapter 1




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store