JavaProgrammingForBeginners CourseBook 61 70
JavaProgrammingForBeginners CourseBook 61 70
Discovered that after every code update, we need to compile that source file
Step 06: Puzzles About Planet
In this step, let's play with Planet source code. Get ready for a comedy of errors, by trial-and-error!
Below is the source code for Planet that worked so well for us:
Planet.java
class Planet {
void revolve() {
System.out.println("Revolve");
}
class Planet {
void revolve() {
System.out.println("Revolve");
}
Console Commands
class Planet {
void revolve() {
System.out.println("Revolve");
}
Console Commands
class Planet {
void revolve() {
System.out.println("Revolve");
}
Console Commands
We misspelled Planet as Plane , and the compiler trapped this error. It shows us a red flag with a message to
help us correct our mistake.
Snippet-04: Messing-up main() - v4
Call a non existing method earth.revolve1(); .
Planet.java
class Planet {
void revolve() {
System.out.println("Revolve");
}
Console Commands
We misspelled the name of the revolve() method, to revolve1() . Rightly, got rapped on our knuckles by the
compiler.
Summary
In this step, we:
Learned that the best way to learn about language features is to play around with working code
Observed that JShell simplifies coding for us, even relaxing some syntax rules
Concluded that developing Java code in code-editors is an empowering feeling!
Step 07: The JVM, JRE And JDK
What are JVM, JRE and the JDK?
Apart from the fact that all start with a 'J', there are important differences in what they contain, and what they do.
The following list gives you a bird's eye view of things.
The JVM runs your program bytecode.
JRE = JVM + Libraries + Other Components
Libraries are built-in Java utilities that can be used within any program you create. System.out.println() was
a method in java.lang , one such utility.
Other Components include tools for debugging and code profiling (for memory management and
performance).
JDK = JRE + Compilers + Debuggers
JDK refers to the Java Development Kit. It's an acronym for the bundle needed to compile (with the compiler)
and run (with the JRE bundle) your Java program.
An interesting way to remember this organization is:
JDK is needed to Compile and Run Java programs
JRE is needed to Run Java Programs
JVM is needed to Run Bytecode generated from Java programs
Let's check a couple of scenarios to test your understanding.
Scenario 1: You give Planet.class file to a friend using a different operating system. What does he need to do to run
it?
First, install JRE for his operating system. Run using java Planet on the terminal. He does not need to run javac, as
he already has the bytecode.
Scenario 2: You gave your friend the source file Planet.java. What does he need to do to run it?
Install JDK of a compatible version. Compile code using javac Planet.java. Run java Planet on his terminal.
In summary
Application Developers need ==> JDK
Application Users need ==> JRE
Summary
In this step, we:
Understood the differences in the scope-of-work done by the JVM, JRE and JDK
Realized how each of them could be invoked by different terminal commands, such as javac and java.
Eclipse
TODO - Need Revision
Introducing Java Software Development with Eclipse
Features of the Eclipse IDE (Integrated Development Environment)
Installation & Configuration
Workspace Creation & Configuration
Project Creation & Organization
JRE Version Selection and Included Libraries
Eclipse Java Perspectives
Source Files Organization into Folders
Packages?
IDE User Interface Description
Perspective
Views
Console Content and Display Options
Creating a new Java class in Eclipse (And related source file)
Package name
A Java solution to solving a problem could be composed of several application components. It is
considered good programming arctice to identify a class for each distinct component. Package is the
Java way to organize classes in source code.
Analogy: Eatables in a Refrigerator (Freezer, Chill-Tray, Vegetable-Tray, Bottle-Rack)
Public method stub : can be used to create a default public static void main(String[] args)
signature
(Include Snapshots)
Class creation pop-up, with selected options
Default generated source code in editor window
Customizing the generated source code to our needs
Syntax Highlighting
Keywords
Built-in Types
Constant literals: numbers, strings
Auto-suggest feature of eclipse as code is being typed in
The "Run as --> Java Application" Option to compile-and-run the source code
Option is avaibale only for classes that have a main() method
Using Eclipse in Debug Mode
Execution of the Multiplication Table Program can be done Step by-step. That is, the entire application dies not
execute at one go, printing the final output onto the console. We can stall its flow at several steps, by taking control
of its execution using the Eclipse IDE. This is possible in a special mode of Eclipse, called Debug Mode.
The process is called Debugging, because this mode is heavily used not just to have fun seeing what happens, but
also to detect and fix software bugs. A bug is a defect in the program being written, that leads to its incorrect
execution. The process of removing bugs is called debugging. Humans being humans, programming errors are but
natural, and a debug mode in the IDE is worth its wight in gold to the programmer. Here is how Eclipse facilitates
debugging of Java applications.
Prior to debugging application, we need to set few Break-Points. A Break-Point is a statement in the source of
the program, before which execution under debug mode is suspended, and control is passed to the
programmer.
The overall state of the data in the suspended program is available to the programmer at that particular break-
point. He/she gets to specify one or more break-points in the program, and when the execution is suspended, a
list of possible actions can be performed, including:
Reading & modifying data variables
Resuming program execution
Re-executing current statement
Skipping all statements till the next break-point
and others.
The Eclipse IDE provides a very user-friendly and intuitive interface to the programmer for controlling execution
of a program in Debug Mode (Provide Snapshots of IDE at various stages of Debug Mode execution).
Setting and Removing a Break-point (also Toggling)
Stepping over a method call, or into and out of a method body (during a method call)
Stepping into int print() and int print(int) and int print(int,int,int) gives us interesting
infromation, but stepping into System.out.printf can freak you out! So, you may want to step over it.
For nested method calls, examine the method call Stack Trace (Call child, call parent relationship)
Example : int print() , int print(int) and int print(int,int,int) method call chain of class
MultiplicationTable .
Observe that execution exits from the for loop when the condition i<=10 is no longer true .
Re-executing a line of code
Skipping execution of all statements till next break-point
Ignoring all remaining breakpoints, resume execution of code till completion
Eclipse IDE Keyboard Shortcuts
Code Text Editor Shortcuts
New Project/Class Creation Shortcuts
Search for a Class
Differences between JShell and IDE
Variables just declared, but used without an initialization, will flag a compilation error in an IDE! But not so, it seems,
in JShell. In regular software, variable initialization at point of declaration (called a "defintion", as we already know)
is mandatory.
Snippet-1 : JShell redefines variables on demand
jshell> int i = 2
$1 ==> 2
jshell> int i = 4
$2 ==> 4
jshell> long i = 1000
$3 ==> 1000
jshell>
When the following code (similar to the one given to JShell) is compiled manually or by IDEs such as Eclipse, it will
flag errors!
package com.in28minutes.firstjavaproject;
package com.in28minutes.firstjavaproject;
public class MultiplicationTable {
public static void print() {
for(int i=1; i<=10;i++) {
System.out.printf("%d * %d = %d", 5, i, 5*i).println();
}
}
}
MultiplicationRunner.java
package com.in28minutes.firstjavaproject;
Console Output
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
Snippet-01 Explained
We have now split the code into two source files:
MultiplicationTable.java: Houses the MultiplicationTable class definition, with some methods we
need.
MultiplicationRunner.java: Acts as the client of the MultiplicationTable class , to invoke its
functionality. It defines a main() method, that instantiates a MultiplicationTable object, and invokes its
print() method.
After this code was rearranged, we still got it to work!
Print-Multiplication-Table: Enhancements
We now want to enhance our current solution to this challenge, even though we've come a long way. "Once
you get the whiff of success, sky is the limit!". Here is the changes we need:
Pass the number whose table needs to be printed, as an argument
Pass the (continuous) range of numbers, whose index entries in the table are to be printed. (For example,
printing the table entries for 6 , with entries for indexes between 15 to 30 ).
One way to achieve all this, would involve overloading the print() method. The next example is one such attempt.
Snippet-02: print() overloaded
Overloading print() works for us, and we now support three ways in which to display any table:
Console Output
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
The default table for 5 , with entries fixed within 1 to 10 .
Console Output
8*1=8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
8 * 10 = 80
Printing a table for any number, but with entries fixed between 1 and 1 .
Console Output
6 * 11 = 66
6 * 12 = 72
6 * 13 = 78
6 * 14 = 84
6 * 15 = 90
6 * 16 = 96
6 * 17 = 102
6 * 18 = 108
6 * 19 = 114
6 * 20 = 120
Displaying a table for any number, with entries for any range
The full code:
MultiplicationTable.java
package com.in28minutes.firstjavaproject;
MultiplicationRunner.java
package com.in28minutes.firstjavaproject;
package com.in28minutes.firstjavaproject;
Console Output
5X1=5