Fundamentals of Java Programming

Welcome to journey of "experiential learning" on Java Fundamentals. In this journey you would learn through your own experience. This method of learning is  specifically defined as "learning through reflection on doing".

So even if you have no prior computer programming experience, if you follow the instructions with full faith you would be writing Java programs even before you hit 20% progress mark of this course.

To give an analogy, this tutorial is like learning to ride a bicycle without side-wheels. You will fall, bruise yourself, but you will learn faster than any other kid in your block. And your skill of negotiating the bumps, potholes, puddles will be far superior than the rest.  

Speed is of essence here - so to make the most of this course, try to complete the modules without any break and follow the instructions, including   the coding tasks mentioned in the modules. If you follow these with full faith, it is guaranteed that your confidence level will soar and you will be able to express your thoughts and ideas through Java programs as naturally as you speak American.


Java Run-time Environment and Java Compiler installed in your computer. Please take help from IT Support or a friend who has successfully compiled his/her first Java code.

A Word of Caution

This course is not a substitute for your Java Fundamentals Text Book. You need to refer to the text book for in depth and complete understanding of the programming constructs and the philosophy behind Java language.

However, text books are often dry and at times you get lost in the theoretical details. Whereas this course is interactive and streamlined. So you may use this course before, during or after studying the text book.  On each occasion, the outcome will be different.

If you do this course first, you will have to assume some of the constructs as they are generally followed. When you study the text book later, you will be able to connect the constructs with the reasons behind them.

If you study text book and do the course simultaneously, it will slow you down and this is not recommended.

If you take the course after you have completed studying the text book, you would be able to match the theory with practice and test your understanding.

My recommendation is take this course before you study the text book and repeat the course (at a faster pace) after you have completed studying the text book.

Are you ready for a vertical take off ? Vertical Lift Off

Understanding the Business Problem before solving Programatically

Jen's Pizza Delivery Startup

Jen's Pizza Delivery

The University Cafeteria closes at 7:00 p.m.

No Pizza store delivers to the campus after 7:00 p.m.

However, there is a huge demand for pizza in the residential dorms between 9:00 p.m. and 10 p.m.

Jen spotted this opportunity and tied up with a off campus pizza store to deliver a bulk order at 9:00 p.m. and deliver individual boxes to the dorms as per the requests from her fellow residents.

There are 10 dorms in the campus and each dorm has 4 wings(E, W, N, S) and 10 rooms in each wing. There are 400 rooms in total. The dorm rooms are numbered as D1E1 - read as Dorm 1 East Wing Room#1. So the 10th room in West wing in Dorm 10 us numbered as D10W10. 

She collects a 10% fee on top of the pizza cost for delivering it to the Dorm Rooms of fellow students. 

Initially there were 5-6 orders per day and she was managing with pen and paper.

Of late more than 20 students are ordering and Jen needs a computer program to keep records of the pizza orders and deliver them correctly.  

Which problems does Jen want to solve through this program?

1. Take orders from fellow students and record them.

2. Put Labels on the individual boxes before shipping to the correct dorm room.


1. It is an ideal world, every one speaks the truth and does not make any mistake while ordering/receiving pizza.

2. The price of all boxes of pizza is same and there is no variety in the type of pizza. So any box can be delivered to any resident without any other consideration.

Are you ready to write this program?

Your First Java Program: A simple program which prints the name of your business - "Jen's 9 p.m. Pizza Delivery"

Source Code for Simple Java program 

// This is a simple Java program.

public class Pizza_Delivery{

