Java Programming Study Notes
❤️ Reference Acknowledgments: A heartfelt gratitude to Professor Christos Chrysoulas for delivering such an amazing and inspiring Java programming course!❤️
Also a special thank you to Teacher Tess Watt for her wonderful academic support and dedication to the students.❤️
I would also like to express my sincere appreciation to the amazing administrative team, especially Miss Debbie Ross and Richard Knight, for their outstanding assistance and continuous support in helping students succeed throughout this Master’s degree program.❤️
Thank you all for your dedication, encouragement, and commitment to our success! 🙏🎓
This review reflects the author's learning and thoughts based on the materials covered in the MSc Computer Science online degree provided by the Heriot-Watt University based in Edinburgh, United Kingdom, delivered on the Coursera platform.
Sharing My Java Tips and Tricks
This section is for Java beginners who are learning the basics of Java syntax. My study notes provide you with simple tips and tricks to help you understand some of the most common syntax differences and confusing concepts that once confused me as a student.
I’ve listed the parts of Java syntax that I found most confusing as I learned them throughout my master’s journey. I also include practical tips that I discovered for solving math problems involving the modulo operator (%) and understanding the Java exception hierarchy.
I hope these study notes can also help you better understand Java (just as they helped me), write more reliable programs, and pass your exams.
Table of Contents
1.1 Modulo Operations using Formula
Solving for the remainder is quite easy when dealing with positive integers. For example:
16 ÷ 5 = 3 with a remainder of 1.
Therefore:
16 % 5 = 1
My Discovery: Formula for solving Remainders
However, as a student, I initially found it challenging when problems involved negative integers, cases where the dividend is smaller than the divisor, or situations where I needed to understand how Java handles the % operator (for example, what is 1 % 10?).
I found it helpful to use this golden formula:
Remainder = Dividend − (Divisor × Integer Quotient)
For example:
1 % 10
The integer quotient is 0, so:
1 − (10 × 0) = 1
Therefore:
1 % 10 = 1
Important: In Java, the % operator calculates the remainder. When integer operands are negative, the remainder has the same sign as the dividend (or is zero). For example:
-16 % 5 = -1
This is because Java's integer division drops the decimal part and rounds the result toward zero:
-16 / 5 = -3
Therefore:
-16 − (5 × -3) = -1
This approach has been a lifesaver for me, especially during exams where programming questions involving loops use the % operator. Understanding how the % operator works is very useful in programming, particularly when working with loops.
1.2 Difference between the == Operator and the .equals() Method
As a student, I easily got confused between the equals operator (==) and the .equals() method. So, let’s break down the difference:
In Java, == and .equals() are used to compare values, but they work differently:
== operator: For objects, it compares whether two references point to the same object in memory. For primitive types, it compares their actual values.
.equals() method: Compares the content or logical value of two objects, depending on how the method is implemented.
For example:
String a = new String("Hello");
String b = new String("Hello");
System.out.println(a == b); // false
System.out.println(a.equals(b)); // trueHere, a and b contain the same text but refer to different String objects. Therefore, == returns false, while .equals() returns true.
In short: Use == to compare primitive values or object references, and .equals() to compare the contents of objects.
1.3 throws Clause vs. Multiple Exceptions in a Single catch Clause
A common Java syntax difference to remember is the separator used between exception types.
1. throws clause → use comma ,
With a throws clause, multiple exception types are separated by commas:
void readFile() throws IOException, SQLException {
// ...
}The comma separates the exception types in a list of exceptions that the method may throw.
This is similar to how commas separate method parameters:
void foo(int x, String y) {
// ...
}2. Multiple exceptions in a single catch clause → use pipe bar |
When handling multiple exception types in a single catch clause, the exception types are separated by a pipe bar |:
try {
// ...
} catch (IOException | SQLException e) {
// Handle either exception
}Here, IOException | SQLException represents alternative exception types that can be handled by the single catch block.
In other words, the same catch block handles either an IOException or a SQLException.
/** Easy trick:
Multiple exception types → one catch clause → use |
In other words:
**/
"Catch an
IOExceptionor aSQLException."
The catch parameter e represents the exception caught from either alternative.
Why are the separators different?
They represent different grammatical constructs in Java:
| Syntax | Separator | Meaning |
|---|---|---|
throws IOException, SQLException | , | A list of exception types the method may throw |
catch (IOException | SQLException e) | | | Multiple alternative exception types handled by one catch block |
So a useful memory trick is:
, = list| = multiple alternative exceptions
The Java Language Specification defines these separately: a throws clause contains a comma-separated list of exception types, while a multi-catch parameter uses | between alternative exception types.
Easy way to remember
throws IOException, SQLException
// ↑
// "and/or list"
catch (IOException | SQLException e)
// ↑
// "or"So:
throws → use comma ,
multiple exceptions in single-catch → use pipe |
For more info, see: Oracle Documentation
1.4 Java Exception Hierarchy
Photo source: Module 10 of the MSc Computer Science course at Heriot-Watt University.
This is the Java Exceptions hierarchy as we learn it in Module 10 of the Java Programming course, which is the first subject of the MSc Computer Science programme.
We can see that Throwable is the superclass of both Exception and Error. Under these two categories, there are numerous subclasses.
Key Takeaway:
Important: I learned that it is very important to be specific when writing exception handlers. For example, if you need to catch only IOException and SQLException, you can use the pipe (|) format we discussed in Section 1.2.
Using `Throwable` might catch more than you expect because `Throwable` is the superclass of both `Exception` and `Error`. As a result, it can catch errors and exceptions that you may not intend to handle.
It is generally better to catch only the specific exceptions that you expect and know how to handle. This makes your code more predictable, easier to understand, and less likely to hide unexpected problems.
