8/20/2019 A Primer on Java
1/59
8/20/2019 A Primer on Java
2/59
A Primer on Java
Second Edition
Rahul Batra
This book is for sale at http://leanpub.com/aprimeronjava
This version was published on 2014-05-26
This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishingprocess. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools andmany iterations to get reader feedback, pivot until you have the right book and build traction onceyou do.
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0Unported License
http://creativecommons.org/licenses/by-nc-nd/3.0/deed.en_UShttp://creativecommons.org/licenses/by-nc-nd/3.0/deed.en_UShttp://creativecommons.org/licenses/by-nc-nd/3.0/deed.en_UShttp://leanpub.com/manifestohttp://leanpub.com/http://leanpub.com/aprimeronjava
8/20/2019 A Primer on Java
3/59
Also By Rahul BatraA Primer on SQL
http://leanpub.com/aprimeronsqlhttp://leanpub.com/u/rbatra
8/20/2019 A Primer on Java
4/59
To Pria
8/20/2019 A Primer on Java
5/59
Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . i
About the author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ii
Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iii
1 An introduction to Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.1 What is Java? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.2 Advantages of Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.3 Getting Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21.4 Writing your first Java program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21.5 Analyzing your first program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 Data Types and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52.1 Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52.2 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52.3 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3 Introducing Classes & Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83.1 Abstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83.2 Defining classes and objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83.3 Creating a class in Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93.4 Objects and references . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4 Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124.1 Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124.2 If-Else Conditional . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134.3 Switch Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
5 Iteration Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165.1 The while loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165.2 The do-while loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175.3 The for loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175.4 break and continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
6 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
8/20/2019 A Primer on Java
6/59
CONTENTS
6.1 Array Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206.2 Using Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216.3 The enhanced for loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
7 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247.1 Parameters & Return Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247.2 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
8 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278.1 String Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278.2 Escape Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288.3 Comparing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
9 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319.1 Extending a class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
9.2 Access Specifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339.3 The super Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339.4 The Object class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
10 Abstract Classes and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3710.1 Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3710.2 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3710.3 Multiple Inheritance and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
11 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4011.1 The try-catch block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4011.2 Java Exception Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4111.3 Throwing an exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4211.4 The finally statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
12 Packages in Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4512.1 What is a package? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4512.2 Packaging a class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4512.3 The CLASSPATH variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4612.4 Importing packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Appendix: Code Editors and Integrated Development Environments . . . . . . . . . . . . 49
Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
8/20/2019 A Primer on Java
7/59
PrefaceWelcome to the second edition of A Primer on Java , a short (e)book meant as a gentle introduction tothe basics of Java. This text was previously referred to as Essential Java in its first edition. I changedthe name on the suggestion of the Reddit community to keep it in line with my other book on SQL.Thanks for the excellent suggestion people.
As before, no prior programming experience is necessary to read this text. But any knowledge of how programs work will benefit the reader greatly while reading the text. Two new chapters (Stringsand Packages) have been added, but this text is still far from being a complete introduction to Java.The Further Reading section at the end of the book should give you some decent pointers on what
to pick up next.Your questions, comments, criticism, encouragement and corrections are most welcome and you cane-mail me at rhlbatra[aht]hotmail[dot]com . I will try answering all on-topic mails and will try toinclude suggestions, errors and omissions in future editions.
Rahul Batra (26th May 2014)
8/20/2019 A Primer on Java
8/59
About the authorRahul Batra was first introduced to programming in 1996 in GWBASIC, but he did not seriouslyforay into it till 2001 when he started learning C++. Along the way, there were dabblings in manyother languages like C, Ruby, Perl and Java. He has worked on Oracle, MySQL, Sybase ASA, Ingresand SQLite.
Rahul has been programming professionally since 2006 and currently lives and works in Gurgaon,India.
8/20/2019 A Primer on Java
9/59
AcknowledgementsThis text would not have been possible without the unwavering support of my family and friendsand I owe many thanks to them. My parents, who raised me and always kept my education as theforemost priority, and without whose encouragement to pursue my dreams I would not even havestarted this. My wife, Pria, who kept telling me to get into Java and kept on believing that I couldwrite.
I also owe my gratitude to my sister for always looking out for me. And finally many thanks to myniece, nephew and friends for bring me such joy into my life.
8/20/2019 A Primer on Java
10/59
1 An introduction to Java1.1 What is Java?
To perform useful tasks on a computer, we either use a prebuilt software application (like a texteditor) or build one ourselves. This process of making software using instructions that a computercan understand is called programming . The set of instructions given is called a program .
These instructions or programs must be given in a precise, formal language which is referred to asa programming language . Java is one such programming language.
Before a program can be run, we require another program to translate the program from the languagewe wrote in (say Java or Pascal) to a language the underlying hardware understands, i.e. a machine language . While this is not a one step process, for simplification we consider it as such. This programis called a compiler . Another class of programs with similar goals is an interpreter , which translatesprogram statements directly into actions without the need for compilation to a machine language.This is also referred to as program execution .
Java uses both a compiler and an interpreter to execute its programs. In the first stage, the Javacompiler translates the program listing to a intermediate bytecode . This bytecode is in the form of coded instructions a virtual hardware machine called the Java Virtual Machine (JVM) understands.The Java interpreter then runs this bytecode according to the specification of this virtual computerof sorts (the JVM) and we have our program execution.
1.2 Advantages of Java
Since Java is compiled to the JVM bytecode specification, it means that a Java program will runon any architecture or operating system where a Java interpreter exists. This is different from thetypical execution flow of compiled languages like Pascal or C, because their source code has to berecompiled to target whichever underlying machine exists. The reason for this particular designchoice was the fact that one of Java’s original goals was to be the programming language forvaried consumer devices, not just personal computers. Since these devices would not be the ideal
computational machines for doing programming and compilation, the concept of a virtual machineto run intermediate bytecode became necessary. You can develop and compile your Java programson the machine of your choice, and execute the resulting bytecode on any other machine having a JVM.
Java also uses a familiar C/C++ style syntax which is widely used. Java itself has become one of themost popular languages since its inception in the mid-90s. The good side effect is that there are ahuge number of jobs, books and reusable code available for the Java programmer.
8/20/2019 A Primer on Java
11/59
An introduction to Java 2
History of Java
Around 1991, James Gosling and his team at Sun Microsystems were trying to create a new
programming language for use in appliances like televisions and toasters. The name of this projectwas Oak . While the concept of programmable devices did not take off back then, the language was
kept and subsequently used in programming on the web. This language is now called Java .
1.3 Getting Java
When you go to download Java, you are faced with two variants of it: the Java Development Kit(JDK) and the Java Runtime Environment (JRE). The former is used by people wanting to write
programs in Java whereas the latter by those who just wish to run Java programs. Thus for thepurpose of this book, we will be needing a JDK.
The JDK itself comes in 3 editions .
1. Java SE - Java Standard Edition
2. Java EE - Java Enterprise Edition
3. Java ME - Java Mobile Edition
We will be focussing on Java SE since the intended target machines for this edition is personalcomputers. As of the date of this text, the current version of Java is Java 8 but any version upwardsof Java 6 should serve you fine. While the step-by-step installation of Java SE for all the majoroperating systems is beyond the scope of this book, it is a fairly simple procedure much like theinstall process of any other package on your choice of OS. You can download your copy of Java SEfrom the Oracle Technology Network¹ website. Installation instructions are also available from thisURL.
Another thing you would need is a good text editor for editing Java source code. Choose any editorthat you are comfortable with, preferably one with syntax highlighting (colors different words insource code with special meaning). If you are yet to decide on one, refer to the Appendix: CodeEditors and Integrated Development Environments at the end of the book to get a list of suggestededitors.
1.4 Writing your first Java program
The first program we are going to try out will simply print out the words “Hello World!” on thescreen. This is a common first program to teach and is a good way to learn about basic programstructure. Fire up your editor and enter the text given below saving the file as HelloWorld.java .
¹http://www.oracle.com/technetwork/java/javase/downloads/index.html
http://www.oracle.com/technetwork/java/javase/downloads/index.htmlhttp://www.oracle.com/technetwork/java/javase/downloads/index.html
8/20/2019 A Primer on Java
12/59
An introduction to Java 3
Listing: the source code of our Hello World Program
1 class HelloWorld {
2 public static void main(String[] args) {
3 System. out.println("Hello World!");
4 }
5 }
Note the case (capitalization) of certain words in the program. Also note how we have indented certain lines to give the appearance of a nested structure to our listing. While such indentation isnot necessary in Java (to a compiler), it is done for human readability and is not considered optionalby other programmers.
To run this program, we go through a two step process. Firstly we need to compile the program using
the Java compiler javac and then run it through the interpreter which is called simply java . Look atthe commands entered below alongwith the output of our program shown on the last line. Run thesame on a console (command prompt) using your choice of directories and remember to save yourprogram in the same directory.
Figure: output of our Hello World program
1 d:\code\ experiments\java>**javac HelloWorld.java**
2 d:\code\ experiments\java>**java HelloWorld**
3 Hello World!
If you enounter an error like ‘javac is not recognized as an internal or external command’ , you haveprobably not added your JDK bin directory to your PATH variable. Check your OS manual or checkonline documentation on how to achieve this. If the first step ran successfully, you would have afile name HelloWorld.class in your directory. Be careful not to include the .class extension whenrunning the program through the Java interpreter in step 2. If all goes well, you should see the textprinted on the command line. Congratulations!
1.5 Analyzing your first program
A full explaination of all the components of this program listing goes beyond the scope of thischapter. For now it is important to study it as a taste of what Java programs look like. The first linedeclares the title of the program as HelloWorld and this must be done in the same casing (uppercaseand lowercase) as the name of your .java file.
The second line declares the main, which is where the program starts running. Notice how thisis within the program title’s curly braces. It also has its own set of curly braces, and within it weencounter the line which actually produces our desired output. The println is a way to tell Java to
8/20/2019 A Primer on Java
13/59
An introduction to Java 4
output something on the screen. We terminated this line with a semicolon and finally wrapped upour code listing with the closing curly braces which we had opened before.
Syntactically Java resembles the C/C++ family of programming languages. It is from them that it gets
the concept of terminating a statement with a semicolon. Nesting of curly braces define heirarchyand a set of curly braces are used to combine multiple statements into a block. A program terminateswhen the last statement of the main completes its execution.
8/20/2019 A Primer on Java
14/59
2 Data Types and VariablesComputers are data processing machines. Their operations revolve around transforming data intomeaningful information. For a programming language like Java to be able to act as a medium forsuch a transformation, it must understand how to work with different kinds of data.
2.1 Literals
Any data that you enter directly into your code is called a literal. That means that in our firstprogram, the bunch of characters “Hello World!” was a string literal. Literals can also be numeric innature.
The point of a literal is to give a formal name to data that you enter into the source code of theprogram, without it becoming a part of the vocabulary of Java as a programming language.
2.2 Variables
A variable is a container for data. We assign a purposeful name to some data that we wish to refer toin other statements of our program, and this binding of data to a name is called variable assignment.Consider the variable assignments given below.
Listing: Some variable assignments
1 a = 10
2 firstName = "Alan"
3 lastName = "Turing"
4 amount = 103.55
It should be noted that the above are not valid statements in Java. However, these pseudo codestatements illustrate the concept of variables nicely. While the first variable name ‘a’ is not
descriptive, we can see that it refers to a numeric value. The other variable names are morepurposeful and store characters and even numbers with a decimal point in them. What would makethese valid Java statements is a few syntactic rules and the important declaration of what is the type of data the variable will store.
8/20/2019 A Primer on Java
15/59
Data Types and Variables 6
2.3 Data Types
A type is a way to tie together the data of a program to the actual storage details of such data. The
Java compiler must know what kind of data it will be dealing with for every variable. This way itwill be able to allocate the correct amount of memory to store such variables. And so we have toexplicitly declare our variables with data types in Java.
Java has numeric data types like (int, float, long etc.) , character (char) data type and a special typecalled boolean which only stores true or false values. To illustrate all these concepts better, we willnow look at a program incorporating variables to calculate simple interest given the rate of interest,time period and the principal amount.
Listing: Calculating simple interest using numeric variables
1 class SimpleInterest {
2 public static void main(String[] args) {3 /* Simple Interest Program
4 SI = P * R * T
5 P: Principal Amount
6 R: Rate of Interest
7 T: Time Period
8 */
9
10 float amount = 100f;
11 float rate = 0.06f; //Interest is 6%
12 int timePeriod = 2; //Assuming 2 years
13
14 float simpleInterest = amount * rate * timePeriod;
15
16 System. out.println("Interest: " + simpleInterest);
17 }
18 }
The program presents many interesting and hitherto unknown facets of Java. The first being the useof comments in the program to convey some meaningful information to the reader of the source code.The Java compiler and interpreter suitably ignore comments. There are two ways to write commentsin your program. Multi-line comments start with /* and end with */. Single line comments start with// and continue till the end of the present line.
The basic structure of our program remains the same. We define the title of our program on thefirst line followed by the main method. In here we define our three variables - amount, rate andtimePeriod . Notice that the data type for the first two is float signifying that these are floatingpoint numbers, i.e. numbers with a decimal point. Such numbers are written with a suffix of f
8/20/2019 A Primer on Java
16/59
Data Types and Variables 7
to their literal values. The timePeriod variable is declared as an int , which stores whole numberswithout any decimal point. Here we are assuming that this variable’s value can never be a fractionalnumber. Such decisions on which data type to pick for a computation variable are a regular part of a programmer’s job.
Our comments at the top specify the formula we are going to use to calculate the simple interest. Itis a good idea to note down such details along with the purpose of a block of code for readabilitypurposes. Since we are dealing with the multiplication of floating point numbers, it makes sense tostore the result also in a float. We multiply our three input variables using the multiply operator * .We then print out the result using println . Run this program using the same two step procedure weused in the last chapter and you should see the output as given below.
1 Interest: 12.0
You would notice that we used another operator, namely +. This concatenated the word ‘Interest:’with our result. However the same operator when used for numeric data types performs an addition.Thus we deduce that the + operator is context sensitive to the data type it is working upon.
8/20/2019 A Primer on Java
17/59
3 Introducing Classes & Objects3.1 Abstraction
A computer is a complex electronic machine. At its heart is a microprocessor that performs thecomputation. The data for a computation is composed of strings of 1s and 0s. Electronically thesestates of 1s and 0s are represented by current pulses. But to perform everyday tasks on a computerlike word processing or sending emails, you do not have to know the details of the entire machineor network. This hiding of unnecessary details is called abstraction.
Abstraction is a powerful programming concept too. The computer does not understand directlywhat you wish to do when you say something along the lines of System.out.println(). Thishas to be translated through a series of steps to instructions that would get you the output. Thusprogramming languages like Java are mediums of expressions providing us with the necessary levelof abstraction to solve a problem.
3.2 Defining classes and objects
In the previous chapter we saw data types and variables combining to give us the ability to holdliteral values. When you think about it, this is nothing but an abstraction of storing a value at a
memory location with the variable name being a way to access this memory location. The datatypes we saw were simple and are called primitive data types .
But what if the kind of data you wish to hold cannot be represented elegantly by the primitive types?For example a date can be thought of as a combination of 3 integer types, one each for day, monthand year. If programming is all about defining a series of abstractions and operations on data, it isimportant that our data be represented closer to its true meaning. Like a date being manipulated asa date rather than independantly as a set of 3 numbers. Such composite variables are called objects and their type is called a class .
We said previously that the type of a variable determines what operations are valid on it. Since withclasses we are creating new data types, we have to define a set of valid operations on them. A class
thus becomes a combination of data types and the operations that are applicable on this new datatype. An object becomes a variable of a class and is said to be an instance of its class.
The type variables which form the data being represented in the class are called the data members or member variables or simply members . In the date example, the three integers to represent day,month and year are the members of that class. The operations defined for a class are called itsmethods .
8/20/2019 A Primer on Java
18/59
Introducing Classes & Objects 9
3.3 Creating a class in Java
We will pick up the example discussed in the previous section as an example for creating our own
version of a date class in Java.
Listing: Our class for a simple date object
1 class MyDate {
2
3 // Member variables
4 int day;
5 int month;
6 int year;
7
8 // Methods
9
10 // Initializes members to some value
11 void initDate() {
12 day = 10;
13 month = 12;
14 year = 1815;
15 }
16
17 // Display the value stored
18 void displayDate() {
19 System. out.println( day + "/" + month + "/" + year);20 }
21 }
Save this file as MyDate.java . Notice that we have some member variables in there (day, month, year ) and two methods (initDate, displayDate) but no *main method. What this file (more appropriatelyclass) stores is attributes for a simple date data type and some simple functionality to set and displaythe date.
Note: The astute reader will no doubt be interested with the date chosen - 10th of December, 1815.
This happens to be the birthdate of Lady Ada of Lovelace. She is considered to be the world’s first
programmer. For more information read her biography ‘Ada, the Enchantress of Numbers’ by Betty
A Toole [1998, Strawberry Press].
8/20/2019 A Primer on Java
19/59
Introducing Classes & Objects 10
Since execution starts in the main method, we will create a separate file which will create an objectof the MyDate class and call its methods. Let us call this file DateRunner.java and the code for itis given below.
Listing: DateRunner class containing main
1 class DateRunner {
2 public static void main(String[] args) {
3
4 //Create a MyDate object
5 MyDate ada;
6 ada = new MyDate();
7
8 ada.initDate();
9 ada. displayDate();
10 }11 }
We will now compile both these source files using javac as shown below.
1 javac MyDate.java DateRunner.java
The next step is to run the application using the java interpreter. Remember that only the DateRunner class contains the main method. It is the responsibility of this class to invoke other classes by creatingtheir objects. Thus we run only the DateRunner through the interpreter to get our output.
1 java DateRunner
2
3 => 10/12/1815
3.4 Objects and references
In our previous example, we created a MyDate object called ada. The two line creation could havebeen shortened to MyDate ada = new MyDate(); but this would hide an important detail of theobject creation process.
When we write MyDate ada, we are not creating an object. We are creating a variable which canrefer or point to an object of the class type specified. The snippet ada = new MyDate(); actuallyallocates some memory space for the object and sends back an object reference as a result. This isassigned to the variable written on the left hand side, i.e ada.
When you assign an object to another object reference, you are not creating a new copy of the object.Instead you will have two references, both pointing to the same object. Changes done through onereference would reflect in the other.
8/20/2019 A Primer on Java
20/59
Introducing Classes & Objects 11
Listing: both references pointing to the same object
1 MyDate ada = new MyDate();
2 MyDate lovelace = ada;
8/20/2019 A Primer on Java
21/59
4 Conditional StatementsMaking decisions are a fundamental part of programming. There are numerous conditional con-structs available in Java to alter the flow of a program based upon a decision outcome you decideupon. To arrive at such an outcome, one has to first perform a comparison operation.
4.1 Comparison Operators
The comparison operators of Java are not that different from other programming languages you mayhave come across, or even elementary mathematics. For example, a greater-than operator is givena symbol of > and it checks if the value on the left hand side expression is greater than that of theright hand side. These operators return a true or false value. Thus it makes sense to store the resultof such a comparison as a boolean variable.
Refer to the table given below for a detailed listing of the comparison operators available.
Operator Description
> Returns true if the left operand is greater than the right operand
< Returns true if the right operand is greater than the left operand
== Returns true if the right operand is equal to the left operand
!= Returns true if the right operand is not equal to the left operand
>= Returns true if the left operand is greater than or equal to the rightoperand
8/20/2019 A Primer on Java
22/59
Conditional Statements 13
4.2 If-Else Conditional
A simple conditional construct in Java is an if-else statement. If a condition is met, the block
following the if is executed, otherwise the one following else is executed. A general syntax forthis construct is given below.
Figure: the general syntax of an if-else statement
1 if () {
2
3 }
4
5 else {
6
7 }
It should be noted that it is optional to have an else clause following an if . You can even omit thecurly braces signifying block boundaries if you have to only execute one statement if a condition ismet. However in the interest of readability, it is wise not to omit them. Let us see an example usingthe this conditional statement.
Listing: Using an if-else clause to change program output
1 int temperature = 102;
2 String state;
3
4 if (temperature < 0 ) {
5 state = "Solid Ice";
6 }
7 else if (temperature > 100) {
8 state = "Vapour";
9 }
10 else {
11 state = "Liquid Water";
12 }
13
14 System. out.println ( state);
In the example given above, we change our output to the state of water given its temperature indegrees celsius. Notice that we have also introduced an else if clause to determine whether thetemperature is sufficiently high enough to turn water to vapour. Feel free to modify the value of thetemperature variable to see the change in output.
8/20/2019 A Primer on Java
23/59
Conditional Statements 14
4.3 Switch Conditionals
A switch conditional is a multi-way selection statement. If your if-else clause is becoming too
unwieldy, it is a good idea to consider using a switch in its place. As an example, suppose we wishto simulate the operations on an ATM machine by allowing the inputs to be characters which arebound to particular action. Our specification is as given below.
D Deposit amount
W Withdraw amount
B Check balance
M Print a Mini-Statement
Turning this into an if-else construct becomes needlessly verbose.
Listing: our ATM action specification modelled using if-else
1 char choice;
2 ...
3
4 if (choice == 'D') {
5 System. out.println("Deposit an amount");
6 }
7 else if (choice == 'W') {
8 System. out.println("Withdraw an amount");
9 }
10 else if (choice == 'B') {
11 System. out.println("Check Balance");
12 }
13 else if (choice == 'M') {
14 System. out.println("Print a Mini Statement");
15 }
16 else {
17 System. out.println("Wrong choice");
18 }
We can use a switch here to make our code shorter without losing any details.
8/20/2019 A Primer on Java
24/59
Conditional Statements 15
Listing: our ATM action specification modelled using switch
1 char choice;
2 ...
3
4 switch (choice) {
5 case 'D': System. out.println("Deposit an amount");
6 break;
7 case 'W': System. out.println("Withdraw an amount");
8 break;
9 case 'B': System. out.println("Check Balance");
10 break;
11 case 'M': System. out.println("Deposit an amount");
12 break;
13 default : System. out.println("Wrong choice");14 }
Notice the use of the break statement after each case . This causes the execution to jump to theend of the switch . A break is optional, but if it is missing, execution continues to the following case statements till a break is enountered. The default statement executes if no match is encountered.
From the data types we have seen until now, a switch would typically operate upon characters andintegers . With the release of Java 7, it now supports String objects for comparison.
8/20/2019 A Primer on Java
25/59
5 Iteration Statements
An iteration or loop repeats a statement or a block of statements till the point an entry conditionis satisfied. It saves us from the tedium of writing similar statements again and again by givingus constructs that allow us to acheieve the same result in a compact manner. Java has three mainiteration constructs: while, do-while and for .
5.1 The while loop
The while loop is a simple iteration construct which first tests a boolean condition and if true,executes a block of statements. The general syntax of a while loop construct is given below.
Figure: general syntax of a while loop
1 while () {
2 // statements
3 }
First the boolean expression is evaluated. If it results in a false value, the execution jumps straightto the end of the block and executes the first statement after the block. However if the expressionevaluates to a true value, the body of the block is executed and then the expression is reevaluated.This cycle (of evaluation and then block execution) continues till the boolean expression remainstrue . Let us use this construct to find the sum of the first twenty(20) natural numbers.
Listing: a program to find the sum of the first 20 natural numbers using a while construct
1 public class WhileLoop {
2 public static void main(String[] args) {
3
4 int ctr = 1;
5 int sum = 0;
6
7 while(ctr
8/20/2019 A Primer on Java
26/59
Iteration Statements 17
Note the entry condition of the while loop and it’s execution block contents. We start our counterfrom 1 (the first natural number) and the block will be executed till the counter remains less than 20or equal to 20. In the execution block, we increment the counter with the ++ operator which adds 1to the value of the variable.
5.2 The do-while loop
This construct is similar to the while loop with one difference. The boolean expression which formsthe conditional part of the loop is written at the end, thus ensuring that the loop will execute atleastonce.
Figure: general syntax of a do-while loop
1 do {
2 // statements
3 } while ();
5.3 The for loop
A for loop consists of three parts - initialization, condition and step . Here the variable doingthe counting is initialized as a part of the construct itself. At the end of each iteration, stepping (increment/decrement) is done followed by checking of the condition.
Figure: general syntax of the for loop construct
1 for(; ; ) {
2 // statements
3 }
To understand this construct better, let us rewrite our previous example of finding the sum of thefirst 20 natural numbers using a for loop.
8/20/2019 A Primer on Java
27/59
Iteration Statements 18
Listing: usage of the for loop construct
1 public class ForLoop {
2 public static void main(String[] args) {
3
4 int sum = 0;
5
6 for(int ctr = 1; ctr
8/20/2019 A Primer on Java
28/59
Iteration Statements 19
11 continue;
12
13 sum = sum + ctr;
14 }
15
16 System. out.println("Odd sum: " + sum);
The objective of this program is a little different than the previous ones. It will print out the sumof only odd natural numbers below 20. Note that while this might not be the best way to achieveour objective, it does show a few interesting things. First, note how we used the while loop entrycondition. The use of true ensures that the loop will run forever unless we explicitly break out of it. This is exactly what we do if we see that our counter is equal to 20. Another thing to note hereis that an if conditional does not need curly braces for its block if there is only one statement toexecute. In this case, it is a break .
Since we are only concerned with odd numbers, we do a continue before we increment the sumvariable. This ensures that the next iteration starts but the even counter value is not included in thesum.
8/20/2019 A Primer on Java
29/59
6 Arrays
An array is a set of variables of the same data type. Let’s say you wish to store a the first 10 oddnumbers. To associate the list of these with variables you can either associate each one with its ownseparate variable name - oddNum1, oddNum2, oddNum3 and so on, or you can store them all in asingle array. Each of these numbers become an array element .
6.1 Array Initialization
Arrays can be declared using the indexing operator denoted as []. This is to be placed either after the
data type identifier of the array or its variable name. The following two declarations of an integerarray are equivalent.
1 int[] oddNums;
2 int oddNums[];
While the latter declaration might be familiar to C/C++ programmers, in this text we would adoptthe former style. I believe it is clearer in indicating the type of the variable as an array of integers.
The declarations that we saw above just associate the variable identifier with the type of an array. Itdoes not allocate any storage in memory for an array. To do that we write an array definition using
the new keyword.
1 int[] oddNums = new int[10];
When using the new keyword to declare an array, you must specify the number of elements in thearray by writing it within [ and ]. Each element of the array is given an initial value depending uponthe type of the array. For numeric types like in the example above, the value assigned is 0 (zero).
We can also initialize values for arrays at the time of creation using curly braces { }. Each value isplaced between these separated by a comma. To clarify this, let us look at an example below.
1 String[] designCommittee = { "Backus", "Bauer", "Bottenbruch", "Katz", "Perlis", \
2 "Rutishauser", "Samelson", "Wegstein" };
We initialize an array called designCommittee of type String . Though we have not discussed thistype yet, for the time being think of it as a data type holding words or a bunch of characters as asingle entity. Thus, our variable is a String type array with 8 elements as initialized. Note that wedid not have to use the new keyword.
8/20/2019 A Primer on Java
30/59
Arrays 21
For the curious, the names chosen as values for the designCommittee array are the members of
the Algol 58 programming language design committee. Their decisions impacted programming
languages for decades to come. Java itself is considered a descendant of the Algol family syntax
style.
6.2 Using Arrays
To store values at an array location we simply write the element number, also known as an index orsubscript in between the square brackets [] immediately following the array variable name. In Java,array indexes begin at 0 (zero). This means that the first element is stored at index 0, the second at
index 1 and so on. Let us look at an example to clarify our concepts regarding the usage of arrays.
Listing: using arrays to store odd numbers
1 public class OddNumbers {
2 public static void main (String[] args) {
3
4 // Initialize a 10 element array
5 int[] oddNums = new int[10];
6
7 // Will will go through the loop 10 times
8 for(int ctr = 0; ctr < oddNums.length; ctr++) {
9 oddNums[ctr] = (ctr * 2 ) + 1 ;
10 }
11
12 System. out.println("Last element: " + oddNums[ oddNums.length - 1]);
13 }
14 }
When you execute this program, you would see an output like Last element: 19 . So what actuallyhappened in the program? We declared an integer array of 10 elements called oddNums . The
expression .length provides you with the length of the array, which in this case wouldbe 10. Array indexes are integers, but you can specify expressions whose output is an integer too.
To find 10 (the length of our array) odd numbers, we initialize a counter to iterate with and initializeit to 0. The Nth odd number can be calculated simply by the formula,
Nth odd number = ((N - 1) x 2) + 1
8/20/2019 A Primer on Java
31/59
Arrays 22
Since array indexing starts at 0 which also is out initial counter (ctr) value, this formula can bereduced simply to
(ctr * 2) + 1
Finally we print out the last element of our array by calculating its index using oddNums.length - 1.
6.3 The enhanced for loop
The enhanced for or for-each loop was added in Java version 5, to iterate over arrays and othercomposite types which we will discuss later. The 3-part for loop, also called the C-style for loop, issomewhat verbose when you want to simply iterate over the array. Let us see an example of eachof these constructs and compare the difference in their syntactical clarity.
Listing: Using the C-style for loop
1 public class ForOld {
2 public static void main (String[] args) {
3 String[] designCommittee = { "Backus", "Bauer", "Bottenbruch", "Katz", "Perli\
4 s", "Rutishauser", "Samelson", "Wegstein" };
5
6 for (int ctr = 0; ctr < designCommittee.length; ctr++) {
7 System. out.println( designCommittee[ctr]);
8 }
9 }
10 }
The objective of the program is fairly simple. It creates a String type array and then iterates over it,printing out the element values. We initialize our counter variable to achieve this iteration, checkwhether our counter has not yet reached the end of the array (using length ) and increment thecounter in each iteration. The same can be achieved using the enhanced for loop as below.
8/20/2019 A Primer on Java
32/59
Arrays 23
Listing: using the enhanced for loop
1 public class ForNew {
2 public static void main (String[] args) {
3 String[] designCommittee = { "Backus", "Bauer", "Bottenbruch", "Katz", "Perli\
4 s", "Rutishauser", "Samelson", "Wegstein" };
5
6 for (String committeeMember : designCommittee) {
7 System. out.println(committeeMember);
8 }
9 }
10 }
Note the structure of the for loop in this case. Our iteration loop has been reduced to the generalstructure:
for ( : )
We can then count on using the variable identifier *(committeeMember) inside the loop blockwithout using array subscripts.
8/20/2019 A Primer on Java
33/59
7 MethodsIn chapter 3, we were introduced to classes and objects. But we haven’t been truly doing object oriented programming . We will now begin to explore in earnest, objects and their fundamentalbuilding blocks - methods.
Recall that a method is an operation of a class. The concept is closely related to a procedure or function which is a block of statements performing a task. A method however is always declaredinside a class. The general syntax of a method is given below.
Figure: general syntax of a method
1 AccessModifier ReturnType MethodName ( optional parameter list) {
2 . . .
3 }
The access modifier controls the visibility of a method, giving the programmer control over whereall a method can be called from. We will be going over this concept a bit later. The return type isthe data type of the return value. Think of it as a result or output of the computation done in themethod. The inputs to the method are its parameters . The method name along with it’s parameterlist is called the method signature.
7.1 Parameters & Return Values
Let us begin by rewriting our simple interest calculation as a separate method, rather than puttingeverything in main .
Listing: the class for calculating simple interest on an amount
1 public class SimpleInterest {
2
3 // Member variables
4 float amount;
5 float rate;
6 int timePeriod;
7
8 // Methods
9 public void initValues (float principal, float rateOfInterest, int numOfYears\
10 ) {
8/20/2019 A Primer on Java
34/59
Methods 25
11 this. amount = principal;
12 this.rate = rateOfInterest;
13 this.timePeriod = numOfYears;
14 }
15
16 public float calcInterest() {
17 float simpleInterest = amount * rate * timePeriod;
18 return simpleInterest;
19 }
20 }
We create a class called SimpleInterest which does not contain the main method. But what it doescontain are methods to initialize values needed to calculate the simple interest and the calculationmethod. We intend to create a separate runner class which would create an object of SimpleInterest
and then call its methods.
Listing: the runner class containing main
1 public class InterestCalculator {
2 public static void main(String[] args) {
3 SimpleInterest applicant = new SimpleInterest();
4 applicant.initValues(100f, 0.06f, 2);
5
6 float amountReturned = 100f + applicant.calcInterest();
7 System. out.println("Amount after time period: " + amountReturned);
8 }
9 }
We create an object of the class SimpleInterest and initialize its member values by passingarguments in the method call - initValues . These arguments of the method call pass their valuesonto the parameters in the method signature.
We also see that our method calcInterest returns a float value. Thus in the method definition weexplicitly state that it will do so, in contrast to initValues which does not return anything. Recallfrom the general syntax of a method definition that the second modifier tells the type of value a
method returns. A method that has only side effects without a return value, is declared as void .Inside a method definition, we use this as to refer to the calling object. It is commonly used to accessthe member variables of a class. In our initValues method, we used this to differentiate between theparameters and the instance (member) variables. While the names of the two kinds of variables aredifferent in this case, they can have the same name, making the use of this essential.
8/20/2019 A Primer on Java
35/59
Methods 26
7.2 Constructors
A constructor is a special method that is called when an object is created using new . It has the same
name as the class it is contained in and does not have a return type. The purpose of a constructor isto perform some initializations like setting values of some instance variables.
When we do not write a constructor, as we haven’t until now, Java automatically creates a noargument constructor which is invoked when an object is created. In our previous example, considerthe SimpleInterest class with the initValues method. We can replace this explicit call to a methodby creating our own constructor. This makes sense because the purpose of initValues is close to thecharter of responsibilities of a constructor.
Listing: the SimpleInterest constructor
1 public SimpleInterest (float principal, float rateOfInterest, int numOfYears) {
2 this. amount = principal;3 this.rate = rateOfInterest;
4 this.timePeriod = numOfYears;
5 }
Our runner class InterestCalculator still makes a reference to a no-argument constructor and theinitValues method. We modify both these lines by removing the line calling initValues altogetherand changing the object creation line to the following.
1 SimpleInterest applicant = new SimpleInterest(100f, 0.06f, 2);
Our program now compiles and runs as before.
8/20/2019 A Primer on Java
36/59
8 StringsA string is a collection of character in Java. For example, Hello, World! is a string, so is “7 + 2 = 11” regardless of the validity of the latter. While characters are represented by the char primitive datatype, String is a class supplied with Java which allows you to deal with strings. You can effectivelyuse this class to create variables for your text data. Let us modify the Hello, World! program we sawin the very first chapter to use a String variable.
Listing: using String variables
1 public class Hello {
2 public static void main(String args[]) {3 String addressee = "Alan!";
4 System. out.println("Hello, " + addressee);
5 }
6 }
We created a String variable called addressee and assigned it an initial value. We then used theSystem.out.println function to display our intended line. Note that we combined a constant stringwith our variable value. This operation is called concatenation and it is represented by the +operator.
8.1 String Methods
A string variable in Java is not a primitive variable like an int or char . It is an object of type String and has methods associated with it just like any other object. One of the commonly used methods iscalled length and this returns the length of the string as an integer value. Let’s modify the previousprogram to include the length method by adding the following lines to the source code.
1 int n = addressee.length();
2 System. out.println("Length of variable: " + n + " characters");
8/20/2019 A Primer on Java
37/59
Strings 28
Figure: output of the modified program to illustrate string length method
1 Hello, Alan!
2 Length of variable: 5 characters
The length method expectedly gave us the result as 5. This means that it included the exclamationmark ! character too. In fact length accounts for all kinds of special symbols including spaces, not just alphabets.
Some of the other methods available for String rely on the position of characters in the string. Thefirst character is given the position number of 0, the second 1 and so on. This means that the lengthof a string is the position of the last character + 1. The position of a character is formally known asits index. Let us explore another method that relies on the index in a string - substring .
1 String sentence = "Alan Turing is considered the father of computing.";
2 System. out.println( sentence. substring(5));
Figure: output of the substring method
1 Turing is considered the father of computing.
The substring method takes an index as a parameter and returns a substring starting from that indexto the end of the string. The index 5 value means the 6th character, i.e. T . You can additionally give
two index values, one for the starting position index and one for the end position. The substringmethod in this case would return you a smaller string starting from the first parameter value indexto the character just before the second.
1 System. out.println( sentence. substring(0,11));
Figure: output of the substring method with two parameters
1 Alan Turing
8.2 Escape Characters
Suppose we wish to print a string like the one given below exactly as it is shown. Do you notice anyirregularities that would cause us problems when we try to System.out.println it?
8/20/2019 A Primer on Java
38/59
Strings 29
1 Simplicity does not precede complexity, but follows it.
2 - Alan Perlis, "Epigrams on Programming, 1982"
A few things that are special in this output is that firstly it is in two lines. The second line is alsoindented by a tab character (4 or 8 spaces) and it contains double quotation marks as a part of theoutput itself. To satisfy these requirements, we must use escape characters or escape sequences which start with a backslash to denote themselves. Below a small table that lists which escapecharacter results in what output.
\n Newline
\t Tab character
” Double quotes
|Backslash
’ Single quote
Using this table we can now work out the System.out.println line that would give the desiredoutput.
1 System.out.println("Simplicity does not precede complexity, but follows it.\n\t- \
2 Alan Perlis, \"Epigrams on Programming, 1982\"");
8.3 Comparing Strings
Strings are not compared by the usual == operator we use for other primitive types like int . Recall
that String is actually a class in Java. If we have two String objects, using the == operator wouldreturn us a true value only if the two variables refer to the same object. It is possible to have twodifferent String objects having the same string content, but in this case == would return false.
Listing: using == on String objects
1 String s1 = new String("Calculus");
2 String s2 = new String("Calculus");
3 String s3 = s1;
4
5 System. out.println( s1 == s2);
6 System. out.println( s1 == s3);
8/20/2019 A Primer on Java
39/59
Strings 30
Figure: output of using == on String objects
1 false
2 true
You can see from the output above that two different String objects return false when we use the== operator, even if their string values are the same. The String class provides us with a method -equals which compares string values contained in the object and return true if they match.
1 System.out.println(s1.equals(s2));
2
3 > true
8/20/2019 A Primer on Java
40/59
9 Inheritance
Inheritance is the process by which we model entities in our problem domain as a hierarchy to leadus to a more conceptually elegant solution structure. This definition is a bit heavy, so we attempt tosimplify it. Think of it as creating parent and child classes where it makes sense to do so.
Consider a class called Employee . A company may wish to differentiate between hourly wageemployees and full time employees. The solution to model this hierarchy is two make two childclasses of Employee , namely HourlyEmployee and FullTimeEmployee . The common behavior (likeall employees have a name) between them can be shared by defining it in the Employee class. Thisis called the base class . The other two classes which inherit the properties and behaviors from thebase class are called the derived classes .
9.1 Extending a class
To create a subclass from an existing class, we use the extends keyword in Java. We can add ormodify properties and behavior of existing classes, which keeps redundancy to a minimum.
Figure: general syntax for extending a class
1 public class extends {
2 . . .
3 }
Keep in mind that while a class can have multiple child classes, a class cannot have multiple parentsin Java. It is however possible for a child class to act as a parent of another class. Let us now see anexample of inheritance in Java.
Listing: the base class - Interest
1 public class Interest {
2
3 // Member variables
4 float amount;
5 float rate;
6 int timePeriod;
7
8 public float calcInterest() {
9 return 1.0f;
10 }
11 }
8/20/2019 A Primer on Java
41/59
Inheritance 32
Listing: the derived class - SimpleInterest
1 public class SimpleInterest extends Interest {
2
3 // Methods
4 public SimpleInterest (float principal, float rateOfInterest, int numOfYears)\
5 {
6 this. amount = principal;
7 this.rate = rateOfInterest;
8 this.timePeriod = numOfYears;
9 }
10
11 public float calcInterest() {
12 float simpleInterest = amount * rate * timePeriod;
13 return simpleInterest;14 }
15 }
Note that in SimpleInterest , we did not redefine the member variables but used them directly in ourconstructor for SimpleInterest . We did however, create a more specialized version of the calcInterest() method in the subclass. Our task now is to create a suitable runner class containing the main methodto test our classes.
Listing: calculating the simple interest using the derived class
1 public class InterestCalculator {
2 public static void main(String[] args) {
3 SimpleInterest applicant = new SimpleInterest(100f, 0.06f, 2);
4
5 float amountReturned = 100f + applicant.calcInterest();
6 System. out.println("Amount after time period: " + amountReturned);
7 }
8 }
A useful feature given to us is that while creating an object we can specify its type as its parentclass. This makes sense because the object, regardless of which child class it instantiates, wouldatleast have the behavior (think method) of its parent. Thus while creating the applicant object, wecan use the statement below.
1 Interest applicant = new SimpleInterest(100f, 0.06f, 2);
8/20/2019 A Primer on Java
42/59
Inheritance 33
9.2 Access Specifiers
An access specifier controls the access and visibility of its member. The member here means a class
member field or a class method. There are three access specifiers available in Java, namely public, private and protected . You have already used the public access specifier for the classes you havebeen writing so far.
A member designated as private can only be accessed by a method of its own class. A public memberhowever can be accessed by any member of any class. A good general rule is to mark your membervariables private and methods public unless the method is only meant to be used internally by othermethods of the class. Good programming principles encourage us to use methods to read and updatemember variables through public methods called getters and setters respectively. This allows usthe flexibility to impose sensible restrictions on the values that a member can take.
Listing: getter and setter for principal
1 private float principal;
2
3 public float getPrincipal () {
4 return principal;
5 }
6
7 public void setPrincipal (float deposit) {
8 if ( deposit < 0 ) {
9 System. out.println("No negatives allowed");
10 System. exit(0);
11 }
12 else {
13 principal = deposit;
14 }
15 }
With respect to inheritance, access specifiers play a special part. Private members are not interitedwhereas public members are inherited.
9.3 The super Keyword
We have seen creating methods with the same name in the inherited subclasses, effectivelyoverriding the super class version of it. The super keyword allows us to invoke the methods of the super class in the subclass. This is useful when the method in the derived class has just someadditions to make to the existing computation in the parent class method.
8/20/2019 A Primer on Java
43/59
Inheritance 34
Listing: Using the super keyword to refer to the superclass members
1 class Cylinder {
2 protected float radius;
3 private float height;4
5 public Cylinder (float r, float h) {
6 this.radius = r;
7 this.height = h;
8 }
9
10 public float surfaceArea() {
11 float sa = 2 * 3.14159f * radius * height;
12 return sa;
13 }
14 }
15
16 class Can extends Cylinder {
17 public Can (float r, float h) {
18 super(r, h);
19 }
20
21 public float surfaceArea() {
22 float sa = super. surfaceArea() + (2 * 3.14159f * radius * radius);
23 return sa;
24 }25 }
26
27 public class SurfaceAreaRunner {
28 public static void main(String[] args) {
29 Can myCan = new Can(2.0f, 10.0f);
30 System. out.println("Surface Area: " + myCan. surfaceArea());
31 }
32 }
The example above makes a Can class as a derived class of Cylinder . A can, we assume is a cylindercovered with both sides. It’s surface area thus being the area of its cylinder and twice the area of any one of its (two) covers.
We use the super keyword twice here. Once when we calculate the surface area of the can, wherewe add the surface area of its covers to the already defined surface area of its cylinder. The otheris when we call the super class constructor using simply super() . Though we did not add anythingin the derived class constructor we could have kept some checks (like radius and height cannot
8/20/2019 A Primer on Java
44/59
Inheritance 35
be negative) and then called the super class constructor. Another point to note is the use of accessspecifiers for radius and height . The radius is given a specifier protected so that it behaves likea private member with one exception, that it is accessible to subclasses. We need it in this casebecause the subclass would calculate the surface area using it. The height on the other hand can
remain private since the subclass does not need it for it’s computations.
9.4 The Object class
There is a class in the Java language which is a parent class to all other classes - Object. It containsa handful of methods which all other classes inherit and can override if they feel the need. One suchmethod is the toString() which can be thought of as a method to display the textual representationof an object. Let us add a single line to our SurfaceAreaRunner program to see this method in action.
1 System.out.println(myCan.toString());2
3 > Can @14893 da
The output is a string containing the name of the class of the object - Can in this case, followed bya @ character which in turn is followed by the memory address where the object is stored.
Note that this address will be different from computer to computer and even between
subsequent executions on the same computer. So don’t be alarmed if it does not match the
output here.
While this is a moderately helpful output, let us write our own toString() method for all kinds of cylindrical objects. To achieve this, we write a ToString() method in the Cylinder class which Can inherits from.
Listing: our modified Cylinder class containing a custom toString() method
1 class Cylinder {
2 protected float radius;
3 private float height;
4
5 public Cylinder (float r, float h) {
6 this.radius = r;
7 this.height = h;
8 }
9
10 public float surfaceArea() {
11 float sa = 2 * 3.14159f * radius * height;
8/20/2019 A Primer on Java
45/59
Inheritance 36
12 return sa;
13 }
14
15 public String toString() {
16 return "Cylindrical object of radius " + radius + " and height " + height;
17 }
18 }
If we call the toString() method on the Can object, the last override of the method will come intoplay. Since the immediate parent of Can is Cylinder , the toString() method of Cylinder will be called,effectively overriding the default method implementation from the Object class.
1 System.out.println(myCan.toString());
23 > Cylindrical object of radius 2.0 and height 10.0
Overriding toString() is a good thing
Whenever possible, it is a good idea to override toString() and provide a better, more
descriptive version than the default. Providing a sensible toString() would help you narrow
down on bugs.
8/20/2019 A Primer on Java
46/59
10 Abstract Classes and Interfaces10.1 Abstract Classes
While making inheritance hierarchies, we sometimes come across a case where it does not makesense to allow a superclass to be instantiated, i.e. have objects of their type. For example we shouldnot allow instantiation of a superclass called SpaceCraft but derived classes like Starship or Battlestar should allow object creation. Without fleshing out the details of a space ship, which in this case isonly possible in the child classes, it makes little sense to have a concrete implementation for it.
Such classes are termed as abstract classes and are useful only to serve as a template or contract
by which all its derived classes must adhere to. Their utility is derived by their extension. Since theyserve as blueprints, these classes must have at least one abstract method , which are simply methodswithout any implementation details. They only contain the method name and their argument list.
We declare a class or method to be abstract by prepending the keyword abstract to the method orclass name.
Listing: an abstract class containing an abstract method
1 abstract class SpaceShip {
2 void turnOnIgnition {
3 // Some code
4 }
5
6 abstract void takeEvasiveManeuver(String direction);
7 }
The abstract class SpaceShip contains a concrete method - turnOnIgnition() . This means thatthe implementation of this method is also fleshed out in this class unlike the abstract method -takeEvasiveManeuver() . Whether we override the concrete method is upto us, but we must give aimplementation to the abstract method in our subclass.
10.2 Interfaces
An interface is like an abstract class containing only abstract methods. It’s purpose is to serveas a pure blueprint for other classes and contains no implementation details. Classes implement aninterface(s) rather than extending it. We declare an interface using the interface keyword and listingthe method signatures with no implementations.
8/20/2019 A Primer on Java
47/59
Abstract Classes and Interfaces 38
List: creating the Interest interface
1 interface Interest {
2 float getFinalAmount(float principal);
3 void calcInterest ();
4 }
We can now create a class SimpleInterest which will implement the Interest interface, meaning itwill provide the implementation of both the methods listed in the interface.
Listing: implementing the Interest interface
1 class SimpleInterest implements Interest {
2
3 // Member variables4 float amount;
5 float rate;
6 int timePeriod;
7 float simpleInterest;
8
9 // Methods
10 public SimpleInterest (float principal, float rateOfInterest, int numOfYears)\
11 {
12 this. amount = principal;
13 this.rate = rateOfInterest;
14 this.timePeriod = numOfYears;
15 }
16
17 public void calcInterest() {
18 this. simpleInterest = amount * rate * timePeriod;
19 }
20
21 public float getFinalAmount(float principal) {
22 float finalAmount = principal + this. simpleInterest;
23 return finalAmount;
24 }25 }
We can now create an object of the type Interface to instantiate SimpleInterest because the latterimplements the former and it would satisfy the contract of its parent, just like in parent classes.
8/20/2019 A Primer on Java
48/59
Abstract Classes and Interfaces 39
Listing: using the implemented interface as a type
1 public class InterestRunner {
2 public static void main(String[] args) {
3 Interest savings = new SimpleInterest (100f, 0.08f, 2);
4 savings.calcInterest();
5 System. out.println( savings. getFinalAmount(100f));
6 }
7 }
10.3 Multiple Inheritance and Interfaces
Multiple inheritance occurs when a derived class inherits its members from two or more super
classes. It can potentially lead to ambiguity when the derived class is supposed to inherit a behaviordefined differently in both its super classes. Whose behavior would get preference? The designers of Java thought that multiple inheritance was more trouble than its worth and thus decided to removeit from the language.
They did this by allowing a class to extend one and only one super class. But if you wanted to adhereto multiple contracts, how would you achieve this? By interfaces . While you are allowed to derivefrom only one parent class, even if it is abstract, you can implement as many interfaces as you want.
Listing: Implementing multiple interfaces
1 class TravelGuide extends Guide implements HotelList, RestaurantList {
2 . . .
3 }
8/20/2019 A Primer on Java
49/59
11 Exception HandlingError handling is an important part of programming. Errors can be generated at compile time, likewhen you invoke javac , or at run time like when you try diving by zero. In Java, an abnormalcondition at run time is called an exception . Syntactical errors like missing a semicolon are caughtat compile time by the Java compiler and it does not let you proceed further till you fix those errors.Run time exceptions are usually logical in nature.
We make our programs reliable by writing exception handling code. The Java compiler enforcescertain exception handling. Also we do not need to write the handling part exactly where the erroroccurred, but this can be separated out to ensure code flow readability. For this we need to be able
to throw our exceptions to the handler.
11.1 The try-catch block
A try-catch block enables exception handling. Any code that may throw an exception is placedinside the try block and the catch takes remedial action if possible. Consider a trivial divisionprogram as given below.
Listing: a simple exception generating program
1 class ExceptionalCalc {2 public static void main (String[] args) {
3 int numerator = 10;
4 int denominator = 0;
5
6 int result = numerator / denominator;
7 System. out.println(result);
8 }
9 }
Even before we run this program, our mathematical senses tell us that unless the computer (ormore specifically, the Java interpreter) can represent infinity on a monitor, we should be seeing anexception.
8/20/2019 A Primer on Java
50/59
Exception Handling 41
Figure: output showing the divide by zero exception
1 Exception in thread "main" java.lang.ArithmeticException: / by zero
2 at ExceptionalCalc.main(ExceptionalCalc.java:6)
We now introduce a try-catch block in this program to gracefully handle this exception. Note thatit is the calculation of the result that throws the exception, so it makes sense to put it in a try block.We should also keep a note of the type of exception thrown, in this case being ArithmeticException ,so that we can introduce it in the catch type.
Listing: the same program with exception handling added
1 class ExceptionalCalc {
2 public static void main (String[] args) {
3 int numerator = 10;4 int denominator = 0;
5
6 try {
7 int result = numerator / denominator;
8 System. out.println(result);
9 }
10 catch ( ArithmeticException e) {
11 System. out.println("I have caught an exception");
12 }
13 }
14 }
When we run this program, instead of a (not so) cryptic error message, we get the string we printed inour catch clause. Here we only gave a simple output indicating to the user that we saw an exceptionbeing raised. However in some cases it is possible to take remedial action provided we understandthe exception raised and how to handle it.
11.2 Java Exception Classes
You might have noticed that we gave the type of exception being handled as an input to the catch clause. Are there other exception type? Yes, in fact Exception is a class type and there are manyspecialized derived class types from it. Let’s see what happens when we change the type of exceptioncaught to this superclass.
8/20/2019 A Primer on Java
51/59
Exception Handling 42
1 catch (Exception e) {
2 . . .
3 }
When we run this source code, we get the same output as before. This is in line with our thinking thatthe ArithmeticException type is nothing but a specialisation of the super type Exception . A generalrule of thumb however, is to use as specific an exception type as possible. You don’t want to takeremedial actions thinking something went wrong with a calculation when the arguments suppliedare less than expected. We can even give multiple catch clauses for a single try block to be able tohandle multiple kinds of exceptions.
11.3 Throwing an exception
If you carefully look at out example program, you might notice that we could do away with ourspecial syntax for exception handling and use an if-else clause.
Listing: usign the if-else for exception handling
1 class ExceptionalCalc {
2 public static void main (String[] args) {
3 int numerator = 10;
4 int denominator = 0;
5
6 if ( denominator == 0) {
7 System. out.println("The denominator should not be 0"); 8 }
9 else {
10 int result = numerator / denominator;
11 System. out.println(result);
12 }
13 }
14 }
The purpose of adding special exception handling mechanisms in Java was to separate out the
program logic from the remedial steps to be taken in an exceptional circumstance. While the try block in our first program contained only one extra instruction (print the result) after the statementthat might cause an exception, it is better to separate out such an exception causing instruction fromthe normal flow of a program. This can be achieved using a throw statement.
8/20/2019 A Primer on Java
52/59
Exception Handling 43
Listing: using the throw statement
1 class ExceptionalCalc {
2 public static void main (String[] args) {
3 int numerator = 10;
4 int denominator = 0;
5
6 try {
7 if ( denominator == 0)
8 throw new ArithmeticException("The denominator should not be 0");
9
10 int result = numerator / denominator;
11 System. out.println(result);
12 }
13 catch ( ArithmeticException e) {14 System. out.println("I have caught an exception");
15 System. out.println( e. getMessage());
16 }
17 }
18 }
You will notice that we are explicity stating the condition where an exceptional circumstance mayoccur, and then throwing a new exception object of type ArithmeticException . We are also passinga message when creating the exception and this is retrieved later in the catch block using the
getMessage() method.
11.4 The finally statement
You will frequently see or create code that will have multiple catch clauses and some statementsfollowing that. But what if there are a particular set of statements you always wish to execute. Anyfollowing statements after the catch clauses may not run if you haven’t caught the particular typeof exception thrown. But if there is some critical operation you must perform before you abort outof your program? This is where the finally statement comes in.
A finally statement defines a block of code that will execute after a try , regardless of how theexception handling went. A good example of what should go in a finally block is closing of anyopen files.
8/20/2019 A Primer on Java
53/59
Exception Handling 44
Listing: the general syntax of a finally statement
1 try {
2 // Some vulnerable code
3 } catch (Exception1 e) {
4 // How to handle Exception1
5 } catch (Exception2 e) {
6 // How to handle Exception2
7 } finally {
8 // This will always execute
9 }
8/20/2019 A Primer on Java
54/59
12 Packages in Java12.1 What is a package?
A package is a collection of related classes. Packages are created to organize code by its purposeand to guarantee uniqueness of class names. Think of a package as a directory or folder hierarchywhere classes are stored. If a class called Interest is stored in a directory structure bank/savings/ , thepackage for the class is bank.savings.
When multiple programmers are working on the same project creating a bunch of classes, itmakes sense to organize these by using packages. This not only separates the physical directorylocation of these classes, it also makes the classes unique even if the names of two classes are thesame. The class Interest in the package bank.savings has nothing to do with the class Interest inbank.fixeddeposit. Thus we are free to choose the most sensible name for our classes as long asthe combination of the package name + class name is unique.
12.2 Packaging a class
We can assign a package to a class by a simple two step process.
1. Place the class source file, i.e. the .java file, in the package hierarchy. If you want to assigna package bank.savings to Interest - place Interest.java inside the directory structurebank/savings .
2. On top of the Interest.java file, write the line package bank.savings;
These steps would ensure that the compiler knows that this Interest.java belongs to the bank.savingspackage.
The Object class we mentioned in the chapter on Inheritance resides in the package
java.lang. If you have access to the JDK source code (usually a src directory or src.zip
file in the JDK installation folder), you can go to the java\lang\ directory and find Object.java in there.
Note that package names are case significant. This means a package called bank.savings is differentfrom the package Bank.Savings. If you think about it, this makes sense to achieve consistency acrossvarious operating systems. Microsoft Windows is case insensitive with respect to directory or filenames, but Unix and its derivatives are not.
8/20/2019 A Primer on Java
55/59
Packages in Java 46
What happens when you do not specify a package statement?
If you do not write the package statement at the top of your class file (like you did not
before this chapter), the classes are put in the default package which has no name. If you
are learning Java, this is acceptable. But once you move to programming larger applications,
it is expected that you always use explicit package names.
12.3 The CLASSPATH variable
You might be wondering how the Java compiler knows where to look for the bank directory tofind the classes contained in the bank.savings package. The answer lies in an operating systemenvironment variable called CLASSPATH.
An enviroment variable is a variable available to all resources that share an environment. Anenvironment variable you set is available to all programs you launch. A system environment
variable is available to all programs throughout the system regardless of the user.
Setting the CLASSPATH environment variable is specific to your operating system. On a MicrosoftWindows machine for example, you can set it from the Control Panel -> Advanced system settings -> Advanced (tab) -> Environment Variables . Separate the directories you wish to specify with asemicolon (;). A typical CLASSPATH value might look like below.
Figure: A Windows CLASSPATH value with 2 directories specified 1 C:\ experiments\java;C:\ stable_utilities
There are two other ways that Java looks for packages. It searches the current directory for packages.Another option is to set the clas