https://github.com/in28minutes/java-tutorial-for-beginners
Java Tutorial For Beginners with 500 Code Examples
https://github.com/in28minutes/java-tutorial-for-beginners
java java-8 java8 programming programming-challenges programming-fundamentals
Last synced: 12 months ago
JSON representation
Java Tutorial For Beginners with 500 Code Examples
- Host: GitHub
- URL: https://github.com/in28minutes/java-tutorial-for-beginners
- Owner: in28minutes
- Created: 2019-04-18T09:34:41.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2023-11-26T21:01:07.000Z (over 2 years ago)
- Last Synced: 2025-04-07T03:13:57.900Z (about 1 year ago)
- Topics: java, java-8, java8, programming, programming-challenges, programming-fundamentals
- Size: 1.27 MB
- Stars: 1,557
- Watchers: 83
- Forks: 1,461
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-examples - Java Tutorial For Beginners
README
# Java Tutorial For Beginners
Welcome to this book on **"Learning Java In 150 Steps"**.
I am **Ranga Karanam**, and I have more than two decades of programming experience.
I love Programming. One of the aims I had when I started ```in28minutes``` was to make learning programming easy. Thanks for helping us provide amazing courses to 300,000 learners across the world.
At **In28Minutes**, we ask ourselves one question every day: "How do we create awesome learning experiences?"
In this book, you will learn to write **object** **oriented** code with Java. You will be exposed to a lot of examples, exercises and tips. We will take up a lot of examples, and try and see how to write code for those in Java.
Help us improve this guide - **Fork, Pull Requests, Shares and Likes are recommended**!
## Table of Contents
* [Chapter 01 - Introduction to Programming with Print-Multiplication-Table](#introduction-to-programming-with-print-multiplication-table)
* [Chapter 02 - Understanding Methods](#understanding-methods)
* [Chapter 03 - Understanding the Java Platform](#understanding-the-java-platform)
* [Chapter 04 - Eclipse](#eclipse)
* [Chapter 05 - Object Oriented Progamming (OOP)](#object-oriented-progamming-oop)
* [Chapter 06 - Primitive Data Types](#primitive-data-types)
* [Chapter 07 - Introducing Conditionals - if, switch and more](#introducing-conditionals---if-switch-and-more)
* [Chapter 08 - Loops](#loops)
* [Chapter 09 - Reference Types](#reference-types)
* [Chapter 10 - Arrays and ArrayList](#arrays-and-arraylist)
* [Chapter 11 - Object Oriented Programming (*OOP*) - Revisited](#object-oriented-programming-oop---revisited)
* [Chapter 12 - Introducing Collections](#introducing-collections)
* [Chapter 13 - Introducing Generics](#introducing-generics)
* [Chapter 14 - Introduction to Functional Programming](#introduction-to-functional-programming)
* [Chapter 15 - Threads and Concurrency](#threads-and-concurrency)
* [Chapter 16 - Introduction To Exception handling](#introduction-to-exception-handling)
* [Chapter 17 - File Operations](#file-operations)
* [Chapter 18 - Concurrency : Advanced Topics](#concurrency--advanced-topics)
## Our Approach
We did a study on why students give up on programming?
The popular answer was
> Difficulty in writing their first program
Put yourselves in the shoes of a beginner and look at this typical ```Java Hello World Example```.
```java
package com.in28minutes.firstjavaproject;
public class HelloWorld
{
public static void main(String[] args) {
System.out.println("Hello World");
}
}
```
A ```Programming Beginner``` will be overwhelmed by this. I remember how I felt when I saw this almost 20 years back. Stunned.
Why?
- There are a number of keywords and concepts - package, public, class, static, void, String[] and a lot more..
- What if the programmer makes a typo? Will he be able to fix it?
**We believe that there has to be a better way to learn programming.**
- Why don't we learn programming step by step?
- Why should it not be a lot of fun?
- Why don't we solve a lot of problems and learn programming as a result?
This is the approach we took to writing this guide and develop our introductory programming courses for Java and Python.
> Do you know? The first 3 hours of our Java Course is available [here](https://courses.in28minutes.com/p/java-tutorial-for-beginner-in-250-steps).
## Introduction to Programming with Print-Multiplication-Table
### Step 01: First Challenge : The Print-Multiplication-Table (*PMT-Challenge*)
Learning to program is a lot like learning to ride a bicycle. The first few steps are the most challenging ones.
Once you use this stepwise approach to solve a few problems, it becomes a habit.
In this book, we will introduce you to Java programming by taking on a few simple problems to start off.
> Having fun along the way is what we will aim to do.
_Are you all geared up, to take on your first programming challenge? **Yes**? Let's get started then!_
Our first *programming challenge* aims to do, what every kid does in math class: reading out a multiplication table.
#### The *PMT-Challenge*
1. Compute the multiplication table for ```5```, with entries from ```1``` to ```10```.
2. Display this table.
```
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
```
As part of solving the multiplication table problem, you will be introduced to:
* **JShell**
* **Statements**
* **Expressions**
* **Variables**
* **Literals**
* **Conditionals**
* **Loops**
* **Methods**
#### Summary
In this step, we:
* Stated our first programming challenge, *PMT-Challenge*
* Identified basic Java concepts to learn, to solve this challenge
### Step 02: Introducing ```JShell```
- - -
```JShell``` is a programming tool, introduced in Java SE 9. JShell is a **_REPL_** interface. The term **REPL** refers to this:
* **_'R'_** stands for **R**ead; *Read* the input Java code
* **_'E'_** means **E**val; *Evaluate* the source code
* **_'P'_** translates to **P**rint; *Print* out the result
* **_'L'_** indicates **L**oop; *Loop* around, and wait for the next input
How about starting off exploring Java? Are you game?
##### Snippet-1: Check the Java programming environment
You can use [https://tryjshell.org/](https://tryjshell.org/) to run the code for the first 25 steps. Or you can [Install Java 12+](https://github.com/in28minutes/java-a-course-for-beginners/blob/master/00-02-java-eclipse-installation.md#installing-java). Here's the [troubleshooting section](https://github.com/in28minutes/java-a-course-for-beginners/blob/master/00-02-java-eclipse-installation.md#troubleshooting) if you face problems.
Launch up command prompt or Terminal.
Let type in ```java -version``` on the terminal and press enter.
```
in28minutes$>java -version
java version "x.0.1"
Java(TM) SE Runtime Environment (build x.0.1+11)
Java HotSpot(TM) 64-bit Server VM (build x.0.1+11, mixed mode)
in28minutes$>
```
A successful execution displays the version of Java installed your system. You need to have atleast Java 9 to pursue this book.
##### Snippet-2: Launch JShell
You can launch JShell by typing ```jshell``` at your terminal.
```java
in28minutes$>jshell
| Welcome to JShell version x.0.1
| For an introduction type: /help intro
jshell>
```
When run, this command displays basic information about the installed ```JShell``` program. A ```jshell``` prompt then appears, which waits for your input.
##### Snippet-3: Sample JShell input, using a built-in command
The ```JShell``` command ```/help```, with a parameter ```intro```, gives you basic guidelines on how you use the tool.
```java
jshell> /help intro
|
| intro
| The jshell tool allows you to execute Java code, getting immediate results.
| You can enter a Java definition (variable, method, class, etc), like: int x =8
| or a Java expression, like: x + x
| or a Java statement or import.
| These little chunks of Java code are called 'snippets'.
| There are also jshell commands that allow you to understand and
| control what you are doing, like: /list
|
| For a list of commands: /help
jshell>
```
##### Snippet-4: Evaluating an expression
Type ```3+4``` at the Jshell prompt
```java
jshell> 3 + 4
$1 ==> 7
jshell>
```
This was your first real **REPL** cycle! When you type in ```3 + 4```, JShell evaluates it and prints the result.
> The entity ```$1``` is actually a variable name assigned to the result. We will talk about this later.
##### Snippet-5: Getting out of JShell
The ```/exit``` command terminates the ```JShell``` program, and we are back to the terminal prompt.
```java
jshell> /exit
| Goodbye
in28minutes$>
```
##### Snippet-6: Again, enter JShell and Exit it!
You can now effortlessly launch, feed code to, and exit from ```JShell```!
```java
in28minutes$> jshell
| Welcome to JShell version 9.0.1
| For an introduction type: /help intro
jshell> /exit
| Goodbye
in28minutes$>
```
#### Summary
In this step, we learned:
* How to launch ```JShell``` from our terminal, and run a few commands on it
* How to run Java code on the ```JShell``` prompt
### Step 03: Welcome to Problem Solving
Lets try to break down the *PMT-Challenge* problem to be able to solve it.
```
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
```
Here is how our draft steps look like
* Calculate ```5 * 3``` and print result as ```15```
* Print ```5 * 3 = 15``` (```15``` is result of previous calculation)
* Do this ten times, once for each table entry (going from ```1``` to ```10```)
#### Summary
In this step, we:
* Broke down the *PMT-Challenge* problem into sub-problems
### Step 04: Introducing Expressions
The first part of solving our *PMT-Challenge* is to calculate the product of ```5``` and another number, say ```3```.
Let's start up jshell and type ```5 X 3```.
```java
in28minutes$> jshell
| Welcome to JShell version x.0.1
| For an introduction type: /help intro
jshell> 5 X 3
| Error:
| ';' expected
| 5 X 3
| ^
| Error:
| not a statement
| 5 X 3
| ^
| Error:
| ';' expected
| 5 X 3
| ^
| Error:
| missing return statement
| 5 X 3
| ^---^
```
You probably look at the symbol 'X' as a multiplier, remembering your school days.
Java does not identify '```X```' as the multiplication operator! Java supports multiplication, but only if you use its *predefined* operator, ```*```.
Let's type in code shown below:
```java
jshell> 5 * 3
$1 ==> 15
jshell>
```
Success!
Let's look at some terminology:
- ```5 * 3 ``` is an expression.
- ```5``` and ```3``` are operands. They are also called **literals** or constant values.
- ```*``` is an operator.
Java also has built-in operators to perform other numerical tasks, such as:
* Addition: ```+```
* Subtraction: ```-```
* Division: ```/```
* Modulo arithmetic: ```%```
The following examples illustrate how to use them.
```java
jshell> 5 * 10
$2 ==> 50
jshell> 5 + 10
$3 ==> 15
jshell> 5 - 10
$4 ==> -5
jshell> 10 / 2
$5 ==> 5
jshell>
```
Your school math memories are still fresh, and the operators don't seem to disappoint either! ```+```, ```-``` and ```/``` are your bread-and-butter operators.
```%``` is the modulo operator, which gives you the remainder when integer division is performed.
```java
jshell> 9 % 2
$6 ==> 1
jshell> 8 % 2
$7 ==> 0
jshell>
```
##### Snippet: Complex Expressions, Anyone?
Java allows you to use more than one operator within an expression.
Let's look at some simple expressions with multiple operators.
```java
jshell> 5 + 5 + 5
$8 ==> 15
jshell> 5 + 10 - 15
$9 ==> 0
jshell> 5 * 5 + 5
$10 ==> 30
jshell> 5 * 15 / 3
$11 ==> 25
```
Each of above expressions have two operators and three operands.
#### Summary
In this step, we:
* Learned how to construct numeric expressions
* Discovered that operators are predefined symbols
* Combined several operators to form larger expressions
### Step 05: Programming Exercise PE-1 (With Solutions)
At this stage, your smile tells us that you enjoy evaluating Java expressions. What if we tickle your mind a bit, to make sure it hasn't fallen asleep?
Okay, here comes a couple of programming exercises.
1. Write an expression to calculate the number of minutes in a day.
2. Write an expression to calculate the number of seconds in a day.
#### Solution 1
60 (minutes in an hour) multipled by 24 (hours in a day)
```java
jshell> 60 * 24
$1 ==> 1440
```
#### Solution 2
60 (seconds in a minute) multipled by 60 (minutes in an hour) multipled by 24 (hours in a day)
```java
$jshell>60 * 60 * 24
$1 ==> 86400
```
### Step 06: Operators
Lets look at a few more examples to understand operators.
#### Invalid Operators
Let's type in ```5 ** 6``` followed by ```5 $ 6``` in JShell
```java
jshell> 5 ** 6
| Error:
| Illegal start of expression
| 5 ** 6
| ^
jshell> 5 $ 6
| Error:
| ';' expected
| 5 $ 6
| ^
| Error:
| not a statement
| 5 $ 6
| ^
| Error:
| ';' expected
| 5 $ 6
| ^
jshell> 5 */ 6
| Error:
| Illegal start of expression
| 5 */ 6
| ^
```
```JShell``` was not impressed with our efforts at all!
Java has a set of grammar rules, called its **syntax**. Code that does not follow these rules throw errors. The compiler informs you by listing the errors, and also provides hints on how you can correct them.
Now, why is an error being thrown?
Operators in Java are all predefined, and limited in number. ```*``` is a valid operator, whereas ```**``` and ```$``` were rejected, with error messages.
#### Understanding Result of Expression with Mixed Types
Let's look at another example:
```java
jshell> 5 / 2
$1 ==> 2
jshell>
```
Surprise, Surprise! ```JShell``` seems to evaluate ``` 5 / 2 ``` to ``` 2 ``` instead of ```2.5```. Where did we go wrong?
Read what follows, with the biggest magnifying lens you can find:
**The result of an expression when evaluated, depends on the operator context**. This context is determined by the operands passed to it
There are two kinds of numbers typically used in programming : integer (1,2,3,...) and floating-point (1.1,2.3, 56.7889 etc). These values are represented by different **types** in Java. Integers are commonly of type ```int```, and the floating-point numbers are ```double``` by default.
In the expression ```5/2```, both ```5``` and ```2``` are of type ```int```. So, the result is also of type ```int```.
Let's try with a few floating point numbers:
```java
jshell> 5.0 / 2.0
$2 ==> 2.5
```
Both ```5.0``` and ```2.0``` are of type ```double```, the result is ```double```. We get a result of ```2.5```, as expected.
Let's do a mixed operation - using a floating point number and integer.
```java
jshell> 5.0 / 2
$3 ==> 2.5
jshell>
```
Among the types ```int``` and ```double```, ```double``` is considered to be a *wider type*. When you perform a numeric operation between two types, the result will be of the wider type.
#### Understanding Precedence of Operators
Let's look at few complex examples of expressions with more than one operator.
```java
jshell> 5 + 5 * 6
$1 ==> 35
jshell> 5 - 2 * 2
$2 ==> 1
jshell> 5 - 2 / 2
$3 ==> 4
jshell>
```
Surprised with the results? You might expect 5 + 5 * 6 evaluate to 10 * 6 i.e. 60. Howeever, we got ```35```!
> We write English left-to-right, and carry this habit to calculations as well.
_In expressions with multiple operators, the order of sub-expression evaluation depends on **operator precedence**._
The basic rules for operator precedence are actually quite simple (we will look at other rules a little later).
The operators in the set {```*```, ```/```, ```%```} have higher precedence than the operators in the set {```+```, ```-```}.
In the expression ```5 + 5 * 6``` : 5*6 is evaluated first. So, 5 + 5 * 6 becomes 5 + 30 i.e. 35.
```5 - 2 * 2``` and ```5 - 2 / 2``` are evaluated by following the same rules.
#### Use paranthesis for clear code
Java provides syntax elements, called **parentheses** ```(``` and ```)```, to group parts of an expression.
```java
jshell> (5 - 2) * 2
$4 ==> 6
jshell> 5 - (2 * 2)
$5 ==> 1
jshell>
```
When you put an expression in parenthesis, it is evaluated first. (5 - 2) * 2 => 3 * 2 => 6.
Parentheses lead to better readability as they reduce confusion and help avoid errors.
The old adage **_A stitch in time saves nine_** rings very true here. Use parentheses to make your code easy to read, even when they might not be necessary.
#### Summary
In this step, we:
* Discovered that operators are all predefined
* Learned that result of operation depends on operand types
* Understood what operator precedence means
* Used parentheses to group parts of an expression
### Step 07: Introducing Console Output
We have computed the product of two literals (as in ```5 * 3```) in earlier steps.
The next step is to print this result in a customized format - ```5 * 3 = 15```.
How do we do this?
Let try typing it in into JShell
```java
jshell> 5 * 3 = 15
| Error:
| unexpected type
| required: variable
| found: value
| 5 * 3 = 15
| ^---^
```
Hmm! Error.
How do we print text?
Java has a built-in utility method called ```System.out.println()```, that displays text on the console.
```java
jshell> System.out.println(3*4)
12
```
We formed an expression, ```3*4```, and *passed* it to ```System.out.println()```, which is a built-in Java **method**.
```System.out.println(3*4)``` is an example of a **statement**. It is a **method call**.
The syntax rules for method calls are quite strict, and all its parts are mandatory.
```java
jshell> System.out.println3*4)
| Error:
| ';' expected
| System.out.println3*4)
|___________________^
| Error:
| cannot find symbol
| symbol: variable println3
| System.out.println3*4)
| ^---------------------^
jshell> System.out.println 3*4
| Error:
| ';' expected
| System.out.println 3*4
| ^
| Error:
| cannot find symbol
| symbol: variable println
| System.out.println 3*4
| ^----------------^
```
What if we want to print an entry of the Multiplication Table, as part of our solution to *PMT-Challenge*? In other words, how do we print the exact text ```5 * 2 = 10``` on the console?
```java
jshell> System.out.println(5 * 2 = 10)
| Error:
| unexpected type
| required: variable
| found: value
| System.out.println(5 * 2 = 10)
|____________________^--------^
```
You wanted to print ```5 * 2 = 10``` on the console. However, we see that we cannot pass ```5 * 2 = 10``` as an argument to ```System.out.println()```.
```5 * 2 = 10``` is not a single value. It is a piece of text with numeric characters, ```*``` and ```=```.
In Java, we represent text using ```String```. A ```String literal``` is a sequence of characters, enclosed within **double quotes**: ```"``` and ```"```.
```java
jshell> System.out.println("5 * 2 = 10")
| 5 * 2 = 10
```
Congratulations! You have now figured out how to display not just numbers on the console, but text as well!
#### Summary
In this step, we:
* Were introduced to the ```System.out.println()``` method for console output
* Used this utility to print a single *PMT-Challenge* table entry
### Step 08: Programming Exercise PE-02
Try and solve the following exercises:
1. Print ```Hello World``` onto the console.
2. Print ```5 * 3```, as is.
3. Print the calculated value of ```5 * 3```.
4. Print the number of seconds in a day, using the ```System.out.println``` method.
5. Do a syntax revision for the code that you write for each of the above exercises. In your code, identify the following elements:
* Numeric and string literals
* Expressions
* Operators
* Operands
* Method calls
### Step 09: Solutions to PE-02
#### Solution 1
```java
jshell> System.out.println("Hello World")
Hello World
```
#### Solution 2
```java
jshell> System.out.println("5 * 3")
5 * 3
jshell>
```
#### Solution 3
```java
jshell> System.out.println(5 * 3)
15
```
#### Solution 4
```java
jshell> System.out.println(60 * 60 * 24)
86400
```
### Step 10: Whitespace, Case sensitiveness and Escape Characters
The term *whitespace* refers to any sequence of continuous space, tab or newline characters.
#### Whitespace
Let's see a few examples of whitespace in action.
Whitespace affects the output when used in-and-around string literals.
```java
jshell> System.out.println("Hello World")
Hello World
jshell> System.out.println("Hello World")
Hello World
jshell> System.out.println("HelloWorld")
HelloWorld
```
Whitespace is ignored by the compiler when used around numeric expressions.
```java
jshell> System.out.println(24 * 60 * 60)
86400
jshell> System.out.println(24 * 60 * 60)
86400
jshell>
```
#### Case Sensitive
Java is case sensitive.
```System.out.println()``` involve pre-defined Java elements : the ```System``` **class** name, the ```out``` **variable** name,and the ```println``` **method** name. All are *case-sensitive*. If any character in these names is used with a different case, you get an error.
```java
jshell> system.out.println("Hello World")
| Error:
| package system does not exist
| system.out.println("Hello World")
| ^-------^
jshell> System.Out.println("Hello World")
| Error:
| cannot find symbol
| symbol: variable Out
| System.Out.println("Hello World")
| ^------------^
jshell> System.out.Println("Hello World")
| Error:
| cannot find symbol
| symbol: method Println(java.lang.string)
| System.out.Println("Hello World")
| ^---------------------^
jshell>
```
Inside a string literal, the case of characters do not cause errors. The literal will be taken in and printed, as-is.
```java
jshell> System.out.println("hello world")
hello world
jshell> System.out.println("HELLO WORLD")
HELLO WORLD
```
#### Escape Characters
An **escape character** is a special symbol, used with another regular symbol to form an **escape sequence**. In Java, the '```\```' (*back-slash*) character plays the role of an escape character. This escape sequence changes the original usage of the symbols.
If you want to print the string **delimiter**, the ```"``` character, you need to escape it with a ```\```. Without it, a ```"``` character within a string literal causes an error!
```java
jshell> System.out.println("Hello "World")
| Error:
| ')' expected
| System.out.println("Hello "World")
| ^
jshell> System.out.println("Hello \"World")
Hello "World
jshell>
```
The escape sequence ```\n``` inserts a *newline*.
```java
jshell> System. out.println("Hello \n World")
Hello
World
jshell> System.out.println("Hello n World")
Hello n World
jshell> System.out.println("Hello\nWorld")
Hello
World
jshell>
```
The escape sequence ```\t``` inserts a *tab*.
```java
jshell> System.out.println("Hello \t World")
Hello World
jshell> System.out.println("Hello t World")
Hello t World
jshell> System.out.println("Hello\tWorld")
Hello World
jshell>
```
How do you print a ```\```?
```
jshell> System.out.println("Hello \ World")
| Error:
| illegal escape character
| System.out.println("Hello \ World")
```
You would need to escape it with another ```\```. Printing ```\\``` outputs the symbol ```\``` to the console.
```java
jshell> System.out.println("Hello \\ World")
Hello \ World
jshell> System.out.println("Hello \\\\ World")
Hello \\ World
```
#### Summary
In this step, we:
* Were introduced to method call syntax, with ```System.out.println()```
* Discovered the uses of whitespace characters
* Learned about Java escape sequences
### Step 11: More On Method Calls
Let's look at method calls with a few more examples.
You know how to invoke a method with a single argument, courtesy ```System.out.println(3*4)```. Other scenarios do exist, such as
* Calling a method without any arguments
* Calling a method with several arguments
Let's check them out, using the built-in methods in Java ```Math``` class.
#### Method without parameters
In method calls, parentheses are a necessary part of the syntax. Don't leave them out!
```Math.random()``` prints a random real number in the range ```[0 .. 1]```, a different one on each call
```java
jshell> Math.random
| Error:
| cannot find symbol
| symbol: Math.random
| Math.random
| ^------------- ^
jshell> Math.random()
$1 ==> 0.0424279106074651_
jshell> Math.random()
$2 ==> 0.8696879746593543
jshell> Math.random()
$3 ==> 0.8913591586787125
```
#### Method with multiple parameters
How do we call ```Math.min``` with two parameters ```23``` and ```45```?
```java
jshell> Math.min 23 45
| Error
| cannot find symbol
| symbol: variable min
| Math.min 23 45
| ^---------^
jshell> Math.min(23 45)
| Error
| ')' expected
| Math.min 23 45
| ---------------^
```
While making method calls, the programmer must
* Enclose all the parameters within parentheses
* Separate individual parameters within the list, using the comma symbol '```,```'.
```java
jshell> Math.min(23, 45)
$4 ==> 23
jshell> Math.min(23, 2)
$5 ==> 2
jshell> Math.max(23, 45)
$6 ==> 45
jshell> Math.max(23, 2)
$7 ==> 2
jshell>
```
```Math.min()``` returns the minimum of two given numbers. ```Math.max()``` returns the maximum of two given numbers.
#### Summary
In this step, we:
* Understood how zero, or multiple parameters are passed during a method call
### Step 12: More Formatted Output
```System.out.println()``` can accept one value as an argument at a maximum.
To display the multiplication table for ```5``` with a calculated value, we need a way to print both numbers and strings.
For this we would need to use another built-in method ```System.out.printf()```.
When ```System.out.printf()``` is called with a *single* string argument, it prints some illegible information. For now, it suffices to know, that this information is about the built-in type ```java.io.PrintStream```.
```java
jshell> System.out.printf("5 * 2 = 10")
5 * 2 = 10$1 ==> java.io.PrintStream@4e1d422d
jshell>
```
The good news is, that if we call the ```println()``` method on this, the illegible stuff disappears.
```java
jshell> System.out.printf("5 * 2 = 10").println()
5 * 2 = 10
```
The method ```System.out.printf()``` accepts a variable number of arguments:
* The first argument specifies the print format. This is a string literal, having zero or more **format specifiers**. A format specifier is a predefined literal (such as ```%d```), that formats data of a particular type (```%d``` formats data of type ```int```).
* The trailing arguments are expressions,
Lots of theory? Let's break it down. Let's look at an example.
```java
jshell> System.out.printf("5 * 2 = %d", 5*2).println()
5 * 2 = 10
jshell>
```
```System.out.printf("5 * 2 = %d", 5*2).println()``` gives an output ```5 * 2 = 10```. ```%d``` is replaced by the calculated value of ```5*2```.
Let's play a little more with ```printf```:
```java
jshell> System.out.printf("%d %d %d", 5, 7, 5).println()
5 7 5
```
Let's try to display a calculated value. In the example below ```5*7``` is calculated as ```35```.
```java
jshell> System.out.printf("%d %d %d", 5, 7, 5*7).println()
5 7 35
```
Let's use this to print the output in the format that we want to use for multiplication table:
```java
jshell> System.out.printf("%d * %d = %d", 5, 7, 5*7).println()
5 * 7 = 35
```
Congratulations. We are able to calculate ```5*7``` and print ```5 * 7 = 35``` successfully.
#### Exercise
1. Print the following output on the console: ```5 + 6 + 7 = 18```. Use three literals ```5```, ```6```, ```7```. Calculate 18 as ```5 + 6 + 7```.
#### Solution
```java
jshell> System.out.printf("%d + %d + %d = %d", 5, 6, 7, 5 + 6 + 7).println()
5 + 6 + 7 = 18
jshell>
```
#### Playing with ```System.out.printf()```
In the example below, there are four format specifiers (```%d```) and only three value arguments ```5, 6, 7```.
```java
jshell> System.out.printf("%d + %d + %d = %d", 5, 6, 7).println()
5 + 6 + 7 = | java.util.MissingFormatArgumentException thrown: Format specifier '%d'
| at Formatter.format (Formatter.java:2580)
| at PrintStream.format (PrintStream.java:974)
| at PrintStream.printf (PrintStream.java:870)
| at (#52:1)
```
In a call to ```System.out.printf()```, if the number of format specifiers exceeds the number of trailing arguments, the Java run-time throws an *exception*.
If the number of format specifiers is less than the number of trailing arguments, the compiler simply ignores the excess ones.
```java
jshell> System.out.printf("%d + %d + %d", 5, 6, 7, 8).println()
5 + 6 + 7
jshell>
```
#### More Format Specifiers
String values can be printed in ```System.out.printf()```, using the format specifier ```%s```.
```java
jshell> System.out.printf("Print %s", "Testing").println()
Print Testing
```
Earlier, we used %d to print an ```int``` value. You cannot use %d to display floating point values.
```java
jshell> System.out.printf("%d + %d + %d", 5.5, 6.5, 7.5).println()
| java.util.IllegalFormatConversionException thrown: d != java.lang.Double
| at Formatter$FormatSpecifier.failedConversion(Formatter.java:4331)
| at Formatter$FormatSpecifier.printInteger(Formatter.java:2846)
| at Formatter$FormatSpecifier.print(Formatter.java:2800)
| at Formatter.format(Formatter.java:2581)
| at PrintStream.format(PrintStream.java:974)
| at PrintStream.print(PrintStream.java:870)
| at #(57:1)
```
Floating point literals (or expressions) can be formatted using ```%f```.
```java
jshell> System.out.printf("%f + %f + %f", 5.5, 6.5, 7.5).println()
5.500000 + 6.500000 + 7.500000
jshell>
```
#### Summary
In this step, we:
* Discovered how to do formatted output, using ```System.out.printf()```
* Stressed on the number and sequence of arguments for formatting
* Explored the built-in format specifiers for primitive types
### Step 13: Introducing Variables
In the previous steps, we worked out how to print a calculated value as part of our multiplication table.
```java
jshell> System.out.printf("%d * %d = %d", 5, 1, 5 * 1).println()
5 * 1 = 5
```
**How do we print the entire multiplication table?**
We can do something like this.
```java
jshell> System.out.printf("%d * %d = %d", 5, 1, 5 * 1).println()
5 * 1 = 5
jshell> System.out.printf("%d * %d = %d", 5, 2, 5 * 2).println()
5 * 2 = 10
jshell> System.out.printf("%d * %d = %d", 5, 3, 5 * 3).println()
5 * 3 = 15
jshell> System.out.printf("%d * %d = %d", 5, 4, 5 * 4).println()
5 * 4 = 20
jshell>
```
Too much work. Isn't it?
If you carefully observe the code, these statements are very similar.
***What's changing?*** The number in the third and fourth parameter slots changes from 1 to 4.
Wouldn't it be great to have something to represent the changing value?
***Welcome variables.***
```java
jshell> int number = 10
number ==> 10
jshell>
```
#### Terminology and Background
In the statement ```int number = 10```,
* ```number``` is a **variable**.
* The literal ```number``` is the variable **name**.
* The Java *keyword* ```int``` specifies the **type** of ```number```.
* The literal ```10``` provided ```number``` with an **initial value**.
* This entire statement is a **variable definition**.
The effects of this variable definition are:
* A specific location in the computer's memory is reserved for ```number```.
* This location can now hold data of type ```int```.
* The value ```10``` is stored here.
You can change the value of number variable:
```java
jshell> number = 11
number ==> 11
```
Above statement is called variable **assignment**.
An assignment causes the value stored in the memory location to change. In this case, ```10``` is replaced with the value ```11```.
You can look up the value of number variable.
```java
jshell> number
number ==> 11
```
You can change the value of a variable multiple times.
```
jshell> number = 12
number ==> 12
jshell> number
number ==> 12
```
Let's create another variable:
```java
jshell> int number2 = 100
number2 ==> 100
jshell> number2 = 101
number2 ==> 101
jshell> number2
number2 ==> 101
jshell>
```
The statement ```int number2 = 100``` defines a *distinct* variable ```number2```.
***How do we use variables to simplify and improve the solution to *PMT-Challenge*?***
Let's take a look.
```java
jshell> System.out.printf("%d * %d = %d", 5, 4, 5*4).println()
5 * 4 = 20
```
Let's define a variable ```i```, and initialize it to ```1```.
```java
jshell>int i = 1
i ==> 1
jshell> i
i ==> 1
jshell> 5*i
$1 ==> 5
```
Let's update the multiplication table printf to use the variable i.
```java
jshell> System.out.printf("%d * %d = %d", 5, i, 5*i).println()
5 * 1 = 5
```
***How do we print ```5 * 2 = 10```?***
We update ```i``` to ```2``` and execute the same code as before.
```java
jshell> i = 2
i ==> 2
jshell> 5*i
$2 ==> 10
jshell> System.out.printf("%d * %d = %d", 5, i, 5*i).println()
5 * 2 = 10
jshell>
```
You can update the value of ```i``` to any number.
The previous statement would print the corresponding multiple with 5.
```java
jshell> i = 3
i ==> 3
jshell> System.out.printf("%d * %d = %d", 5, i, 5*i).println()
5 * 3 = 15
jshell> i = 10
i ==> 10_
jshell> System.out.printf("%d * %d = %d", 5, i, 5*i).println()
5 * 10 = 50
jshell>
```
By varying the value of ```i```, we are able to print different multiples of 5 with the same statement.
Congratulations! You made a major discovery. Variables.
#### Summary
In this step, we:
* Understood the need for variables
* Observed what different parts of a variable definition mean
* Seen what goes on behind-the-scenes when a variable is defined
### Step 14: Programming Exercise PE-03 (With solution)
1. Create three integer variables ```a```, ```b``` and ```c```.
* Write a statement to print the sum of these three variables.
* Change the value of ```a```, and then print this sum.
* Then again, change the value of ```b``` and print this sum.
#### Solution to PE-03
```java
jshell>int a = 5
a ==> 5
jshell>int b = 7
b ==> 7
jshell>int c = 11
c ==> 11
jshell>System.out.printf("a + b + c = %d", a+b+c).println()
a + b + c = 23
jshell>a = 2
a ==> 2
jshell>System.out.printf("a + b + c = %d", a+b+c).println()
a + b + c = 20
jshell>b = 9
b ==> 9
jshell>System.out.printf("a + b + c = %d", a+b+c).println()
a + b + c = 22
jshell>
```
### Step 15: Using Variables
Variables should be declared before use.
```java
jshell>newVariable
| Error:
| cannot find symbol
| symbol: newVariable
| newVariable
^------------^
```
Java is a **strongly typed** programming language. This means two things:
* Every variable in a program must be declared, with a type.
* Values assigned to a variable should be:
* same type as the variable's type, or
* **compatible** with the variable's type
```java
jshell> int number = 5.5
| Error:
| incompatible types: possible lossy conversion from double to int
| int number = 5.5
|_____________^---^
jshell>
```
The variable ```number``` is an integer, mathematically a number. The constant ```5.5``` is a number as well.
Why does it result in error?
```5.5``` is a floating-point number of type ```double```. We are trying to store a ```double``` inside a memory slot meant for ```int```.
Let's consider another example:
```java
jshell> int number = "Hello World"
| Error:
| incompatible types: java.lang.String cannot be converted to int
| int number = "Hello World"
|_____________^--------------^
jshell>
```
```number``` is an ```int``` variable, and we are trying to store a ```String``` value ```"Hello World"```. ***Not allowed.***
#### Summary
In this step, we:
* Observed how declaring a variable is important
* Understood the compatibility rules for basic Java types
### Step 16: Variables: Behind-The-Scenes
Giving a value to a variable, during its declaration, is called its initialization.
```java
jshell> int number = 5
number ==> 5
jshell>
```
The statement ```int number = 5``` combines a declaration and the initialization of ```number```.
The next statement ```int number2 = number``` is a little different.
```java
jshell> int number2 = number
number2 ==> 5
jshell> number2
number2 ==> 5
jshell>
```
The initial value for ```number2``` is another variable, which is previously defined (```number```).
Here's what goes on behind the scenes with ```int number2 = number```:
* A memory slot is allocated for ```number2``` with size of ```int```.
* Value stored in ```number```'s slot is copied to ```number2```'s slot.
In example below, ```a = 100```, ```c = a + b```, ``` a = c``` are called ```assignments```.
```java
jshell> int a = 5
a ==> 5
jshell> int b = 10
b ==> 10
jshell> a = 100
a ==> 100
jshell> int c
c ==> 0
jshell> c = a + b
c ==> 110
jshell> a = c
a ==> 110
jshell> int d = b + c
d ==> 120
jshell>
```
Variable **assignment** is allowed to happen in multiple ways:
* From a literal value, to a variable, having compatible types. The statement ```a = 100``` is one such example.
* From a variable to another variable, of compatible types.The statement ```a = c``` illustrates this case.
* From a variable expression to a variable. This expression can be formed with variables and literals of compatible types, and is evaluated before the assignment. The statement ```c = a + b```below is an example of this.
An assignment to a constant literal is **not allowed**.
```java
jshell> 20 = var
| Error:
| unexpected type
| required : variable
| found : value
| 20 = var
| ^^
jshell>
```
#### Summary
In this step, we discussed how to provide variables with initial values and the basics of assigment.
### Step 17: Naming Variables
The syntax rules for variable definition are quite strict. Do we have any freedom with the way we name variables? The answer is "yes", to some extent.
### Variable Name Rules
Here are the Java rules for variable names, in a nutshell:
A variable name can be a sequence, in any order, of
* Letters [```A-Z, a-z```]
* Numerical digits [```0-9```]
* The special characters '```$```' ("dollar") and '```_```' ("underscore")
With the following exceptions:
* The name cannot start with a numerical digit [```0-9```]
* The name must not match with a predefined Java **keyword**
Let's now see what kinds of errors the compiler throws if you violate these naming rules.
***Error : Using a invalid character ```-```***
```java
jshell> int test-test
| Error:
| ';' expected
| int test-test
| ^
```
***Error : Starting name of a variable with a number***
```java
jshell> int 2test
| Error:
| '.class' expected
| int 2test
| ^
| Error:
| not a statement
| int 2test
| ^--^
| Error:
| unexpected type
| required: value
| found: class
| int 2test
| ^--^
| Error:
| missing return statement
| int 2test
| ^-------^
jshell>
```
***Error : Using a keyword as the name of variable***
In Java, certain special words are called ```keywords```. For example, some of the data types we looked at - ```int```, ```double```. These ```keywords``` cannot be used as variable names.
```java
jshell> int int
...> ;
| Error:
| '.class' expected
| int int
| ^
| Error:
| unexpected type
| required: value
| found: class
| int int
| ^--^
| Error:
| missing return statement
| int int
| ^------...
```
### Variable Naming Conventions
Good programmers write readable code. Giving proper names to your variables makes your code easy to read.
In a football scorecard application, using a name ```s``` for a score variable is vague. Something like ```score``` carries more meaning, and is preferred.
```java
jshell> int s
s ==> 0
jshell> int score
score ==> 0
jshell>
```
> The Java community, with its wealth of experience, *suggests* that programmers follow some **naming conventions**. Above examples is one of these. Violation of these rules does not result in compilation errors. That's why, these are called **conventions**.
In Java, another convention is to use **CamelCase** when we have multiple words in variable name.
```java
jshell> int noOfGoals
noOfGoals ==> 0
```
```noOfGoals``` is easier to read than ```noofgoals```.
Both ```noOfGoals``` and ```NoOfGoals``` are equally readable.
But in Java, the convention followed is to start variable names with a lower case letter. So, for a variable, ```noOfGoals``` is preferred.
*** NOT RECOMMENDED***
```
jshell> int NoOfGoals
NoOfGoals ==> 0
```
Your variable names can be very long.
```java
jshell> int iThinkThisIsQuiteALongName
iThinkThisIsQuiteALongName ==> 0
jshell> int iThinkThisIsSuchALongNameThatIMightNeverUseSuchALongNameAgain
iThinkThisIsSuchALongNameThatIMightNeverUseSuchALongNameAgain ==> 0
```
However, avoid using very long variable names.
***Use the shortest meaningful and readable name possible for your variables.***
#### Summary
In this step, we:
* Explored the Java language rules for variable names
* Understood that there are conventions to guide us
* Looked at a popular variable naming conventions
### Step 18: Introducing Primitive Types
The table below lists the **primitive types** in Java.
| Type of Values | Java Primitive Type | Size (in bits) | Range of Values | Example |
| :---: | :---: |:---: |:---: |:---:|
| Integral Values |byte|8|```-128``` to ```127```|```byte b = 5;```|
| Integral Values |short|16|```-32,768``` to ```32,767```|```short s = 128;```|
| Integral Values |int|32|```-2,147,483,648``` to ```2,147,483,647```|```int i = 40000;```|
| Integral Values |long|64|```-9,223,372,036,854,775,808``` to ```9,223,372,036,854,775,807```|```long l = 2222222222;```|
|Floating-Point Values| float| 32 | approximately ±3.40282347E+38F. NOT very precise (avoid for financial/scientific math)|```float f = 4.0f;```|
|Floating-Point Values|double|64| approximately ±1.79769313486231570E+308. NOT very precise, but better than float (also avoid for financial/scientific math)|```double d = 67.0;```|
|Character Values|char|16|```'\u0000``` to ```'\uffff```|```char c = 'A';```|
|Boolean Values| boolean|1| ```true``` or ```false```|```boolean isTrue = false;```|
Let's now look at how we create data of these types, and store them in memory.
We choose type for data, based on:
* The type of data we want to store
* The range of values we would want to store
> Note: In the above examples, we used semi-colon ```;``` character at the end. This is the Java **statement separator**, which is not mandatory in ```JShell``` for single statements. However, it is a must when you use *code editors* and *IDE*'s, to write Java code.
#### Integer Types
The only difference among the integer types is their storage capacity.
```java
jshell> byte b = 5
b ==> 5
jshell> short s = 128
s ==> 128
jshell> int i = 40000
i ==> 40000
jshell> long l = 2222222222
l ==> 2222222222
jshell>
```
#### Floating Value Types
```double``` is the default type for floating type values with size 64 bits.
```
jshell> double d = 67.0
d ==> 67.0
```
```float``` occupies 32 bits. ```float``` literals need to have a suffix '```f```' (as in ```4.0f```).
```
jshell> float f = 4.0f
f ==> 4.0
```
If this suffix ```f``` is omitted, a floating-point number is assumed to be a ```double```. You cannot store a 64 bit value into 32 bits.
```java
jshell> float f2 = 4.0
| Error:
| incompatible types: possible lossy conversion from double to float
| float f2 = 4.0
| ^-^
```
You can store ```float``` value in ```double```. Remember, both types store similar kind of values and a ```float``` value is only 32 bits where as ```double``` has 64 bits.
```
jshell> double d2 = 67.0f
d2 ==> 67.0
```
#### Character Type
The character type ```char``` can store a single character symbol. The symbol must be enclosed within a pair of single quotes, ```'``` and ```'```.
```java
jshell> char c = 'A'
c ==> 'A'
```
Following are a few char declaration errors: Not using single quotes and trying to store multiple characters.
```
jshell> char ch = A
| Error:
| cannot find symbol
| symbol: variable A
| char ch = A
| ^
jshell> char cab = 'AB'
| Error:
| unclosed character literal
| char cab = 'AB'
| ^
```
#### Boolean Type
The concept of a ```boolean``` type is rooted in mathematical logic. The data type can store two possible values, ```true``` and ```false```. Both are case-sensitive labels, but not enclosed in quotes.
```java
jshell> boolean isTrue = false
isTrue ==> false
jshell> isTrue = true
isTrue ==> true
jshell> boolean isFalse = True
| Error:
| cannot find symbol
| symbol: variable True
| boolean isFalse = True
| ^--^
jshell> isFalse = False
| Error:
| cannot find symbol
| symbol: variable False
| isFalse = False
| ^---^
```
```boolean``` data are mostly used in expressions used to form logical conditions in your code. We will talk more about this - when we explore Java conditionals.
#### Summary
In this step, we:
* Looked at the primitive data types provided in Java
* Understood the categories into which these types are divided
* Saw what kind of data can be stored in each primitive type
### Step 19: Choosing A Data Type
How do we choose the data type for a variable? Let's look at a few examples.
#### Example 1 : Goals in a Football match
Consider a sports broadcaster that writes a program to track football scores. In a football game there are two teams playing. A game is generally 90 minutes long, with extra time pushing it to 120 minutes at the most. Practically speaking, the scorelines are never huge. From a practical standpoint, the number of goals scored in a match would never exceed 7200 (the number of seconds in 120 minutes). Playing it a bit safe, a ```short``` data type would be enough to store the score of each side (though a ```byte``` would be more than enough).
```java
jshell> short numTeamAGoals = 0
numTeamAGoals ==> 0
jshell> short numTeamBGoals = 0
numTeamBGoals ==> 0
jshell>
```
#### Example 2 : How do we store population of the world?
We know that the global count of humans is well over 7 billion now, so the only integer data type that can store it is a ```long```.
```java
jshell> long globalPopulation = 7500000000;
globalPopulation ==> 7500000000
jshell>
```
#### Example 3 : How do we store average rainfall in a month?
Rainfall is usually measured in millimeters (*mm*), and we know computing an average over 30 days is very likely to yield a floating-point number. Hence we can use a ```float```, or for more accuracy, a ```double``` to store that average.
```java
jshell> float avgJanRainfall = 31.77f;
avgJanRainfall ==> 31.77
jshell> double averageJanRainfall = 31.77
averageJanRainfall ==> 31.77
jshell>
```
#### Example 4 : Grades of a Student
Classroom grades in high school are generally A, B, C , ....
For a program that generates a grade report, a ```char``` type would be the best bet.
```java
jshell> char gradeA = 'A'
gradeA ==> 'A'
jshell> char gradeB = 'B'
gradeB ==> 'B'
jshell> char gradeC = 'C'
gradeC ==> 'C'
jshell> char gradeD = 'D'
gradeD ==> 'D'
jshell> char gradeF = 'F'
gradeF ==> 'F'
jshell>
```
#### Example 5 : Is a Number Odd or Even?
A ```boolean isNumEven``` is a good bet, with a default initial value of ```false```. It can be changed to ```true``` if the number turns out to be even.
```java
jshell> boolean isNumEven
isNumEven ==> false
jshell> isNumEven = true
isNumEven ==> true
jshell>
```
#### Summary
In this step, we:
* Understood how to think while choosing a data type
* Explored cases for integer, floating-point, character and boolean data
### Step 20: The Assignment Operator ```=```
We were first exposed to the assignment operator while discussing variable assignment.
We are allowed to use expressions to assign values to variables, using code such as ```c = a + b```.
Let's look at a few additional ways assignment can be done.
```java
jshell> int i = 10
i ==> 10
jshell> int j = 15
j ==> 15
jshell> i = j
i ==> 15
jshell>
```
Let's consider the following example:
```java
jshell> i = i * 2
i ==> 30
jshell>
```
The same variable ```i``` appears on both the right-hand-side (*RHS*) and left-hand-side (*LHS*). How would that make any sense?
The expression on the *RHS* is *independently* evaluated first. The value we get is next copied into the memory slot of the *LHS* variable.
Using the logic above, the assignment ```i = i * 2``` actually updates the value of ```i``` (initially ```15```) to ```30``` (doubles it).
Java also does the right thing when it encounters the puzzling statement ```i = i + i```, where ```i``` is all over the place! The code doubles the value of ```i```, and this reflects on the ```JShell``` output.
```java
jshell> i = i + i
i ==> 60
```
The next example is self explanatory.
```
jshell> i = i - i
i ==> 0
```
#### Variable Increment and Decrement
We have already seen increment by assignment:
```java
jshell> int i = 0
i ==> 0
jshell> i = i + 1
i ==> 1
jshell> i = i + 1
i ==> 2
jshell> i = i + 1
i ==> 3
jshell>
```
Conversely, the statement ```i = i - 1``` is called a variable **decrement**. This can also be done repeatedly, and ```i``` changes value every time.
```java
jshell> i = i - 1
i ==> 2
jshell> i = i - 1
i ==> 1
jshell> i = i - 1
i ==> 0
jshell>
```
#### Summary
In this step, we:
* Looked at the assignment operator ```=``` in a more formal way
* Looked at heavily used scenarios for assignment, such as increment and decrement
### Step 21: Assignment Puzzles, and *PMT-Challenge* revisited
Now that we understand how to define, assign to, and use variables in expressions, it's time to push the boundaries a bit. The following examples explore how we could play around with variables in our code.
##### Snippet-1 : Pre- and Post- Increment and Decrement
There are two short-hand versions for increment. ```number++``` denotes *post-increment*. Conversely, ```++number``` means **pre-increment**.
Operator ```++``` can be applied only to variables of integer types, which are ```byte```, ```short```, ```int``` and ```long```.
```number++``` is equivalent to the statement ```number = number + 1```
```java
jshell> int number = 5
number ==> 5
jshell> number++
$1 ==> 5
jshell> number
number ==> 6
jshell> ++number
$2 ==> 7
jshell> number
number ==> 7
jshell>
```
```number--``` is *post-decrement*. On the other hand, ```--number``` is **pre-decrement**.
```java
jshell> number--
$3 ==> 7
jshell> number
number ==> 6
jshell> --number
$4 ==> 5
jshell> number
number ==> 5
jshell>
```
***What is the difference between prefix and postfix vesions?***
Although both prefix and postfix versions achieve the same visible result in above examples, there is a slight difference. We will explore this later.
##### Snippet-2 : Compound Assignment Operators
The compound assignment operator combines the ```=``` with a numeric operator.
```i += 2``` : Add ```2``` to the current value of ```i```
```java
jshell> int i = 1
i ==> 1
jshell> i = i + 2
i ==> 3
jshell> i += 2
$1 ==> 5
jshell>
```
```i -= 1``` : Subtract ```1``` from the current value of ```i```
```java
jshell> i
i ==> 5
jshell> i -= 1
$2 ==> 4
jshell> i
i ==> 4
jshell>
```
```i *= 4``` : Multiply ```i``` with ```4```, and store the result back into ```i```
```java
jshell> i *= 4
$3 ==> 20
jshell> i
i ==> 20
jshell>
```
```i /= 4``` : Divide ```i``` by ```4```, and store the result back into ```i```
```java
jshell> i /= 4
$4 ==> 5
jshell> i
i ==> 5
jshell>
```
```i %= 2``` : Divide ```i``` by ```2```, and store the **remainder** back into ```i```
```java
jshell> i %= 2
$5 ==> 1
jshell> i
i ==> 1
jshell>
```
#### Summary
In the step, we:
* Looked at the built-in Java increment and decrement operators
* Explored the side-effects of prefix and postfix versions of these operators
* Seen the advantages of compound assignment
### Step 22: Some ```JShell``` Usage Tips
* Shortcuts
1. Toggling to limits of current prompt entry
* '```Ctrl+a```' : to start of line
* '```Ctrl+e```' : to end of line
2. Moving up and down history of completed prompt inputs
* *up-arrow* key : move back in time
* *down-arrow* key : move forward in time
3. Reverse-keyword-search
* Search for a term, in reverse order of JShell prompt inputs history : input '```Ctrl+r```'
* Scrolling within matches (in reverse order of history) : repeatedly input '```Ctrl+r```'
* ```/exit``` : resets and exits the JShell session, clearing all stored variables values.
* JShell internal variables : ```$1```, ```$2```, and so on.
* When expressions involving literals and/or previously declared variables are entered as-is, without assignment, those expressions are still evaluated. The result is stored in such an internal variable, and is available for use until the JShell session is reset.
* Variable Declaration rules relaxation
1. Single Java statements need not be terminated with the separator '```;```'.
2. If multiple statements are entered on a single line at the prompt, successive statements must be separated by the '```;```'. However, the trailing '```;```' can still be omitted.
#### Summary
In this step, we:
* Explored some useful keyboard shortcuts for ```JShell``` commands
* Understood the idea behind ```JShell``` internal variables
* Observed how ```JShell``` reduces typing effort, by relaxing some coding rules
### Step 23: Introducing Conditionals - the ```if```
The *PMT-Challenge* needs us to print a total of 10 table entries. The ```for``` **loop** is a suitable iteration mechanism to get this done. The word *loop* means exactly what the English dictionary says.
*As ```i``` varies from ```1``` through ```10```, do some stuff involving ```i```*
For the *PMT-Challenge*, ```do some stuff``` would be the call to ```System.out.printf()```.
The way a ```for``` loop is structured is:
```java
for(initialization; condition; update) {
statement1;
statement2;
}
```
Here's how above code runs
1. Execute ```initialization``` code.
2. If ```condition``` evaluates to true, execute ```statements```. Else, go out of loop.
3. Execute ```update```
4. Repeat 2 and 3
***Does it sound complex?*** Let's break it down.
Let's start with understanding what ```condition``` is all about.
#### Logical Operators
```condition``` represents a logical value(a ```true``` or a ```false```).
Turns out Java has a class of **logical operators**, which can be used with operands within **logical expressions**, evaluating to a ```boolean``` value.
While in school, you probably used the ```=``` symbol to compare numbers.
***The world of Java is a little different.*** ```=``` is assignment operator. ```==``` is the comparison operator.
```java
jshell> int i = 10
i ==> 10
jshell> i == 10
$1 ==> true
jshell> i == 11
$2 ==> false
```
These are other comparison operators as well, such as ```<``` and ```>```.
```java
jshell> i < 5
$3 ==> false
jshell> i > 5
$4 ==> true
```
```<=``` and ```>=``` are simple extensions.
```java
jshell> i <= 5
$4 ==> false
jshell> i <= 10
$5 ==> true
jshell> i >= 10
$6 ==> true
jshell>
```
#### Conditionals: The "if" Statement
We would want to execute specific lines of code only when a condition is true.
Enter ```if``` statement.
An ```if``` statement is structured this way:
```java
if (condition) {
statement;
}
```
```statement``` is executed only if ```condition``` evaluates to ```true```.
Typically, all the code that we write is executed always.
In the example below, ```i is less than 5``` is always printed.
```java
jshell> int i = 10
i ==> 10
jshell> System.out.println("i is less than 5")
i is less than 5
```
Using the ```if``` statement, we can control the execution of ```System.out.println("i is less than 5");```.
```java
jshell> if (i < 5)
...> System.out.println("i is less than 5");
jshell>
```
The condition ```i < 5``` will evaluate to ```false```, since ```i``` is currently ```10```. Nothing is printed to console.
Let's change ```i``` to ```4``` and execute the ```if``` condition.
```java
jshell> i = 4
i ==> 4
jshell> if (i < 5)
...> System.out.println("i is less than 5");
i is less than 5
jshell>
```
```i is less than 5``` is printed to console.
By controlling the value stored in the variable ```i```, we are able to control whether the statement ```System.out.println("i is less than 5");``` actually runs.
***Hurrah! We just achieved *conditional execution*!***
Just as we can compare a variable with a literal, it is possible to compare the values of two variables. The same set of comparison operators, namely ```==```, ```<```, ```>```, ```<=``` and ```>=``` can be used.
```java
jshell> int number1 = 5
number1 ==> 5
jshell> int number2 = 7
number2 ==> 7
jshell> if (number2 > number1)
...> System.out.println("number2 is greater than number1");
number2 is greater than number1
jshell> number2 = 3
number2 ==> 3
jshell> if (number2 > number1)
...> System.out.println("number2 is greater than number1");
jshell>
```
#### Summary
In this step, we:
* Understood the need for a conditionals
* Were introduced to the concept of logical expressions, and conditional operators
* Explored usage of the Java ```if``` statement
### Step 24: Programming Exercise PE-04
- - -
1. Create four integer variables ```a```, ```b```, ```c``` and ```d```. Write an ```if``` statement to print if the sum of ```a``` and ```b``` is greater than the sum of ```c``` and ```d```.
2. Store three numerical values as proposed angles of a triangle in integer variables ```angle1```, ```angle2``` and ```angle3```. Create an ```if``` statement to state whether these three angles together can form a triangle.
_Hint: A triangle is a closed geometric figure with three angles, whose sum must exactly equal ```180 degrees```._
3. Have a variable store an integer. Create an ```if``` statement to find out if it's an even number.
_Hint: Use operator ```%```._
### Step 25: Solution to PE-04
#### Solution 1
```java
jshell> int a = 5
a ==> 5
jshell> int b = 7
b ==> 7
jshell> int c = 4
c ==> 4
jshell> int d = 3
d ==> 3
jshell> if (a + b > c + d)
...> System.out.println("a and b together tower above c plus d");
a and b together tower above c plus d
jshell>
```
#### Solution 2
```java
jshell> int angleOne = 55
angleOne ==> 55
jshell> int angleTwo = 65
angleOne ==> 55
jshell> int angleThree = 60
angleOne ==> 55
jshell> if (angleOne + angleTwo + angleThree == 180)
...> System.out.println("The three angles together form a triangle");
The three angles together form a triangle
jshell> angleThree = 75
angleOne ==> 55
jshell> if (angleOne + angleTwo + angleThree == 180)
...> System.out.println("The three angles together form a triangle");
jshell>
```
#### Solution 3
```java
jshell> int num = 10
num ==> 10
jshell> if (num % 2 == 0)
...> System.out.println("The number is even");
The number is even
jshell> num++
num ==> 11
jshell> if (num % 2 == 0)
...> System.out.println("The number is even");
jshell>
```
### Step 26: ```if``` Statement again
Let's look at a few more examples of if statements.
##### Snippet-1
Here's a basic example of conditional execution.
```java
jshell> int i = 5
i ==> 5
jshell> if (i == 5)
...> System.out.println("i is odd");
i is odd
```
Let's add two statements to the second line.
```java
jshell> if (i == 5)
...> System.out.println("i is odd"); System.out.println("i is prime");
i is odd
i is prime
jshell>
```
Both text messages are printed out.
Let's change value of ```i``` to ```6``` and execute again.
```java
jshell> i = 6
i ==> 6
jshell> if (i == 5)
...> System.out.println("i is odd"); System.out.println("i is prime");
i is prime
jshell>
```
```i is prime``` is printed to console. Why?
Why is this statement executed when the condition is ```false```?
The ```if``` statement, by default, binds only to the next statement.
It does not control the execution of ```System.out.println("i is prime");```, which in fact runs unconditionally, always.
Is there a way we can ensure conditional execution of a two statements? And more than two?
##### Snippet-2 : ```if``` with statement blocks
We seem to have pulled a rabbit out of the hat here, haven't we! The rabbit lies in the pair of *braces* : '```{```' and '```}```'.
They are used to group a sequence of statements together, into a **block**. Depending on the condition value (```true``` or ```false```), either all the statements in this block are run, or none at all.
```java
jshell> int i = 5
i ==> 5
jshell> if (i == 5) {
...> System.out.println("i is odd");
...> System.out.println("i is prime");
...> }
i is odd
i is prime
jshell> i = 6
i ==> 6
jshell> if (i == 5) {
...> System.out.println("i is odd");
...> System.out.println("i is prime");
...> }
jshell>
```
It is considered good programming practice to *always* use blocks in an ```if``` conditional. Here is an example:
```java
if (i == 5) {
System.out.println("i is odd");
}
```
A block can also consist of a single statement! This improves readability of code.
#### Summary
In this step, we:
* Saw the importance of using statement blocks with ```if``` conditionals
* Understood how control flow can be made more readable
### Step 27: Introducing Loops: The ```for``` Statement
The *PMT-Challenge* needs us to print a total of 10 table entries. The ```for``` **loop** is a suitable iteration mechanism to get this done.
The word *loop* means exactly what the English dictionary says.
*As ```i``` varies from ```1``` through ```10```, do some stuff involving ```i```*
```for``` loop is built this way:
```java
for(initialization; condition; update) {
statement1;
statement1;
}
```
***Here's how above code runs:***
1. Execute ```initialization``` code.
2. If ```condition``` evaluates to true, execute ```statements```. Else, go out of loop.
3. Execute ```update```
4. Repeat 2 and 3
We've already seen these components in isolation:
* _initialization_ : ```int i = 1```
* _condition_ : ```i <= 10```
* _update_ : ```i++```
Let's now put them together to get the bigger picture. Here is how it might look:
```java
for (int i = 1; i <= 10; i++) {
System.out.println("Hello World");
}
```
This loop, when executed, prints the message "Hello World" on a separate line, for a total of ```10``` times.
##### Snippet-1 : *PMT-Challenge* Solution
We need to replace the "Hello World" message with the console print of a table entry. This print is controlled by ```i``` taking values from ```1``` through ```10```.
```java
jshell> int i
i ==> 0
jshell> for (i=0; i<=10; i++) {
...> System.out.printf("%d * %d = %d", 5, i, 5*i).println();
...> }
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 2
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
```
##### Snippet-1 Explained
1. The first step is the initialization: ```i = 1```.
2. After this, the statement ```System.out.printf("%d * %d = %d", 5, i, 5*i).println();``` is executed once.
3. Then, the update occurs: ```i++```.
4. Immediately after, the condition ```i<=10``` is evaluated. It returns ```true```. Progress happens. Since it is a ```for``` *loop*, the statement is executed again.
5. Thereafter, this sequence is executed until ```i``` gets a value of ```11``` (due to successive updates).
6. The moment ```i``` reaches ```11```, , the condition becomes ```false```. The looping within the ```for``` terminates.
Meanwhile, the Multiplication Table for ```5```, for entries ```1``` through ```10``` has been displayed!
Now, wasn't that really elegant? It sure was!
Let's pat ourselves on the back for having reached this stage of learning. This elegant, yet powerful technique (*loops*) is used in almost every Java program that's written.
#### Summary
In this step, we:
* Observed why we need a looping construct, such as the ```for```
* Understood the mechanism of a ```for``` loop
* Used a ```for``` loop in iteration, to solve our *PMT-Challenge*
### Step 28: Programming Exercise PE-05
1. Repeat the entire process at arriving at the Multiplication Table Print problem, now for the number ```7```. Start with a fresh ```JShell``` session, if you're still using an existing one for quite some time (Rinse, and repeat!).
2. Use the final solution to print Multiplication Tables for ```6``` and ```10```.
3. Print the integers from ```1``` to ```10```.
4. Print the integers from ```10``` to ```1```.
5. Print the squares of the integers from ```1``` to ```10```.
6. Print the squares of the first ```10``` even integers.
7. Print the squares of the first ```10``` odd integers.
### Step 29: Solution to PE-05
#### Solution 2
```java
jshell> int i
i ==> 0
jshell> for (i=0; i<=10; i++) {
...> System.out.printf("%d * %d = %d", 6, i, 6*i).println();
...> }
6 * 1 = 6
6 * 2 = 12
6 * 3 = 18
6 * 4 = 24
6 * 5 = 30
6 * 6 = 36
6 * 7 = 42
6 * 8 = 48
6 * 9 = 54
6 * 10 = 60
jshell> i = 0
i ==> 0
jshell> for (i=0; i<=10; i++) {
...> System.out.printf("%d * %d = %d", 10, i, 10*i).println();
...> }
10 * 1 = 10
10 * 2 = 20
10 * 3 = 30
10 * 4 = 40
10 * 5 = 50
10 * 6 = 60
10 * 7 = 70
10 * 8 = 80
10 * 9 = 90
10 * 10 = 100
```
#### Solution 3
```java
jshell> for (int i=1; i<=10; i++) {
...> System.out.printf(i).println();
...> }
1
2
3
4
5
6
7
8
9
10
```
#### Solution 4
This is the first time we are using ```i--```. Isn't this interesting?
```java
jshell> for (int i=10; i>0; i--) {
...> System.out.printf(i).println();
...> }
10
9
8
7
6
5
4
3
2
1
```
#### Solution 5
```java
jshell> for (int i=1; i<=10; i++) {
...> System.out.printf(i*i).println();
...> }
1
4
9
16
25
36
49
64
81
100
```
#### Solution 6
```update``` of a for loop can do a lot of things. Here, we are using ```i += 2```.
```java
jshell> for (int i=2; i<20; i += 2)
...> System.out.printf(i*i).println();
...> }
4
16
36
64
100
144
196
256
324
400
```
#### Solution 7
```java
jshell> for (int i=1; i<=19; i += 2) {
...> System.out.printf(i*i).println();
...> }
1
9
25
49
81
121
169
225
289
361
```
### Step 30: Puzzling You With ```for```
In the conceptual form of the ```for``` construct:
```java
for (initialization; condition; updation) {
statement;
statement;
//...
statement;
}
```
It may surprise you that each one of *initialization*, *condition*, *updation* and *statements block* is **optional**. They can all be left out individually, in combination, or all altogether!! Let's examine a few interesting cases in code.
1. Empty initialization, Empty Statement
Increments the control variable in quick succession until the condition evaluates to false.
```java
jshell> int i = 1
i ==> 1
jshell> for (; i<=10; i++);
jshell> i
i ==> 11
jshell>
```
2. Multiple initializations, Empty Statement
You can have multiple statements in ```initialization``` and ```update``` separated by ```,```.
```java
jshell> int j
i ==> 11
jshell> for (i=1, j=2; i<=10; i++, j++);
jshell> i
i ==> 11
jshell> j
j ==> 12
jshell>
```
In the example below, i is incremented and j is decremented.
```java
jshell> for (i=1, j=2; i<=10; i++, j--);
jshell> i
i ==> 11
jshell> j
j ==> -8
jshell>
```
3. Infinite Loop
An infinite loop is one where the *condition* is left *empty*. An empty condition always evaluates to ```true```. Since a ```for``` loop only terminates when the condition becomes ```false```, such a loop this never terminates.
It can only be terminated externally with a keyboard interrupt (```CTRL + c```).
```java
jshell> for (;;);
^C
jshell>
```
4. Statement Block in for
As in case of the ```if``` conditional statement, we can have statement blocks in ```for``` loops as well. As before, we enclose the statement set between braces ('```{```' and '```}```'). The statements are executed repeatedly, in the same order as they appear in the block.
```java
jshell> for (i=1; i<=5; i++) {
...> System.out.println("No 1");
...> System.out.println("No 2");
...> }
No 1
No 2
No 1
No 2
No 1
No 2
No 1
No 2
No 1
No 2
```
#### Summary
In this step, we saw that all components of a ```for``` loop are optional:
* *initialization*
* *condition*
* *updation*
* *statement block*
### Step 31: A Review Of Basic Concepts
Before we go further, let's quickly get a Big Picture of what we are doing here!
A **computer** is a *machine* that does a job for you and me. It is can be used to run tasks that we find complicated, time-consuming, or even boring! For instance, a laptop can play music from a CD, videos from the web, or fire a print job.
We have mobile phones around us, which are mini versions of computers. Then there are others, ranging from blood-sugar monitors to weather-forecast systems. Computers surround us, wherever we go!
Any computer is made up of two basic layers:
* The **hardware**: Consists of all the *electronic* and *mechanical* parts of the computer, including the *electronic circuits*.
* The **software**: Describes the *instructions* fed into the computer, which are stored and run on its *hardware*.
If the human body were a computer,
* Its *hardware* would consist of the various organs (such as limbs, blood and heart)
* Its *software* would be the signals from the nervous system, which drive these organs.
**Computer programming** involves writing software instructions to run tasks on a computer. The user who gives these instructions is called the **programmer**. Often, computer programming involves solving challenging, and very interesting problems.
In the previous steps, we introduced you to the basic Java language concepts and constructs.
We solved a programming challenge, the *PMT-Challenge* using basic Java constructs.
We used JShell REPL to learn the following concepts, Step by-step:
* Expressions, Operators and Statements
* Variables and Formatted Output
* Conditionals and Loops
At each step, we applied fresh knowledge to enhance our solution to the *PMT-Challenge*
Hope you liked the journey thus far. Next sections delve deeper into Java features, using the same Step by-step approach. We will catch up again soon, hopefully!
## Understanding Methods
Feeling good about where you are right now? You just created an elegant, yet powerful solution to the *PMT-Challenge*, using:
* Operators, variables and expressions
* Built-in formatted output
* Conditionals for control-flow, and
* Iteration through loops
And guess what we ended up with? A good-looking display of a Multiplication Table! There's a good chance people in your circles want to see it, use it and maybe share it among their friends.
However, some might be unhappy that it works only for ```5```, and not for ```8``` and ```7```. Maybe it would, but then they would need to type in those lines of code again. This might disappoint them, and your new found popularity would slide downhill.
Surely a more elegant solution exists, a pattern waiting to unravel itself.
Exist it does, and the mechanism to use it lies with Java **methods**. A ```method``` is a feature that allows you to group together a set of statements, and give it a name. This name represents the *functionality* of that set, which can be re-used when necessary.
A method is essentially a *routine* that performs a certain task, and can do it any number of times it is asked to. It may also return a result for the task it performs. The syntax for a ***method definition*** is along these lines:
```
ReturnType methodName () {
method-body
}
```
Where
* ```methodName``` is the name of the routine
* ```method-body``` is the set of statements
* a pair of braces ```{``` and ```}``` enclose ```method-body```
* ```ReturnType``` is the type of ```methodName```'s return value
#### Summary
In this step, we:
* Examined the need for labeling code, and its reuse
* Learned that a Java method fits the bill
* Saw how a method definition looks like, conceptually
### Step 01 : Defining A Simple Method
We will start off, by writing a simple method that prints "```Hello World```" twice on your screen.
```java
jshell> void sayHelloWorldTwice() {
...> System.out.println("Hello World");
...> System.out.println("Hello World");
...> }
| created method sayHelloWorldTwice()
```
A little bit of theory:
* Above code for the method is called **method definition**.
* ```void``` indicates that the method does not return any computed result - We will examine return values from methods, a little later.
***When the code ran, it didn't exactly welcome us twice, did it?*** All we got was a boring message from ```JShell```, mumbling ```created method sayHelloWorldTwice()```.
That's because defining a method ***does NOT*** execute its statement body.
Since the statement block is not stand-alone anymore, its functionality needs to be **invoked**. This is done by writing a **method call**.
Let's look at a few examples for **method calls**.
```java
jshell> sayHelloWorldTwice
| Error:
| cannot find symbol
| symbol: variable sayHelloWorldTwice
| sayHelloWorldTwice
| ^----------------^
jshell> sayHelloWorldTwice()
Hello World
Hello World
```
All that we had to do was to add parantheses to name of the method.
> The trailing '```;```' can be left out in ```JShell```, and is another example of syntax rules being relaxed.
> Method names need to follow the same rules as variable names.
#### Summary
In this step we:
* Got our hands dirty coding our first method
* Learned that defining and invoking methods are two different steps
### Step 02: Exercise-Set PE-01
Let's now reinforce our basic understanding of methods, by trying out a few exercises.
#### Exercise Set -5
1. Write and execute a method named ```sayHelloWorldThrice``` to print ```Hello World``` thrice.
2. Write and execute a method that prints the following four statements:
```I've created my first variable```
```I've created my first loop```
```I've created my first method```
```I'm excited to learn Java```
#### Solutions to PE-01
#### Solution-1
```java
jshell> void sayHelloWorldThrice() {
...> System.out.println("Hello World");
...> System.out.println("Hello World");
...> System.out.println("Hello World");
...> }
| created method sayHelloWorldThrice()
jshell> sayHelloWorldThrice()
Hello World
Hello World
Hello World
```
#### Solution-2
```java
jshell> void sayFourThings() {
...> System.out.println("I've created my first variable");
...> System.out.println("I've created my first loop");
...> System.out.println("I've created my first method");
...> System.out.println("I'm excited to learn Java");
...> }
| created method sayFourThings()
jshell> sayFourThings()
I've created my first variable
I've created my first loop
I've created my first method
I'm excited to learn Java
```
### Step 03: Editing A Method Definition (```Jshell``` Tips)
##### Snippet-01: Editing sayHelloWorldTwice()
```java
jshell> void sayHelloWorldTwice() {
...> System.out.println("Hello World");
...> System.out.println("Hello World");
...> }
| created method sayHelloWorldTwice()
```
The ```/methods``` command lists out the methods defined in the current session.
```
jshell> /methods
| void sayHelloWorldTwice()
jshell>
```
The ```/list``` command lists the code of the specified method.
```java
jshell> /list sayHelloWorldTwice
59 : void sayHelloWorldTwice() {
System.out.println("HELLO WORLD");
System.out.println("HELLO WORLD");
}
jshell>
```
The ```/edit``` command allows you to modify the method definition, in a separate editor window.
```java
jshell> /edit sayHelloWorldTwice
| modified method sayHelloWorldTwice
jshell> sayHelloWorldTwice()
HELLO WORLD
HELLO WORLD
jshell>
```
The ```/save``` method takes a file name as a parameter. When run, it saves the session method definitions to a file.
```java
jshell> /save backup.txt
jshell> /exit
| Goodbye
in28minutes$>
```
#### Summary
In this step, we explored a few ```JShell``` tips that make life easier for you while defining methods
### Step 04: Methods with Arguments
We wrote the method ```sayHelloWorldTwice``` to say ```Hello World``` twice. In the programming exercise, we wanted to print ```Hello World``` thrice and we wrote a new method ```sayHelloWorldThrice```.
Imagine you're in the Java classroom, where your teacher wants to test your Java skills by saying:
_"I want you to print_ ***```Hello World```*** _an arbitrary number of times"_.
Now, that probably would test your patience as well!
How to write a method to print ```Hello World``` an arbitrary number of times?
The thing to note is the word "arbitrary", which means the method body should have no clue! This number has to come from outside the method, which can only happen with a method call.
External input to a method is given as a **method argument**, or *parameter*.
To support arguments during a call, the concept of a method needs to be tweaked to:
```java
ReturnType methodName(ArgType argName) {
method-body
}
```
The only addition to the concept of a method, is the phrase
```ArgType argName```
within the parentheses. ```argName``` represents the argument, and ```ArgType``` is its type. The next example should clear the air for you.
##### Snippet-01: Method with an argument: Definition
Let's look at a method definition using an argument ```numOfTimes```.
```java
jshell> void sayHelloWorld(int numOfTimes) {
...> }
| created method sayHelloWorld(int)
```
Let's try to call the method.
```java
jshell> sayHelloWorld()
| Error:
| method sayHelloWorld in class cannot be applied to given types;
| required : int
| found : no arguments
| reason : actual and formal argument lists differ in length
| sayHelloWorld(
|^-----------------^
jshell> sayHelloWorld(1)
jshell>
```
Method call must include the same number and types of arguments, that it is defined to have. ```sayHelloWorld()``` is an error because ```sayHelloWorld``` is defined to accept one parameter. ```sayHelloWorld(1)``` works.
However, the method does not do anything. Isn't it sad?
##### Snippet-02 : Passing and accessing a parameter
The next example will show you how method body code can access arguments passed during a call. Not only that, the argument values can be used during computations.
```java
void sayHelloWorld(int numOfTimes) {
System.out.println(numOfTimes);
}
```
`System.out.println(numOfTimes)` prints the value of argument passed.
A method can be invoked many times within a program, and each time different values could be passed to it. The following example will illustrate this fact.
```java
jshell> sayHelloWorld(1)
1
jshell> sayHelloWorld(2)
2
jshell> sayHelloWorld(4)
4
jshell> /edit sayHelloWorld
| modified method sayHelloWorld(int)
jshell>
```
##### Snippet-03: More complex method
Code inside a method can be any valid Java code! For instance, you could write code for iteration, such as a ```for``` loop.
Let's look at an example:
```java
void sayHelloWorld(int numOfTimes) {
for (int i=1; i<=numOfTimes; i++) {
System.out.println(numOfTimes);
}
}
```
In the above example, we printed ```numOfTimes``` a total of ```numOfTimes``` for each method call.
We print ```2``` two times and ```4``` four times.
```java
jshell> sayHelloWorld(2)
2
2
jshell> sayHelloWorld(4)
4
4
4
4
jshell>
```
##### Snippet-4 : Saying "Hello World", Again and again...
We wanted to print "Hello World" multiple times. Let's update the method:
```java
void sayHelloWorld(int numOfTimes) {
for (int i=1; i<=numOfTimes; i++) {
System.out.println("Hello World");
}
}
```
Isn't this cool?
```java
jshell> sayHelloWorld(1)
Hello World
jshell> sayHelloWorld(2)
Hello World
Hello World
jshell> sayHelloWorld(4)
Hello World
Hello World
Hello World
Hello World
jshell>
```
You can now proudly demonstrate this code to your Java instructor. Your program can print "Hello World" an arbitrary number of times!
What started off giving you a headache, will probably keep you in her good books, for the rest of your course!
Armed with this confidence booster, let's now see how Java treats mistakes you may make.
.
##### Snippet-5 : Parameter type mismatch
Java is a strongly typed language, with strict rules laid out for type compatibility. We saw that with variables, and how they play out with expressions and assignments. The same type compatibility rules are enforced by the compiler, when it needs to match the arguments from method calls with method definition.
```java
jshell> sayHelloWorld("value")
| Error:
| incompatible types: java.lang.String cannot be converted to int
| sayHelloWorld("value")
| ^-----^
jshell> sayHelloWorld(4.5)
| Error:
| incompatible types: possibly lossy conversion from double to int
| sayHelloWorld(4.5)
| ^-^
jshell>
```
#### Summary
In this step, we:
* Understood why Java supports method arguments, and how we may use them
* Observed how method arguments lead to convenience and reuse
* Decided to abide by type compatibility for actual arguments
### Step 05: Exercise Set PE-02 (With Solutions)
#### Exercises
1. Write a method ```printNumbers(int n)``` that prints all successive integers from ```1``` to ```n```.
2. Write a method ```printSquaresOfNumbers(int n)``` that prints the squares of all successive integers from ```1``` to ```n```.
#### Solution-01
```java
jshell> void printNumbers(int n) {
...> for(int i=0; i< n; i++) {
...> System.out.println(i);
...> }
...> }
| created method printNumbers(int)
```
**jshell>**
#### Solution 2
```java
jshell> void printSquaresOfNumbers(int n) {
...> for(int i=0; i< n; i++) {
...> System.out.println(i*i);
...> }
...> }
| created method printSquaresOfNumbers(int)
jshell>
```
### Step 07: *PMT-Challenge* Revisited (And Some Puzzles)
A method, in its own right, provides an elegant way to name and reuse a code block. In addition, its behavior can be controlled with parameters.
Can we top-up the existing solution to the *PMT-Challenge*, with even more elegance? You're right, we're talking about:
1. Writing a method to print the multiplication table for 5.
2. Using this method to print the multiplication table for any number.
Want to take a look? Dive right in.
Here's what we did earlier:
```java
jshell> for (int i=1; i<=10; i++) {
...> System.out.printf("%d * %d = %d", 5, i, 5*i).println();
...> }
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
```
Let's wrap this in a method:
```java
void printMultiplicationTable() {
for (int i=1; i<=10; i++) {
System.out.printf("%d * %d = %d", 5, i, 5*i).println();
}
}
```
You can call it to print 5 table.
```java
jshell> printMultiplicationTable()
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
jshell>
```
#### Summary
In this step, we:
* Revisited the *PMT-Challenge* solution we had earlier
* Enclosed its logic within a method definition, ```printMultiplicationTable()```
* Haven't fully explored its power yet!
### Step 08: Methods With Arguments, And Overloading
The real power of a definition such as ```printMultiplicationTable()``` is when we arm it with arguments. Not verbal arguments, but "value" ones. That's right!
We will modify ```printMultiplicationTable()``` to have it accept a parameter, which would make it more flexible.
```java
void printMultiplicationTable(int number) {
for (int i=1; i<=10; i++) {
System.out.printf("%d * %d = %d", number, i, number*i).println();
}
}
```
We can now print the multiplication table of any number, by calling the method ```printMultiplicationTable(number)```.
```java
jshell> printMultiplicationTable(6)
6 * 1 = 6
6 * 2 = 12
6 * 3 = 18
6 * 4 = 24
6 * 5 = 30
6 * 6 = 36
6 * 7 = 42
6 * 8 = 48
6 * 9 = 54
6 * 10 = 60
jshell>
```
Nothing new to explain here. We have only combined stuff you have been learning so far, most recently adding the flavor of methods to the existing code.
Soak in the power, simplicity and elegance of this program, folks!
#### Overloaded Methods
It turns out we can call both versions of the method, `printMultiplicationTable()` and `printMultiplicationTable(5)` :
```java
jshell> printMultiplicationTable()
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
jshell>
```
and
```java
jshell> printMultiplicationTable(5)
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
jshell>
```
You can have multiple methods with same name but different number of parameters. This is called ***method overloading***.
#### Summary
In this step, we:
* Enhanced the logic of ```printMultiplicationTable()``` by passing arguments
* Added flexibility to the *PMT-Challenge* solution
* Were introduced to the concept of method overloading
### Step 09: Methods With Multiple Arguments
It is possible, and pretty useful, to define methods that accept more than one argument.
We have already seen how to call two in-built Java methods, ```Math.max``` and ```Math.min```, which accept 2 arguments each.
Time now to enrich our understanding, by writing one such method ourselves as well.
A method ```void sum(int, int)``` that computes the sum of two integers, and prints their output.
```java
jshell> void sum(int firstNum, int secondNum) {
...> int sum = firstNum + secondNum;
...> System.out.println(sum);
...> }
| created method sum(int, int)
jshell> sum(5, 10)
15
jshell>
```
A method ```void sum(int, int, int)``` that computes the sum of three integers, and prints their output.
```java
jshell> void sum(int firstNum, int secondNum, int thirdNum) {
...> int sum = firstNum + secondNum + thirdNum;
...> System.out.println(sum);
...> }
| created method sum(int,int,int)
jshell> sum(5, 10, 15)
30
```
There are overloaded methods. They have same name ```sum``` and have different number of arguments.
#### Summary
In this step, we:
* Used our understanding of method overloading to define our own methods
### Step 10: Returning From A Method
A method can also be coded to return the result of its computation. Such a result is called a **return value**.
A lot of built-in Java methods have return values, the most notable we have seen being ```Math.min()``` and ```Math.max()```.
```java
jshell> Math.max(15, 25)
$1 ==> 25
jshell> $1
$1 ==> 25
```
```Math.max()``` does return a value, which is stored in the ```JShell``` variable ```$1```.
A return mechanism is quite important, as it provides you the flexibility of:
* Sharing computed results with other code and methods
* Improving the breaking down of a problem, into sub-problems
The next example explains how you can collect a method's return value.
```java
jshell> int max = Math.max(15, 25)
max ==> 25
jshell> max
max ==> 25
jshell>
```
We are storing the return value in a variable ```int max```.
We could define our own method that returns a value, in a similar manner. In the method ```sumOfTwoNumbers``` above, instead of displaying ```sum``` at once, we could ```return``` it to the calling-code.
```java
jshell> int sumOfTwoNumbers(int firstNum, int secondNum) {
...> int sum = firstNum + secondNum;
...> return sum;
...> }
| created method sumOfTwoNumbers(int,int)
jshell> sumOfTwoNumbers(1, 10)
$2 => 11
```
The statement ```return sum;``` is called a **return statement**.
When the code ```sum = sumOfTwoNumbers(1, 10)``` is run, ```sum``` collects the result returned by ```sumOfTwoNumbers()```. You could now print ```sum``` on the console, store it in another variable, or even invoke a different method by passing it as an argument!
```
jshell> sum = sumOfTwoNumbers(1, 10)
sum => 11_
jshell> sum = sumOfTwoNumbers(15, 15)
sum => 30
jshell>
```
#### Summary
In this step, we:
* Understood the need for a return mechanism with methods
* Explored the syntax for a return statement
* Wrote our own method ```sumOfTwoNumbers()``` with a return value, and saw how it became more flexible
### Step 11: Exercise-Set PE-04 (With Solutions)
Let's now try a few exercises on methods that return values.
#### Exercises
1. Write a method that returns the sum of three integers.
2. Write a method that takes as input two integers representing two angles of a triangle, and computes the third angle. *Hint: The sum of the three angles of a triangle is 180 degrees*.
#### Solution-01
```java
jshell> int sumOfThreeNumbers(int firstNum, int secondNum, int thirdNum) {
...> int sum = firstNum + secondNum + thirdNum;
...> return sum;
...> }
| created method sumOfThreeNumbers(int,int, int)
jshell>
```
#### Solution-02
```java
jshell> int getThirdAngle(int firstAngle, int secondAngle) {
...> int sumOfTwo = firstAngle + secondAngle;
...> return 180 - sum;
...> }
| created method getThirdAngle(int,int)
jshell>
```
### Step 12: Java Methods, A Review
In this section, we introduced you to the concept of methods in Java.
* We understood that methods are routines that perform a unit of computation. They group a set of statements together into a block, and need to be run by being invoked, through a method call.
* We can define methods that accept no arguments, a single argument and multiple arguments.
* A method can also be defined to return results of its computation, and this makes it your program more flexible.
## Understanding the Java Platform
JShell is amazing to start with the basics of Java programming. It abstracts all complexities behind writing, compiling and running Java code.
What's happening behind the screens? Let's find out.
### Step 01: The Java Platform - An Introduction
A computer only understands binary code, which are sequences of ```0```’s and ```1```’s (called **bits**). All instructions to a computer should be converted to ```0```’s and ```1```’s before execution.
***When we wrote our code in JShell, how was it converted to binary code?***
We write our programs in a high-level language, such as Java, as it is easier to learn, remember and maintain.
Who converts it to binary code?
Typically, **Compiler** is a program which understands the syntax of your programming language and converts it into binary code.
Java designers wanted it to be Platform independent. Compile the code once and run it anywhere.
However, different Operating Systems have different instruction sets - different binary code.
Java provides an interesting solution:
- All Java compilers translate source code to an **intermediate representation** ( **bytecode** ).
- To run Java programs (**bytecode**), you need a ***JVM*** (Java Virtual Machine).
- ***JVM*** understands **bytecode** and runs it.
- There are different JVM's for different operating systems. Windows JVM converts **bytecode** into Windows executable instructions. Linux JVM converts **bytecode** into Linux executable instructions.
The Java compiler translates Java source code to **bytecode**, which is stored as a **.class** file on the computer.
#### Summary
In this step, we:
* Understood the role of the Java compiler in translating source code
* Realized the need for an Intermediate Representation (IR) such as bytecode
* Understood how the (Compiler + JVM + OS) combination ensure source code portability
### Step 02: Creating a Java ```class```
First, let's understand the concept of a `class`.
Let's consider an example:
A **Country** is a *concept*. **India**, **USA** and **Netherlands** are *examples* or *instances* of **Country**.
Similarly, a `class` is a template. `Objects` are `instances` of a `class`.
We will discuss more about `class` and `object` in the section on Object Oriented Programming.
The syntax to create a class, and its objects, is very simple.
```java
jshell> class Country {
...> }
created class Country
```
Let's create an instance of the class:
```java
jshell> Country india = new Country();
india ==> Country@6e06451e
```
The syntax is simple - `ClassName objectName = new ClassName()`.
```india``` is an object of type ```Country```, and is stored in memory at a location indicated by ```6e06451e```.
Let's create a few more instances of the class:
```java
jshell> Country usa = new Country();
usa ==> Country@6e1567f1
jshell> Country netherlands = new Country();
netherlands ==> Country@5f134e67
jshell>
```
```india```, ```usa``` and ```netherlands``` are all different objects of type ```Country```.
We actually left the contents of the ```class``` ```Country``` bare.
A ```class``` does often include both data (member variables) and method definitions. More on this at a relevant time.
Let's consider another example:
```java
jshell> class Planet {
...>}
created class Planet
jshell> Planet planet = new Planet();
planet ==> Planet@56ef9176
jshell> Planet earth = new Planet();
earth ==> Planet@1ed4004b
jshell> Planet planet = new Planet();
venus ==> Planet@25bbe1b6
```
We can also create a ```class``` for a different concept, like a ```Planet```.
```Planet``` is now a different template. ```planet```, ```earth``` and ```venus``` are instances of `Planet` class.
#### Summary
In this step, we:
* Saw how ```class``` creation actually creates a new type
* Understood how to create instances of classes - objects
### Step 03: Adding A Method To A ```class```
A method defined within a ```class```, denotes an action than can be performed on objects of that ```class```.
Let's define a method inside ```Planet``` - to ```revolve()```!
```java
jshell> class Planet {
...> void revolve() {
...> System.out.println("Revolve");
...> }
...> }
replaced class Planet
update replaced variable planet, reset to null
update replaced variable earth, reset to null
update replaced variable venus, reset to null
```
Syntax of `revolve()` is similar to other methods we've created before.
> The ```class``` template got updated as a result of adding new method.
> Earlier instances ```planet```,```earth``` and ```venus``` got reset to ```null``` as they are based on the old template
Let's re-instantiate a few objects.
```java
jshell> Planet earth = new Planet();
earth ==> Planet@192b07fd
jshell> Planet planet = new Planet();
venus ==> Planet@64bfbc86
jshell>
```
How do we call the ```revolve``` method?
```java
jshell> Planet.revolve();
| Error:
| non-static method revolve() cannot be referenced from a static context
| Planet.revolve();
|^-----------------^
jshell> earth.revolve();
Revolve
jshell> venus.revolve();
Revolve
jshell>
```
Our attempt to perform ```revolve()``` did not work with the syntax ```Planet.revolve()``` as it is not a static method (more on that in a later section).
Invoking ```revolve()``` through syntax such as ```earth.revolve()``` succeeds, because ```earth``` is an object of type ```Planet```.
#### Summary
In this step, we:
* Learned how to add a method definition to an existing ```class```
* Discovered how to invoke it, on objects of the ```class```
### Step 04: Writing and Running Java Code in separate Source Files
So far, we have enjoyed creating classes and defining methods within them, all with our friendly neighborhood ```JShell```.
```JShell``` kind of spoiled us kids, by relaxing some Java syntax rules, invoking the compiler on our behalf, and also running the code like room service.
The time has come for us frog princes to outgrow the proverbial well. Yes, you're right! Time to start writing code in a proper code editor, and smell the greener grass on that side.
Let's start with creating a new source code file - 'Planet.java'. Choose any folder on your hard-disk and create the file shown below:
_**Planet.java**_
```java
class Planet {
void revolve() {
System.out.println("Revolve");
}
}
```
> You can get the code that we wrote earlier in JShell for ```Planet``` by running the command ```/list Planet```, and copying the code.
> You can use any text editor of your choice, for now.
The source file name must match the ```class``` name it contains. Here, we must name the file `Planet.java`.
The next course of action would be to compile this code (Stage 2). For that, exit out of ```JShell```, and run back to your system terminal or command prompt. Now, see what plays out!
```java
jshell> /exit
| Goodbye
```
```cd``` to the folder where you have created the file `Planet.java`
```
in28minutes$> cd in28Minutes/git/JavaForBeginners/
command-prompt> ls
Planet.java
```
Check Java version.
```
command-prompt> java -version
java version "x.0.1"
Java(TM) SE Runtime Environment (build x.0.1+11)
Java HotSpot(TM) 64-bit Server VM (build x.0.1+11, mixed mode)
```
You can compile java code using ```javac Planet.java```. You can see that a new file is created `Planet.class`. This contains your `bytecode`.
```
command-prompt> javac Planet.java
command-prompt> ls
Planet.class Planet.java_
```
You can run the class using command `java Planet`.
```java
command-prompt> java Planet
Error: Main method not found inside class Planet, please define the main method as
public static void main(String[] args)
or a JavaFX application class must extend javax.application.Application
command-prompt>
```
Why is there an error? Let's discuss that in the next step.
#### Summary
In this step, we:
* Ventured out of ```JShell``` into unexplored territory, to write source code
* Learned how to invoke the compiler to compile source files, from the terminal
### Step 05: Introducing ```main()```, And Running Bytecode
In the previous step, we compiled **_Planet.java_**. In this step, let's run the bytecode generated, which lies inside **_Planet.class_**.
##### Snippet-01: Running ```Planet```
**_Console Commands_**
```java
command-prompt> ls
Planet.class Planet.java
command-prompt> java Planet
Error: Main method not found inside class Planet, please define the main method as
public static void main(String[] args)
or a JavaFX application class must extend javax.application.Application
command-prompt>
```
The code may have compiled without any errors, but what's the use if we cannot run the program to see what stuff it's got!
**_The ```main()``` method is essential to run code defined within any ```class```._**
The definition of ```main()``` needs to have this exact synatx:
`public static void main(String[] args) { /* */ }`
A few details:
* `void` is its return type
* `main` is its name
* `args` is its formal argument. Here, it's an **array** of ```String```.
* `public``` and ```static``` are reserved Java keywords. More on these later sections.
Let's add one such definition within the ```Planet``` code. Open up your text editor and add the ```main``` method as shown below.
**_Planet.java_**
```java
class Planet {
void revolve() {
System.out.println("Revolve");
}
public static void main(String[] args) {
}
}
```
Let's run it now:
```java
command-prompt> java Planet
Error: Main method not found inside class Planet, please define the main method as
public static void main(String[] args)
or a JavaFX application class must extend javax.application.Application
```
Why is there no change in result?
After changing your Java source file, you need to compile the code again to create a new class file.
```
command-prompt> javac Planet.java
command-prompt> ls
Planet.class Planet.java_
```
Let's try it again.
```
command-prompt> java Planet
command-prompt>
```
We got a blank stare from the terminal. Why?
What did we write in the ```main``` method? Nothing.
```java
public static void main(String[] args) {
}
```
Let's fix it. We need to edit **_Planet.java_** once again!
**_Planet.