        public static void main(String args[]){

              System.out.println("Welcome to Jen's 9 p.m. Pizza Delivery");

              return ;



Try to follow Step 1, 2 and 3 and watch your first Java program run 

If you manage to get it right the first time, either it is the luckiest day of your life or you are brighter than the rest of your cohorts.   

If you don't, its perfectly normal. Watch the video and do some web search to identify the problems and fix them. 

Finally when you see the desired outputs it is "Pure Joy" 

Print a Flier for your Pizza Deliver business

Promoting your Pizza Delivery Business in Campus

Now lets see if we can modify the program to print a Flier promoting your business.

List down things to do:

1. <Your Name>'s Pizza Delivery.

2. Put your Contact Details - Phone Number, Email ID and Dorm No/Room No.

3. Print a line of decorative characters before and after the Information. 

For Example:


*    <Your Name>'s 9 p.m. Pizza Delivery             *

*   Call : <Your Number>                                             *

*   Email: <Your Email-ID>                                         *

*   Address:  < Your Dorm No/ Room No>             *


How to Do?

Let's start by examining the source code line by line

Line#1 : 

// This is a simple Java program.

This is a "comment line".  Go ahead and change the comment to  "// This is a really complex Java program" and see if the output changes when you compile the saved file and execute the bytecode.

Line#2 &3  and Line#9 :

public class Pizza_Delivery{ 


For the time being, take this as a rule for Java programming. There could be many classes (basic building block for Java programming language which we will examine in details later) Java file but a single public class. And that class name must be same as that of the Java file. A 'class' must be defined within braces - Opening brace '{' and closing brace '}'.

Line#4&5  & 8:

public static void main(String args[]){


You can see a lot of keywords here like public, static, void and an argument list  which defines the main method. We will go through the meanings of these keywords in due course. For the time being lets use this construct of the main method to encapsulate the code statements which we want our program to execute.  

Finally, Line#6 &7:

System.out.println("Welcome to Jen's 9 p.m. Pizza Delivery");

return ;

The statement System.out.println("<Any String>") prints <Any String> to the System's Output i.e. screen or console window or the command prompt window when you execute the program.  You can put a number of System.out.println statements to achieve your objective of printing the Business Card for your 9 p.m. Pizza Delivery Startup. Finally, return is an instruction to the processing unit that it is a logical end of the program and   control is returned back to the processing unit for any other method or program to run.

Learning Variables and Literals and Getting ready to take orders through a Java Program

Introducing programming with variables

The first program we wrote was printing the same information every time you executed. Computer programs would not be useful unless they can handle variety in the inputs, process them according to the logic coded in the program and present the intended output. To achieve this you need to use 'variables' in your program.   

A variable is formally defined as a "named storage location in the computer's memory".  You need to read this definition in the context of a running program (i.e. a program in execution). 

When you have defined a variable in your program, a named storage location in the computer memory is allocated. This memory location is used to store value of the variable.   

Now, the program logic will use this 'variable' to compute other expressions, modify the value of this variable till the intended result is achieved.

Although we have spoken about a single 'variable' - usually a program would consist of multiple variables. Each variable aims to achieve a specific purpose. Then you have to construct the program logic so that the variables and the program logic work together to meet the program objective. 

Is this getting too Technical?  Lets take a few steps back to our High School Algebra lessons.

Similarity between programming with variables and solving algebraic equations

A variable in a program is like the proverbial 'x' in Algebra. It is a symbol which can hold different values.

When you used to solve for 'x' in an algebraic equation, you used to write the expression involving 'x'.

Then you would mentally simplify the expression, write down the simplified expression.

Repeat the step till you reach the final value of 'x'.

Programming with variables is quite similar - the difference being that instead of you, the computer executes the steps. 

Wow!  Isn't that cool?

But how does the computer know how to solve the variable.

Here comes your role as a programmer.

You need to provide all the steps needed to solve the expression in your program, so that the computer can execute those steps and present the result.

Its time to take a practical demonstration of the concepts discussed so far.  So we go back to solving the Order Capture Problem for your 9 p.m. Pizza Delivery.

What information I need to capture when some one rings for a Pizza delivery?

  • I have to capture name of the Student and the room where Pizza has to be delivered.
  • There are 10 dorms in the campus - D1 to D10.
  • Each dorm has 4 wings(E, W, N, S) - D1-E to D10-S.
  • Each wing has 10 rooms - D1-E-1 to D10-S-10.
  • There are 400 rooms in total. 

When Someone rings for Pizza I need to know 

a> Who is calling ?

b> Which one of the 400 rooms do I need to deliver Pizza ?

Lets define a few variables to solve this problem.

1. Variable to store name, we call it "name" and it is of datatype String class. We will examine datatypes in next section. For the time being let us assume that String class datatype helps to store a named memory location which can store name of a person.

2. Variable to store Dorm number - let us call it "dorm_number" and its datatype is int. Int is a primitive datatype, but we will learn the concepts of primitive and user defined datatypes in a later section.

3. Variable to store Dorm Wing - let us call it "dorm_wing" and its datatype is char. Char is again a primitive datatype which can store characters like 'A', 'a', ...

4. Variable to store room number - let us call it "room_number" and its datatype is int. 

Note: A variable's name is also referred to an Identifier. In other words you identify the variable in your program by its identifier. 

So the variable declaration looks like:

String name;

int dorm_number;

char dorm_wing;

int room_number;

We have used 3 different datatypes - String, int and char to store these variables. The choice of these datatypes will be clear when we go through the section on Primitive Data Types.  

Now we have to read inputs' of these 'variables' from the console window using a Java built in datatype called the Scanner Class. Just like the System.out  Class which we have been using to print text to the console or the screen, we use the Scanner class to take input from the console. 

The concept of Class and  methods will be clear when we get into details of Object Oriented Programming. For the time being, let us use these as presented in the sample code. 

The Scanner class is a class in java.util, which allows the user to read values of various types. 

Methods in scanner  allow us to read in String or numeric values from the keyboard. 

The following is a sample code for reading and storing the input 'variables'  and printing them back to the console so that we can take out printout of the order labels.

Introduction to Primitive Datatypes

Primitive Data Types

Computers work on data.

There are innumerable types of data

  • numeric (payment, discount, years,..)
  • textual (name, address, nationality,...)
  • non-printable (e.g. bytecode generated by the Java Compiler)

just to name a few.

Programs handle data.

Conceptually any data handled by a computer can be expressed as a string of 0's and 1's like the following Binary representation "1011110110000000"

The smallest unit of data that can be addressed/accessed/retrieved/stored by a program is 8 bits of data called 1 Byte.

Now some data can be stored in 1 byte, while others need more than 1 byte. Lets see this with a series of examples for Numeric Datatypes:




Example Usage: Variable 

byte 1 Byte Integers in the range of -128 to +127  Daily outside temperature
short 2 Bytes Integers in the range of -2^15 to +2^15 Total Kilometers covered by a car in a month
int 4 Bytes Integers in the range of -2^31 to +2^31 Distance between earth and moon
long 8 Bytes Integers in the range of -2^63 to +2^63 Number of hamburgers produced by a leading fast food chain in a year
float 4 Bytes Floating-point numbers in the range of ±3.4 x 10^-38 to ±3.4 x 10^38 ,with 7 digits of accuracy paycheck
double 8 Bytes Floating-point numbers in the range of ±1.7 x 10^-308 to ±1.7 x 10^308,with 15 digits of accuracy  atomic mass/distance or astronomic distance

The boolean Datatype

Used for variables which can hold one of the two possible values : true or false.

Boolean variables are  useful for evaluating conditions which can be either true or false.

The char Datatype

Variables defined as the char datatype is used for storing characters as we have seen in our Pizza Delivery Problem where the Wings of the Dorm are stored as a single character 'E' or 'W' or 'N' or 'S'. Note the use of single quotes in assigning values to char datatype variables.  

Note: Even though you can access 1 Byte of data,  yet programmatically you can still modify a single bit in those 8 bits  

Primitive Datatypes are the building blocks (as defined in any programming language) for storing and retrieving data


For more detailed understanding of the datatypes  please refer to