diff --git a/.gitignore b/.gitignore
index 194fce6..341f520 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,6 +5,9 @@
# don't track unpublished builds or stage
output
+# don't track .cache directory
+.cache/*
+
# don't track assets generated from source
generated-assets
**/*.pkl
diff --git a/source/ap-java-cheatsheet.ptx b/source/ap-java-cheatsheet.ptx
new file mode 100644
index 0000000..7e8f94a
--- /dev/null
+++ b/source/ap-java-cheatsheet.ptx
@@ -0,0 +1,204 @@
+
+
+
+
+ The following is intended to be useful in better understanding Java functions coming from a Python background.
+
+
+ Ternary Operator: Provides a compact, one-line if-else statement. For instance,
+ No Chained Comparisons: Java does not support chained comparisons. Range checks must use logical operators, such as
+ String Formatting: Java uses methods like
+ No Tuple or List Unpacking: Java does not have a direct equivalent to Python's tuple and list unpacking. Assignment must be done one variable at a time, such as
+ Short-Circuiting: The logical operators
+ Streams API: Java's Stream API is the idiomatic alternative to Python's List Comprehensions. It can be used to filter, map, and reduce data in a sequence of steps. For a simpler example, to generate a basic list of numbers, instead of a multi-line loop, you can write
+
+
+
+
+
+
+
+ This book assumes that you are already familiar with the
+
+ Data Types +
++ User input and output +
++ Conditionals and Exception Handling +
++ Loops and Iteration +
++ Once we have the basics of Java behind us we will move on to look at more powerful features of the language. +
+ + ++
+ Classes and Interfaces +
++ Recursion +
++ File Handling +
++ Finally, we will look at common errors and how to find the help you need. +
+ ++ However, it's a good idea to also try a Java IDE to build code outside of + this online book. There are many Java IDEs available. If you are enrolled in a course, your instructor will likely recommend one, so you should learn to use that one. +
+
+ To use Github classroom, students need to sign up for a free Github account (
+ To install Java software on your local computer, below are several popular Java IDEs and editors that you can download and install. Please be sure to use the one that is recommended by your instructor if you are enrolled in a course, as they may have specific preferences or requirements. +
+ ++
+
+
+
+
+
+
+
+ There are also a lot of online cloud IDEs where you can code online in many + programming languages. Most are free to start, but offer different + features for a price. These are great options if you are using a + Chromebook or you cannot install software on your computer or you want an + easy no-installation option. +
+ +Here are some popular online IDEs:
++
+ CodeHS (
+ PickCode (
+ Replit (
+ JuiceMind (
+ Thank you to Beryl Hoffman for contributing to this section from her CSAwesome: AP Java Programming book. +
+In
+ Programming languages will always change. + As the field of computer science advances there will be new programming languages and you will need to learn them. + It is important to learn several programming languages so that you know what to expect. + There are certain features that most programming languages have in common; variables, loops, conditionals, functions. + And there are some features that are unique. + If you know what is common in languages that is a good place to start. +
+
+
+ Although Python code is generally slower than Java and C++ code, in practice Python programs can achieve equivalent performance. Performance can be defined as how efficiently software can accomplish its tasks.
+ This can be done by compiling Python code to C code (see:
+ It is easier to learn to create interesting programs in Java than in C or C++, for several reasons: +
+ ++
+ C++’s syntax is more complicated than Java’s, making it more difficult to learn.
+ For example, C++ supports a feature called operator overloading, which makes it possible to change the behavior of operators like
+ Certainly, C and C++ are important languages, and are worth learning. + But for these and other reasons, we’ve decided to use Java for this course. + Learning Java will be a good preparation for learning these and other languages! +
+ + +Learning multiple programming languages helps programmers adapt to different styles and environments.
+Python is a dynamic scripting language that is beginner-friendly, but it is less strict with types and generally slower than compiled languages.
+Languages like Java and C++ are statically typed and offer better performance and maintainability for large-scale projects.
+Java has a simpler syntax than C++ and includes automatic garbage collection, which reduces the complexity of memory management.
+Java’s extensive standard library enables the development of sophisticated programs without relying on external dependencies.
+Which of the following best describes Python as a programming language?
+Statically typed and high-performance
No. This better describes languages like Java or C++.
Dynamically typed and beginner-friendly
That’s right! Python is dynamically typed and easy for beginners.
Industrial strength and verbose
No. Python is more informal and concise.
Memory-managed and pointer-based
No. That describes lower-level languages like C or C++.
Why is Java a better language for beginners compared to C++?
+It requires more manual memory management
No. Java manages memory automatically.
It has a smaller standard library
No. Java has a very large standard library.
It avoids complex syntax and has automatic garbage collection
Correct! These features make Java easier for beginners.
It supports operator overloading
No. That's a C++ feature and it adds complexity.
What is a major benefit of learning multiple programming languages?
+You can standardize all projects using one universal syntax
No. Each language has its own syntax and is suited for different tasks.
You will minimize runtime errors across all platforms
No. Runtime errors depend more on logic and environment than the number of languages you know.
You gain exposure to different language features and paradigms
Great choice! This helps you become a more adaptable and well-rounded programmer.
You can bypass the need for understanding compilation and interpretation
No. Understanding how code is executed remains essential regardless of how many languages you know.
- A time-honored tradition in Computer Science is to write a program called “hello world.” The “hello world” program is simple and easy.
+ A time-honored tradition in Computer Science is to write a program called “Hello World.” The “Hello World” program is simple and easy.
There are no logic errors to make, so getting it to run relies only on understanding the syntax.
- To be clear, lets look at a “complicated” version of hello world for Python:
+ To be clear, lets look at a “complicated” version of hello world for Python in
def main(): print("Hello World!")
+
+ def main():
+ print("Hello World!")
+
+
- Remember that we can define this program right at the Python command line and then run it:
+ Remember that we can define this program right at the Python command line and then run it with
>>> main() "Hello World!" >>>+
+ The command line interface of the program is shown in
+>>> main()
+Hello World!
+ - Now lets look at the same program written in Java: + Now let's look at the same program written in Java:
+
public class Hello {
@@ -31,18 +54,20 @@ public class Hello {
}
- What we see is that at the core there are a few similarities, such as a main and the string “Hello World”. However, there is a lot more stuff around the edges that make it harder to see the core of the program. Do not worry! An important skill for a computer scientist is to learn what to ignore and what to look at carefully. You will soon find that there are some elements of Java that will fade into the background as you become used to seeing them. One thing that will help you is to learn a little bit about Java
$ javac Hello.java
@@ -51,6 +76,7 @@ $ ls -l Hello.*
-rw-r--r-- 1 bmiller bmiller 117 Jul 19 17:46 Hello.java
The command
- Now that we have compiled our java source code we can run the compiled code using the
$ java Hello
@@ -70,6 +97,8 @@ Hello World!
$
Now you may be wondering what good is that extra step? What does compiling do for us? There are a couple of important benefits we get from compiling:
@@ -95,8 +124,8 @@ $
public class Hello {
@@ -162,7 +191,7 @@ public class Hello {
-
public static void main(String[] args)
@@ -252,7 +281,7 @@ public static void main(String[] args)
-
System.out.println("Hello World!");
@@ -276,11 +305,11 @@ System.out.println("Hello World!");
Java statements can spread across many lines, but the compiler knows it has reached the end of a statement when it encounters a
System.out.println("Hello World");
@@ -296,6 +325,7 @@ System.
;
The last two lines of the hello world program simply close the two blocks using
- If we wanted to translate the Java back to Python we would have something like the following class definition.
+ If we wanted to translate the Java back to Python we would have something like
class Hello(object):
@staticmethod
@@ -316,21 +346,32 @@ class Hello(object):
print("Hello World!")
Notice that we used the decorator
+ The command line interface of the program is shown in
>>> Hello.main("")
Hello World!
->>>
+ Construct a complete Java program that prints your name and your favorite color to the console. + Drag the blocks into the correct order on the right. +
++ It is worth pointing out that Java has some very handy naming conventions. It is advisable to both use meaningful names and to follow these naming conventions while developing software in Java for good maintenance and readability of code. +
+ ++
+ Class names should be nouns that are written in UpperCamelCase, namely with the first letter of each word capitalized including the first.
+ For example,
+ Method names use lowerCamelCase which start with a verb that describes the action they perform. This means that method names start with a lower case letter, and use upper case for each internal-word method names. For example,
+ Instance variables of a class start with a lower case letter and use lowerCamelCase like method names. For example,
+ Constants are in all upper case letters or in upper snake case, which also known as screaming snake case, and which is a naming convention in which each word is written in uppercase letters, separated by underscores.
+ For example,
+ Which of the following is a valid variable name according to the core syntax rules of Java, but causes a syntax error in Python? +
++ Incorrect. Leading underscores are valid in both Java and Python (commonly used in Python for private/protected attributes). +
++ Incorrect. Snake_case names with underscores are valid in both languages (and are actually standard convention in Python). +
+
+ Correct! Java allows the dollar sign (
+ Incorrect. Numbers at the end of variable names are valid syntax in both Java and Python. +
+
+ One of the great things about Python is that all of the basic data types are objects.
+ Integers are objects, floating point numbers are objects, lists are objects, everything.
+ In Java that is not the case.
+ In Java, some of the most basic data types like integers and floating point numbers are not objects.
+ The benefit of having these primitive data types be non-objects is that operations on the primitives are fast.
+ The problem is that it became difficult for programmers to combine objects and non-objects in the way that we do in Python.
+ So, eventually all the non-object primitives ended up with Objectified versions.
+ In older versions of Java, it was the programmers responsibility to convert back and forth from a primitive to an object whenever necessary. + This process of converting a primitive to an object was called “boxing.” The reverse process is called “unboxing.” In Java 5, the compiler became smart enough to know when to convert back and forth and is called “autoboxing.” In this book, we will typically use the Object version of all the numeric data types and let the compiler do its thing. +
++ With that distinction in mind, here are the common types you'll use, most of which are similar to Python's types: +
+ int: The primitive type for integers (whole numbers), such as 3, 0, and -76. +
++ double: The primitive type for floating-point numbers like 6.3 or -0.9. +
++ boolean: The primitive type that can only be true or false. +
++ char: The primitive type for a single character, like 'a' or 'Z'. It is represented using single quotes. +
++ String: An object type that represents a sequence of characters in double quotes, like "Hello". +
+
+
+def main():
+ """ Program to convert a temperature from Fahrenheit to Celsius. """
+ fahr = int(input("Enter the temperature in F: ")) # get the temperature in Fahrenheit
+ cel = (fahr - 32) * 5.0/9.0 # convert to Celsius
+ print("the temperature in C is: ", cel)
+main()
+
+ Next,
+import java.util.Scanner; // import the Scanner class to read input from the user
+ /**
+ * Program to convert a temperature from Fahrenheit to Celsius in Java.
+ */
+public class TempConv {
+ public static void main(String[] args) {
+ Double fahr;
+ Double cel;
+ Scanner in; // declare a Scanner variable called in
+ in = new Scanner(System.in); // create a Scanner object to read input from the user
+ System.out.println("Enter the temperature in F: ");
+ fahr = in.nextDouble(); // read the temperature in Fahrenheit from the user
+ cel = (fahr - 32) * 5.0/9.0; // convert to Celsius
+ System.out.println("The temperature in C is: " + cel);
+ }
+}
+ + There are several new concepts introduced in this example. We will look at them in the following order: +
+ ++
+ Import +
++ Variable Declaration +
++ In Java, you can use any class that is available without having to import the class, subject to two very important conditions: +
+ ++
+ The
+ You must use the full name of the class +
+
+ Your first question might be how do the
+
+ Java knows about all the classes that are defined in .java and .class files in your current working directory. +
++ Java knows about all the classes that are shipped with Java. +
+
+ Java knows about all the classes that are included in your
+
+ A .jar file that contains Java classes +
++ Another directory that contains Java class files +
+
+ You can think of the import statement in Java as working a little bit like the
+ So, what exactly does the import statement do? What it does is tell the compiler that we are going to use a shortened version of the class’s name. In this example we are going to use the class
+ Here is where we run into one of the most important differences between Java and Python. Python is a
+ A valid variable name in Java can contain letters, digits, and underscores. It must begin with a letter, an underscore, or a dollar sign. It cannot start with a digit and it cannot be a reserved keyword (like class, int, or static). Variable names are case-sensitive, so
+ An important feature of Java is that when you declare a variable of a primitive type (like int or double), the system automatically allocates a fixed amount of memory to store its value directly. This is different from reference types (like String or Scanner), where the variable holds a memory address that points to the actual object data stored elsewhere. This distinction makes operations on primitives very fast. +
+
+ In the example above, lines 5—7 contain variable declarations. Specifically we are saying that
+ For Python programmers, the following error is likely to be even more common. Suppose we forgot the declaration for
+ TempConv.java:13: cannot find symbol
+ symbol : variable cel
+ location: class TempConv
+ cel = (fahr - 32) * 5.0/9.0;
+ ^
+ TempConv.java:14: cannot find symbol
+ symbol : variable cel
+ location: class TempConv
+ System.out.println("The temperature in C is: " + cel);
+ ^
+ 2 errors
+
+
+ When you see the first kind of error in
+ The general rule in Java is that you must decide what kind of an object your variable is going to reference and then you must declare that variable before you use it. In our temperature converter, the calculation
+ Construct a complete Java program that reads a distance in kilometers from the user and converts it to miles. + Drag the blocks into the correct order on the right. +
++ Typecasting is the process of converting a variable from one type to another. In Java, this is often necessary when you want to perform operations that require different data types. For example, if you have an integer and you want to convert it to a double for more precise calculations, you would use typecasting. +
+ ++ In Java, typecasting can be done in two ways: implicit and explicit. Implicit typecasting occurs automatically when converting from a smaller data type to a larger one (like int to double), while explicit typecasting requires you to specify the conversion manually (like double to int). +
+ +
+ Implicit typecasting happens automatically when converting a value from a smaller data type to a larger one, as there is no risk of losing information. For example, you can assign an int to a double without any special syntax as shown in
+void main() {
+ int myInt = 10;
+ double myDouble = myInt; // Automatic casting from int to double
+
+ System.out.println(myDouble);
+}
+
+
+ Explicit typecasting is required when converting from a larger data type to a smaller one, as you might lose data. You must do this manually by placing the target type in parentheses () before the value as shown in
+void main() {
+ double originalDouble = 9.78;
+ int castedInt = (int) originalDouble; // Explicitly casts double to int. The value of castedInt is now 9.
+
+ System.out.println(castedInt);
+}
+
+ + Besides primitive types, type casting is also a fundamental concept when working with objects, especially within an inheritance hierarchy. This involves converting an object reference from one class type to another, typically between a superclass and a subclass. This is often referred to as upcasting and downcasting. +
+
+ In
+/**
+ * Animal class is the superclass, with a method makeSound().
+ */
+class Animal {
+ public void makeSound() {
+ System.out.println("The animal makes a sound.");
+ }
+}
+
+/**
+ * Dog class is a subclass of Animal, with an additional method bark().
+ */
+class Dog extends Animal {
+ public void bark() {
+ System.out.println("The dog barks!");
+ }
+}
+
+
+ Upcasting (Implicit): Upcasting is casting a subclass instance to a superclass reference type. This is always safe because a subclass object is guaranteed to have all the methods and properties of its superclass. Therefore, upcasting is done implicitly by the compiler as
+
+/**
+ * Animal class is the superclass, with a method makeSound().
+ */
+class Animal {
+ public void makeSound() {
+ System.out.println("The animal makes a sound.");
+ }
+}
+
+/**
+ * Dog class is a subclass of Animal, with an additional method bark().
+ */
+class Dog extends Animal {
+ public void bark() {
+ System.out.println("The dog barks!");
+ }
+}
+
+/**
+ * Upcasting example: A Dog object is created, but the reference is of type Animal.
+ */
+void main() {
+// A Dog object is created, but the reference is of type Animal.
+// This is implicit upcasting.
+ Animal myAnimal = new Dog();
+
+ myAnimal.makeSound(); // This is valid, as makeSound() is defined in Animal.
+
+ // myAnimal.bark(); // This would cause a compile-time error!
+ // The compiler only knows about the methods in the Animal reference type.
+}
+
+
+ Downcasting (Explicit): Downcasting is casting a superclass reference back to its original subclass type. This is potentially unsafe because the superclass reference might not actually point to an object of the target subclass. You must perform an explicit cast. If you cast to the wrong type, Java will throw a
+ To safely downcast, you should first check the object's type using the
+/**
+ * Animal class is the superclass, with a method makeSound().
+ */
+class Animal {
+ public void makeSound() {
+ System.out.println("The animal makes a sound.");
+ }
+}
+
+/**
+ * Dog class is a subclass of Animal, with an additional method bark().
+ */
+class Dog extends Animal {
+ public void bark() {
+ System.out.println("The dog barks!");
+ }
+}
+
+/**
+ * Downcasting example: An Animal reference is downcast to a Dog reference after checking its type.
+ */
+void main() {
+ Animal myAnimal = new Dog();
+
+ // 'myAnimal' is an Animal reference, but it points to a Dog object.
+ if (myAnimal instanceof Dog myDog) {
+ // Now we can access methods specific to the Dog class.
+ myDog.bark();
+ }
+}
+
+
+
+ In
+ Construct a program that safely downcasts a
+ Strings in Java and Python are quite similar. Like Python, Java strings are immutable. However, manipulating strings in Java is not quite as obvious since Strings do not support an indexing or slicing operator. That is not to say that you can’t index into a Java string, you can. You can also pull out a substring just as you can with slicing. The difference is that Java uses method calls where Python uses operators. +
+ +
+ In fact, this is the first example of another big difference between Java and Python. Java does not support any operator overloading.
+ Next, let’s look at a program which reads numbers from a file and produces a histogram showing the frequency of the numbers. The data file we will use has one number between 0 and 9 on each line of the file.
+def main():
+ """ Program to read numbers from a file and produce a histogram. """
+ count = [0]*10 # create a list of 10 zeros
+ data = open('test.dat') # open the data file
+ # read each line and update the count
+ for line in data:
+ count[int(line)] = count[int(line)] + 1
+ idx = 0
+ for num in count: # iterate over the list and print the histogram
+ print(idx, " occurred ", num, " times.")
+ idx += 1
+main()
+
+ Test running the program. It will read
+ 1 + 2 + 3 + 9 + 1 ++
+Lets review what is happening in this little program. First, we create a list and initialize the first 10 positions in the list to be 0. Next we open the data file called ‘test.dat’. Third, we have a loop that reads each line of the file. As we read each line we convert it to an integer and increment the counter at the position in the list indicated by the number on the line we just read. Finally we iterate over each element in the list, printing out both the position in the list and the total value stored in that position. +
+ +
+To write the Java version of this program we will have to introduce several new Java concepts. First, you will see the Java equivalent of a list, an
+
+
+
+The
+
+
+The
+
+
+The
+
+ The Java code below contains the first mention of the
+import java.util.Scanner; // import the Scanner class to read input from the user
+import java.util.ArrayList; // import the ArrayList class to use dynamic arrays
+import java.io.File; // import the File class to read from files
+import java.io.IOException; // import the IOException class to handle file input/output exceptions
+/**
+ * Program to read numbers from a file and produce a histogram in Java.
+ */
+public class Histo {
+ public static void main(String[] args) {
+ Scanner data = null;
+ ArrayList<Integer> count; // create an ArrayList to hold counts of numbers, of type Integer
+ Integer idx;
+
+ // Try to open the data file and handle any potential IOExceptions
+ try {
+ data = new Scanner(new File("test.dat"));
+ }
+ catch ( IOException e) {
+ System.out.println("Unable to open data file");
+ e.printStackTrace(); // print the stack trace for debugging
+ System.exit(0);
+ }
+ count = new ArrayList<Integer>(10); // create an ArrayList with an initial capacity of 10
+ for (Integer i = 0; i < 10; i++) {
+ count.add(i,0); // initialize the first 10 positions in the ArrayList to hold the value 0
+ }
+ while(data.hasNextInt()) {
+ // read each integer from the file and update the count
+ idx = data.nextInt();
+ count.set(idx,count.get(idx)+1);
+ }
+ idx = 0;
+ for(Integer i : count) {
+ // iterate over each element in the ArrayList and print the histogram
+ System.out.println(idx + " occurred " + i + " times.");
+ idx++;
+ }
+ }
+}
+
+ Before going any further, I suggest you try to compile
+ Now, let’s look at what is happening in the Java source. As usual, we declare the variables we are going to use at the beginning of the method. In
+ Technically, you don’t have to declare what is going to be in an array list. The compiler will allow you to leave the
+ Note: Histo.java uses unchecked or unsafe operations.
+ Note: Recompile with -Xlint:unchecked for details.
+
+
+ Without the
+ Lines 13—20 in
+ try {
+ Put some risky code in here, like opening a file
+ } catch (Exception e) {
+ If an error happens in the try block an exception is thrown. We will catch that exception here!
+ }
+
+
+ Notice that in line 16 in
+ On line 22 in
+ The syntax of this for loop probably looks very strange to you, but in fact it is not too different from what happens in Python using range. In fact
+ Match each Python
+ The next loop (lines 27–30) in
+ Line 29 in
+ The last loop in
+ As was said at the outset of this section, we are going to use Java
+import java.util.Scanner; // import the Scanner class to read input from the user
+import java.io.File; // import the File class to read from files
+import java.io.IOException; // import the IOException class to handle file input/output exceptions
+/**
+ * Program to read numbers from a file and produce a histogram using arrays in Java.
+ */
+public class HistoArray {
+ public static void main(String[] args) {
+ Scanner data = null;
+ Integer[] count = {0,0,0,0,0,0,0,0,0,0}; // create an array of 10 integers initialized to 0
+ Integer idx;
+ try {
+ data = new Scanner(new File("test.dat"));
+ }
+ catch ( IOException e) {
+ System.out.println("Unable to open data file");
+ e.printStackTrace();
+ System.exit(0);
+ }
+ while(data.hasNextInt()) {
+ idx = data.nextInt();
+ count[idx] = count[idx] + 1; // increment the count for the number read from the file, using array indexing
+ }
+ idx = 0;
+ for(Integer i : count) {
+ System.out.println(idx + " occurred " + i + " times.");
+ idx++;
+ }
+ }
+}
+
+ The main difference between
+ Construct a short Java program that creates an array of three integers, + changes the first value, and prints it. Drag the blocks into the correct order on the right. +
+
+ Just as Python provides the dictionary when we want to have easy access to key-value pairs, Java also provides us a similar mechanism. Rather than the dictionary terminology, Java calls these objects Maps. Java provides two different implementations of a map, one is called the
+ Lets stay with a simple frequency counting example, only this time we will count the frequency of words in a document.
+def main():
+ """ Program to read a file and count the frequency of words in the file. """
+ data = open('alice30.txt')
+ wordList = data.read().split() # split the file into a list of words
+ count = {}
+ for w in wordList: # iterate over the list of words
+ w = w.lower()
+ count[w] = count.get(w,0) + 1
+ keyList = sorted(count.keys()) # sort the keys in alphabetical order
+ for k in keyList: # iterate over the sorted list of keys
+ print("%-20s occurred %4d times" % (k, count[k]))
+main()
+
+ This program reads the file
+ Down, down, down. Would the fall NEVER + come to an end! 'I wonder how many + miles I've fallen by this time?' she + said aloud. 'I must be getting somewhere + near the centre of the earth. Let me see: + that would be four thousand miles down, + I think--' (for, you see, Alice had + learnt several things of this sort in + her lessons in the schoolroom, and though + this was not a VERY good opportunity for + showing off her knowledge, as there was no + one to listen to her, still it was good + practice to say it over) '--yes, that's + about the right distance--but then I + wonder what Latitude or Longitude I've got + to?' (Alice had no idea what Latitude was, + or Longitude either, but thought they were + nice grand words to say.) ++
+ Notice that the structure of
+import java.util.Scanner;
+import java.util.ArrayList; // import the ArrayList class to use dynamic arrays
+import java.io.File;
+import java.io.IOException;
+import java.util.TreeMap; // import the TreeMap class to use a map for counting word frequencies
+/**
+ * Program to read a file and count the frequency of words in the file.
+ */
+public class HistoMap {
+ public static void main(String[] args) {
+ Scanner data = null;
+ TreeMap<String,Integer> count; // create a TreeMap to hold word counts, mapping each word (String) to its frequency (Integer)
+ Integer idx;
+ String word;
+ Integer wordCount;
+ try { // Try to open the data file and handle any potential IOExceptions
+ data = new Scanner(new File("alice30.txt"));
+ }
+ catch ( IOException e) {
+ System.out.println("Unable to open data file");
+ e.printStackTrace();
+ System.exit(0);
+ }
+ count = new TreeMap<String,Integer>(); // create a TreeMap to hold word counts
+ while(data.hasNext()) {
+ word = data.next().toLowerCase(); // read each word from the file, convert it to lowercase
+ wordCount = count.get(word); // get the current count for the word from the TreeMap
+ if (wordCount == null) {
+ wordCount = 0;
+ }
+ count.put(word,++wordCount); // increment the count for the word and put it back into the TreeMap
+ }
+ for(String i : count.keySet()) { // iterate over the keys (words) in the TreeMap
+ System.out.printf("%-20s occurred %5d times\n", i, count.get(i) );
+ }
+ }
+}
+
+ Improve
+ Rearrange the blocks to create a general Java method that accepts a dictionary (Map) of item prices and updates the price of a specific item. If the item does not exist, do nothing. +
+All variables must be declared with their type before use in Java due to static typing.
+Java has primitive types (
The
Java Strings are immutable like Python, but use methods like
Maps (
What is the correct way to declare an ArrayList that will hold String objects in Java?
+ArrayList<String> list;
+Good job!
+ArrayList list;
+No, you must specify the type of objects it will hold using generics.
+ArrayList[String] list;
+No, the syntax for generics uses < > brackets, not [ ].
+ArrayList() list;
+No, this is not a valid declaration.
+The process of automatically converting between primitive types and their Object wrapper classes in Java is called:
+Autoboxing
+Right! Good job!
+Casting
+No, casting is manually converting a type.
+Overloading
+No, overloading refers to methods, not type conversion.
+Unboxing
+No, unboxing is the reverse of autoboxing.
+Which method would you use to get the character at position 3 in a Java String called str?
+str.charAt(3)
+Correct!
+str[3]
+No, Java does not support this type of indexing.
+str.get(3)
+No, this method does not exist in Java.
+str.substring(3)
+No, this will return a substring starting at position 3, not a single character.
+
+
+score = 95
+if score >= 90: # Note the colon at the end of the line
+ print("Excellent work!")
+
+
+ In Java, this same pattern requires two changes: the condition must be in parentheses
+ public class SimpleIfExample {
+ public static void main(String[] args) {
+ int score = 95;
+ if (score >= 90) { // Note the parentheses and curly braces
+ System.out.println("Excellent work!");
+ }
+ }
+ }
+
+
+ Once again you can see that in Java the curly braces define a block rather than indentation.
+ In Java, the parentheses around the condition are required because it is technically a function that evaluates to
+ age = 16
+ if age >= 18: # notice the semicolon
+ print("You can vote.")
+ else: # notice the semicolon
+ print("You are not yet eligible to vote.")
+
+
+ public class IfElseExample {
+ public static void main(String[] args) {
+ int age = 16;
+ if (age >= 18) {
+ System.out.println("You can vote.");
+ } else { // else has its own block.
+ System.out.println("You are not yet eligible to vote.");
+ }
+ }
+ }
+
+
+grade = int(input('enter a grade'))
+if grade < 60:
+ print('F')
+elif grade < 70: # notice the semicolon
+ print('D')
+elif grade < 80:
+ print('C')
+elif grade < 90:
+ print('B')
+else:
+ print('A')
+
+In Java, we have a couple of ways to write this.
+public class ElseIf {
+ public static void main(String args[]) {
+ int grade = 85;
+ if (grade < 60) {
+ System.out.println('F');
+ } else { // else has its own block.
+ if (grade < 70) {
+ System.out.println('D');
+ } else {
+ if (grade < 80) {
+ System.out.println('C');
+ } else {
+ if (grade < 90) {
+ System.out.println('B');
+ } else {
+ System.out.println('A');
+ }
+ }
+ }
+ }
+ }
+ }
+
+We can get even closer to the
+public class ElseIf {
+ public static void main(String args[]) {
+ int grade = 85;
+ if (grade < 60) {
+ System.out.println('F');
+ } else if (grade < 70) { // notice how we got rid of the curly braces.
+ System.out.println('D');
+ } else if (grade < 80) {
+ System.out.println('C');
+ } else if (grade < 90) {
+ System.out.println('B');
+ } else System.out.println('A');
+ }
+}
+
+Java also supports a
+ The
+ grade = 85
+ # Convert grade to a scale of 0-10 using integer division
+ tempgrade = grade // 10
+ def grading(tempgrade):
+ match grade:
+ case 10 | 9:
+ return 'A'
+ case 8:
+ return 'B'
+ case 7:
+ return 'C'
+ case 6:
+ return 'D'
+ case _:
+ return 'F'
+ print(grading(tempgrade))
+
+
+
+ public class SwitchUp {
+ public static void main(String args[]) {
+ int grade = 85;
+ // Convert grade to a scale of 0-10 using integer division
+ int tempgrade = grade / 10;
+ switch(tempgrade) {
+ case 10:
+ case 9:
+ System.out.println('A');
+ break;
+ case 8:
+ System.out.println('B');
+ break;
+ case 7:
+ System.out.println('C');
+ break;
+ case 6:
+ System.out.println('A');
+ break;
+ default:
+ System.out.println('F');
+ }
+ }
+ }
+
+The
+ Finally, the
+ Rearrange the blocks to create a Java method that accepts a temperature reading and returns a status string ("CRITICAL", "WARNING", or "NORMAL"). +
+
+Using this operator can make code shorter and more readable in cases where a simple conditional assignment is needed.
+public class Ternary {
+ public static void main(String[] args) {
+ int a = 4;
+ int x = 2;
+ int outp;
+
+ // ternary:
+ outp = (a % 2 == 0) ? (a * a) : (3 * x - 1);
+ System.out.println("ternary result: " + outp);
+
+ // Equivalent using if/else
+ if (a % 2 == 0) {
+ outp = a * a;
+ } else {
+ outp = 3 * x - 1;
+ }
+
+ System.out.println("if/else result: " + outp);
+ }
+}
+
+
+ In
+ Rearrange the blocks to create a Java method that calculates shipping costs. Orders over $100 get free shipping ($0.0), while members pay $5.0 and non-members pay $10.0. +
+
+ In Python, if you want a program to continue running when an error has occurred, you can use
+ number = int(input("Please enter a whole number: ")) # ask user for a number
+ squared = number ** 2 # square the number
+ print("Your number squared is " + str(squared))
+
+
+
+ import java.util.Scanner;
+
+ public class SquareNumber {
+ public static void main(String[] args) {
+ Scanner user_input = new Scanner(System.in); // create a scanner object
+
+ System.out.print("Please enter a whole number: ");
+ int number = user_input.nextInt(); // ask user for a number
+ int squared = number * number; // square the number
+
+ System.out.println("Your number squared is " + squared);
+ }
+ }
+
+
+ This code works well, but will end with an exception if the user types anything other than a whole number (such as 12.5 or two). If we wanted to ensure the code will continue to run until the user enters the correct format, we could add
+ while True:
+ try: # try to convert the user input to an integer
+ number = int(input("Please enter a whole number: "))
+ squared = number ** 2
+ print("Your number squared is " + str(squared))
+ break
+ except ValueError: # if the user enters a non-integer, print an error message
+ print("That was not a valid number. Please try again: ")
+
+
+ Now that we have Python code that will continuously prompt the user until they enter a whole number,
+ import java.util.Scanner;
+ import java.util.InputMismatchException;
+
+ public class SquareNumberWithValidation {
+ public static void main(String[] args) {
+ Scanner scanner = new Scanner(System.in);
+
+ while (true) { // keep asking the user for a number until they enter a valid integer
+ try { // try to convert the user input to an integer
+ System.out.print("Please enter a whole number: ");
+ int number = scanner.nextInt();
+ int squared = number * number;
+ System.out.println("Your number squared is " + squared);
+ break;
+ } catch (InputMismatchException e) { // if the user enters a non-integer, print an error message
+ System.out.println("That was not a valid number. Please try again: ");
+ scanner.nextLine(); // Clear the invalid input from the scanner
+ }
+ }
+ }
+ }
+
+ Firstly, let's talk about the extra import alongside the
+
+ Note that as with other structures in Java,
Java requires parentheses around the condition and curly braces for code blocks in
Java uses
+ Java's
+ Java uses the
Which is a correct Java
if (x > 0) { System.out.println("Positive"); }
+Correct! Java requires parentheses and curly braces.
+if x > 0: print("Positive")
+No, that's Python syntax, not Java.
+if x > 0 { System.out.println("Positive"); }
+No, Java requires parentheses around the condition.
+if (x > 0) print("Positive");
+No,
How do you write Python’s
elif (score > 90)
+No,
else: if (score > 90)
+Incorrect syntax; no colon in Java and not the right structure.
+else if (score > 90)
+Right! Java uses
ifelse (score > 90)
+No,
What is one limitation of Java's
It cannot evaluate relational expressions like greater than or less than.
+No, while
It cannot handle more than five case labels.
+No, there is no such limit. You can have many case labels in a
It always requires a
Incorrect. The
It can only compare a variable to constant values using equality.
+Correct! Java's
+ You have already seen a couple of examples of iteration and looping in Java. + So this section will just serve as a reference for the differences in syntax. +
+ +
+for i in range(10): # range(10) is a list of integers from 0 to 9
+ print(i)
+
+
+
+public class DefiniteLoopExample {
+ public static void main(String[] args) {
+ for (Integer i = 0; i < 10; i++ ) { // notice how the initialization, condition, and update are all on the same line.
+ System.out.println(i);
+ }
+ }
+}
+
+
+ Recall that the
+ range(stop)
+ range(start,stop)
+ range(start,stop,step)
+
+
+ The Java
+ for (start clause; stop clause; step clause) {
+ statement1
+ statement2
+ ...
+ }
+
+
+ If you want to start at 100, stop at 0 and count backward by 5,
+for i in range(100, -1, -5): # start at 100, stop at 0, decrement by 5
+ print(i)
+
+
+
+public class DefiniteLoopBackward {
+ public static void main(String[] args) {
+ for (Integer i = 100; i >= 0; i -= 5) { // start at 100, stop at 0, decrement by 5
+ System.out.println(i);
+ }
+ }
+}
+
+
+ In Python, the
+
+l = [1, 1, 2, 3, 5, 8, 13, 21] # create a list of integers
+for fib in l: # iterate over the list
+ print(fib)
+
+
+
+import java.util.ArrayList;
+
+public class ForEachArrayListExample {
+ public static void main(String[] args) {
+ ArrayList<Integer> l = new ArrayList< // create an ArrayList of integers
+ l.add(1); // add the first integer to the list
+ l.add(1); // add the second integer to the list
+ l.add(2); // keep going
+ l.add(3);
+ l.add(5);
+ l.add(8);
+ l.add(13);
+ l.add(21); // add the last integer to the list
+ for (Integer i : l) { // iterate over the list
+ System.out.println(i);
+ }
+ }
+}
+
+
+
+public class ForEachArrayExample {
+ public static void main(String[] args) {
+ int l[] = {1,1,2,3,5,8,13,21}; // create an array of integers using primitive syntax
+ for(int i : l) { // iterate over the array
+ System.out.println(i);
+ }
+ }
+}
+
+
+
+public class StringIterationExample {
+ public static void main(String[] args) {
+ String t = "Hello World"; // create a string
+ for (char c : t.toCharArray()) { // iterate over the characters in the string
+ System.out.println(c);
+ }
+ }
+}
+
+
+ Rearrange the blocks to create a Java method that accepts an upper bound integer
+i = 5
+while i > 0: # while i is greater than 0
+ print(i)
+ i = i - 1
+
+
+ In Java, we add parentheses and curly braces.
+public class WhileLoopExample {
+ public static void main(String[] args) {
+ int i = 5;
+ while (i > 0) { // while i is greater than 0
+ System.out.println(i);
+ i = i - 1;
+ }
+ }
+}
+
+
+public class DoWhileExample {
+ public static void main(String[] args) {
+ int i = 10;
+ do { // do-while loop, will run at least once no matter the condition
+ System.out.println("This runs once, i = " + i);
+ } while (i < 5); // while i is less than 5
+ }
+}
+
+ + Rearrange the blocks to create a Java method that accepts a starting balance and a target amount, then calculates how many years it takes for the balance to reach or exceed the target by doubling each year. +
++
Java’s
To loop over lists or arrays in Java, the enhanced for-each loop uses the syntax
Python’s
Java also supports the
Java’s
To iterate through characters in a Java String, use
Which of the following is the correct format for a definite loop in Java that runs 10 times?
+for i in range(10):
No, that is Python syntax.
for (int i = 0; i < 10; i++)
Correct! That’s the proper Java syntax for a definite loop.
loop i from 0 to 10
No, this is not valid syntax in Java.
for (i < 10; i++)
No, the initialization part is missing in this Java loop.
Which loop correctly iterates through all elements in a Java array of integers?
+for (int i : array)
Yes! This is Java's enhanced for-each loop for arrays.
for (i in array)
No, that's closer to Python syntax.
for (int i = 0; i < array; i++)
No,
foreach i in array:
No, this is not valid Java syntax.
What is a unique characteristic of the Java
It checks the condition before the loop body runs.
No, that describes a regular
It always runs infinitely.
No, a
It guarantees the loop body runs at least once.
Correct! The
It is not supported in Java.
No, Java does support
+
+
+
+ The best way to understand classes and objects is to see them in action. Let's define a
+ class Dog:
+ """ A simple Dog class definition. """
+ def __init__(self, name, breed, fur_color):
+ # constructor method to create a Dog object
+ self.name = name
+ self.breed = breed
+ self.fur_color = fur_color
+ self.trained = False # dogs are not trained by default
+ print("Dog named " + self.name + " created!")
+
+ def bark(self):
+ # method to make the dog bark
+ print(self.name + " says woof!")
+
+ def sit(self):
+ # method to make the dog sit
+ if self.trained: # check if the dog has been trained otherwise it will not sit
+ print(self.name + " sits.")
+ else:
+ print(self.name + " has not been trained.")
+
+ def train(self):
+ # method to train the dog, which will set the trained attribute to True
+ self.trained = True
+
+
+ Let's unpack what is going on in
+ The next three blocks of code are the class's methods. These include
+
+ Next, we will use this class to create a new
+ class Dog:
+ """ A simple Dog class definition. """
+ def __init__(self, name, breed, fur_color):
+ # constructor method to create a Dog object
+ self.name = name
+ self.breed = breed
+ self.fur_color = fur_color
+ self.trained = False # dogs are not trained by default
+ print("Dog named " + self.name + " created!")
+
+ def bark(self):
+ # method to make the dog bark
+ print(self.name + " says woof!")
+
+ def sit(self):
+ # method to make the dog sit
+ if self.trained: # check if the dog has been trained otherwise it will not sit
+ print(self.name + " sits.")
+ else:
+ print(self.name + " has not been trained.")
+
+ def train(self):
+ # method to train the dog, which will set the trained attribute to True
+ self.trained = True
+
+ # Create a Dog object called my_dog
+ my_dog = Dog("Rex", "pug", "brown")
+
+
+ In the final line of code in
+ Now that we have created a
+ class Dog:
+ """ A simple Dog class definition. """
+ def __init__(self, name, breed, fur_color):
+ # constructor method to create a Dog object
+ self.name = name
+ self.breed = breed
+ self.fur_color = fur_color
+ self.trained = False # dogs are not trained by default
+ print("Dog named " + self.name + " created!")
+
+ def bark(self):
+ # method to make the dog bark
+ print(self.name + " says woof!")
+
+ def sit(self):
+ # method to make the dog sit
+ if self.trained:
+ print(self.name + " sits.")
+ else:
+ print(self.name + " has not been trained.")
+
+ def train(self):
+ # method to train the dog, which will set the trained attribute to True
+ self.trained = True
+
+
+ my_dog = Dog("Rex", "pug", "brown")
+ my_dog.bark() # call the bark method
+ my_dog.sit() # call the sit method
+
+
+ When running
+ Now, we have a full class definition and have utilized its methods. Class definitions in Java will be covered thoroughly in chapter 6. For now, it is important to know that Python programs can be written without using classes at all. Java, on the other hand, requires all code to reside in a class. This will be discussed later in chapter 6. +
+ +
- Here is a mostly complete implementation of a Fraction class in Python that we will refer to throughout this section:
+
class Fraction:
@@ -76,37 +242,54 @@
"""
self.num = num
self.den = den
- def __repr__(self):
- if self.num > self.den:
- retWhole = int(self.num / self.den)
- retNum = self.num - (retWhole * self.den)
+ def __repr__(self):
+ """
+ :return: a string representation of the fraction
+ """
+ if self.num > self.den:
+ retWhole = int(self.num / self.den) # find the whole number part
+ retNum = self.num - (retWhole * self.den) # find the numerator part
return str(retWhole) + " " + str(retNum) + "/" + str(self.den)
else:
return str(self.num) + "/" + str(self.den)
def show(self):
+ """
+ :return: print the fraction
+ """
print(self.num, "/", self.den)
def __add__(self, other):
+ """
+ :param other: the fraction to add
+ :return: the sum of the two fractions
+ """
# convert to a fraction
other = self.toFract(other)
- newnum = self.num * other.den + self.den * other.num
- newden = self.den * other.den
+ newnum = self.num * other.den + self.den * other.num # find the new numerator
+ newden = self.den * other.den # find the new denominator
common = gcd(newnum, newden)
return Fraction(int(newnum / common), int(newden / common))
- __radd__ = __add__
+ __radd__ = __add__ # allow the fraction to be added to a number
def __lt__(self, other):
+ """
+ :param other: the fraction to compare
+ :return: whether the fraction is less than the other
+ """
num1 = self.num * other.den
num2 = self.den * other.num
return num1 < num2
def toFract(self, n):
+ """
+ :param n: the number to convert to a fraction
+ :return: the fraction representation of the number
+ """
if isinstance(n, int):
other = Fraction(n, 1)
elif isinstance(n, float):
wholePart = int(n)
- fracPart = n - wholePart
- # convert to 100ths???
- fracNum = int(fracPart * 100)
- newNum = wholePart * 100 + fracNum
- other = Fraction(newNum, 100)
+ fracPart = n - wholePart
+ fracNum = int(fracPart * 100) # convert to 100ths
+ newNum = wholePart * 100 + fracNum # combine the whole and fractional parts
+ other = Fraction(newNum, 100)
elif isinstance(n, Fraction):
other = n
else:
@@ -115,9 +298,12 @@
return other
def gcd(m, n):
"""
- A helper function for Fraction
+ A helper function for Fraction.
+ :param m: the first number
+ :param n: the second number
+ :return: the greatest common divisor
"""
- while m % n != 0:
+ while m % n != 0: # keep going until the gcd is found
oldm = m
oldn = n
m = oldn
@@ -126,61 +312,69 @@
print(sorted([Fraction(5, 16), Fraction(3, 16), Fraction(1, 16) + 1]))
- The instance variables (data members) we will need for our fraction class are the numerator and denominator. Of course in Python we can add instance variables to a class at any time by simply assigning a value to
- The declarations of instance variables can come at the beginning of the class definition or the end. Cay Horstman, author of
- public class Fraction {
- private Integer numerator;
+ public class Fraction {
+ private Integer numerator; // notice the private modifier
private Integer denominator;
}
- Notice that we have declared the numerator and denominator to be private.
- This means that the compiler will generate an error if another method tries to write code like the following:
+
- Fraction f = new Fraction(1,2);
- Integer y = f.numerator * 10;
+ Fraction f = new Fraction(1,2);
+ Integer y = f.numerator * 10; // trying to access the numerator
- Direct access to instance variables is not allowed.
- Therefore if we legitimately want to be able to access information such as the numerator or denominator for a particular fraction we must have getter methods.
- It is very common programming practice to provide getter and setter methods for instance variables in Java.
+
-public Integer getNumerator() {
+public Integer getNumerator() { // getter method
return numerator;
}
-public void setNumerator(Integer numerator) {
+public void setNumerator(Integer numerator) { // setter method
this.numerator = numerator;
}
-public Integer getDenominator() {
+public Integer getDenominator() { // getter method
return denominator;
}
-public void setDenominator(Integer denominator) {
+public void setDenominator(Integer denominator) { // setter method
this.denominator = denominator;
}
+
public Fraction(Integer top, Integer bottom) {
- num = top;
+ num = top; // notice the use of num instead of top
den = bottom;
}
+
public Fraction(Integer num, Integer den) {
- this.num = num;
+ this.num = num; // notice how we use this.num instead of num
this.den = den;
}
Now we come to one of the major differences between Java and Python.
The Python class definition used the special methods for addition and comparison that have the effect of redefining how the standard operators behave: in Python,
-
+
- Let’s begin by implementing addition in Java:
+
-public Fraction add(Fraction otherFrac) {
+public Fraction add(Fraction otherFrac) {
Integer newNum = otherFrac.getDenominator() * this.numerator +
- this.denominator * otherFrac.getNumerator();
- Integer newDen = this.denominator * otherFrac.getDenominator();
- Integer common = gcd(newNum, newDen);
+ this.denominator * otherFrac.getNumerator(); // notice the use of this.
+ Integer newDen = this.denominator * otherFrac.getDenominator(); // find the new denominator
+ Integer common = gcd(newNum, newDen); // find the greatest common divisor
return new Fraction(newNum/common, newDen/common);
}
First you will notice that the
Second, you will notice that the method makes use of the
public Fraction add(Fraction otherFrac) {
Integer newNum = otherFrac.getDenominator() * numerator +
- denominator * otherFrac.getNumerator();
+ denominator * otherFrac.getNumerator(); // notice the absence of this.
Integer newDen = denominator * otherFrac.getDenominator();
Integer common = gcd(newNum, newDen);
return new Fraction(newNum/common, newDen/common);
}
The addition takes place by multiplying each numerator by the opposite denominator before adding. @@ -331,16 +535,18 @@ public Fraction add(Fraction otherFrac) {
+
+
-public Fraction(Integer num) {
- this.numerator = num;
+public Fraction(Integer num) {
+ this.numerator = num; // set the numerator to the Integer
this.denominator = 1;
}
-public Fraction add(Integer other) {
- return add(new Fraction(other));
+public Fraction add(Integer other) { // overload the add method when the parameter is an Integer
+ return add(new Fraction(other));
}
Notice that the overloading approach can provide us with a certain elegance to our code. @@ -371,21 +578,21 @@ public Fraction add(Integer other) {
- Our full
public class Fraction {
private Integer numerator;
private Integer denominator;
- public Fraction(Integer num, Integer den) {
+ public Fraction(Integer num, Integer den) { // constructor that takes two Integers
this.numerator = num;
this.denominator = den;
}
- public Fraction(Integer num) {
+ public Fraction(Integer num) { // constructor that takes a single Integer, sets the denominator to 1
this.numerator = num;
this.denominator = 1;
}
@@ -401,10 +608,10 @@ public class Fraction {
Integer common = gcd(newNum,newDen);
return new Fraction(newNum/common, newDen/common );
}
- public Fraction add(Integer other) {
+ public Fraction add(Integer other) { // overload the add method when the parameter is an Integer
return add(new Fraction(other));
}
- private static Integer gcd(Integer m, Integer n) {
+ private static Integer gcd(Integer m, Integer n) { // a helper method for the add method
while (m % n != 0) {
Integer oldm = m;
Integer oldn = n;
@@ -413,83 +620,88 @@ public class Fraction {
}
return n;
}
- public static void main(String[] args) {
+ public static void main(String[] args) { // a main method
Fraction f1 = new Fraction(1,2);
System.out.println(f1.add(1));
}
}
- It is worth pointing out that Java has some very handy naming conventions. It is advisable to both use meaningful names and to follow these naming conventions while developing software in Java for good maintenance and readability of code. -
- --
- Class names should be nouns that are written in UpperCamelCase, namely with the first letter of each word capitalized including the first.
- For example,
- Method names use lowerCamelCase which start with a verb that describes the action they perform. This means that method names start with a lower case letter, and use upper case for each internal-word method names. For example,
- Instance variables of a class start with a lower case letter and use lowerCamelCase like method names. For example,
- Constants are in all upper case letters or in upper snake case, which also known as screaming snake case, and which is a naming convention in which each word is written in uppercase letters, separated by underscores.
- For example,
- If you ran the program above you probably noticed that the output is not very satisfying. Chances are your output looked something like this: + +
+ If you ran
Fraction@6ff3c5b5
The reason is that we have not yet provided a friendly string representation for our
+ Rearrange the blocks to create a
+
We are not interested in most of the methods on that list, and many Java programmers live happy and productive lives without knowing much about most of the methods on that list.
- However, to make our output nicer we will implement the
public String toString() {
- return numerator.toString() + "/" + denominator.toString();
+ return numerator.toString() + "/" + denominator.toString(); // convert to a string
}
+
-object1 == object2
+object1 == object2 // this checks to see if the two objects are the same object in memory
- is NOT the same as
+
-object1.equals(object2)
+object1.equals(object2) // this checks to see if the two objects are equal by looking at their instance variables
- Here is an
-public boolean equals(Fraction other) {
+public boolean equals(Fraction other) { // check if this fraction is equal to another fraction
Integer num1 = this.numerator * other.getDenominator();
Integer num2 = this.denominator * other.getNumerator();
if (num1 == num2)
@@ -610,6 +825,7 @@ public boolean equals(Fraction other) {
}
One important thing to remember about
+
- Here is code that makes the
-public class Fraction extends Number {
+public class Fraction extends Number { // fraction class is a child of Number
...
}
- The keyword
- This really isn’t much work for us to implement these methods, as all we have to do is some type conversion and some division:
+ This really isn’t much work for us to implement these methods, as all we have to do is some type conversion and some division as shown in
-public double doubleValue() {
+public double doubleValue() { // convert to a double
return numerator.doubleValue() / denominator.doubleValue();
}
-public float floatValue() {
+public float floatValue() { // convert to a float
return numerator.floatValue() / denominator.floatValue();
}
-public int intValue() {
+public int intValue() { // convert to an int
return numerator.intValue() / denominator.intValue();
}
-public long longValue() {
+public long longValue() { // convert to a long
return numerator.longValue() / denominator.longValue();
}
+
- However, and this is a big however, it is important to remember that if you specify
- Suppose you try to define a method as follows:
+ Suppose you try to define a method as
public void test(Number a, Number b) {
- a.add(b);
+ a.add(b); // this is a bad idea
}
The Java compiler would give an error because
+ Construct the
+
+
The
int compareTo(T o)
@@ -778,79 +1034,86 @@ iff y.compareTo(x) throws an exception.)
...
- To make our
-public class Fraction extends Number implements Comparable<Fraction> {
+public class Fraction extends Number implements Comparable<Fraction> { // fraction class is a child of Number and implements the Comparable interface
...
}
The specification
-public int compareTo(Fraction other) {
+public int compareTo(Fraction other) { // compare this fraction with another fraction
Integer num1 = this.numerator * other.getDenominator();
Integer num2 = this.denominator * other.getNumerator();
return num1 - num2;
}
Suppose that you wanted to write a Student class so that the class could keep track of the number of students it had created.
Although you could do this with a global counter variable that is an ugly solution.
- The right way to do it is to use a static variable.
- In Python we could do this as follows:
+ The right way to do it is to use a
class Student:
numStudents = 0
- def __init__(self, id, name):
+ def __init__(self, id, name):
self.id = id
self.name = name
- Student.numStudents = Student.numStudents + 1
+ # this is a static variable, that can be accessed without the self prefix
+ Student.numStudents = Student.numStudents + 1
def main():
for i in range(10):
- s = Student(i,"Student-"+str(i))
+ s = Student(i,"Student-"+str(i)) # create a new Student object
print('Number of students:', Student.numStudents)
main()
- In Java we would write this same example using a static declaration.
+
public class Student {
- public static Integer numStudents = 0;
+ public static Integer numStudents = 0; // static member variable, shared by all instances of the class
private int id;
private String name;
public Student(Integer id, String name) {
this.id = id;
this.name = name;
- numStudents = numStudents + 1;
+ numStudents = numStudents + 1; // a static variable, that can be accessed without the Student prefix
}
public static void main(String[] args) {
for(Integer i = 0; i < 10; i++) {
@@ -861,9 +1124,11 @@ public class Student {
}
- In this example notice that we create a static member variable by using the
+
-private static Integer gcd(Integer m, Integer n) {
+private static Integer gcd(Integer m, Integer n) { // static method to compute the greatest common divisor of two integers
while (m % n != 0) {
Integer oldm = m;
Integer oldn = n;
@@ -888,17 +1157,18 @@ private static Integer gcd(Integer m, Integer n) {
}
- Here is a final version of the
import java.util.ArrayList;
import java.util.Collections;
@@ -992,5 +1262,132 @@ public class Fraction extends Number implements Comparable<Fraction> {
}
In Java, instance variables (fields) must be declared in the class body before they are used. Unlike Python, you cannot dynamically add new instance variables to an object at runtime.
+Java uses access modifiers like
Java requires a constructor method to initialize objects. A constructor has the same name as the class and defines its parameters explicitly, whereas Python uses the
Every Java class inherits from the
By default, Java’s
Java supports inheritance through abstract classes (like
How are instance variables declared in Java compared to Python?
+They can be created dynamically anywhere in the class like Python.
No, Java does not allow dynamic creation of instance variables at runtime.
Instance variables are declared inside methods only.
No, instance variables are declared in the class body, not in methods.
They must be declared in the class body before use.
Correct! Java requires instance variables (fields) to be declared in the class body.
Java does not use instance variables.
No, instance variables are fundamental in Java classes.
What Java feature encourages encapsulation and controlled access to instance variables?
+Declaring all variables as public.
No, that would expose data and reduce encapsulation.
Using access modifiers like
Right! This is how Java enforces encapsulation.
Using global variables.
No, Java does not support global variables and this reduces encapsulation.
Avoiding the use of classes altogether.
No, encapsulation is a class-based concept in Java.
How does Java initialize objects differently than Python?
+Java uses a constructor method named after the class with explicit parameters.
Correct! Unlike Python's
Java uses the
No, Java does not have
Java initializes objects automatically without constructors.
No, Java requires constructors for explicit initialization.
Java uses global initialization functions instead of constructors.
No, Java uses constructors, not global functions, for object initialization.
What must you do in Java to print objects in a readable way and compare two objects based on their contents rather than their memory references?
+Use
No,
Only override
No, you should override
Java automatically handles content comparison without overrides.
No, default
Override
Yes! This improves output and content-based comparison.
+ In this chapter, we will explore how to translate your recursive logic from Python to Java. While the core concepts of recursion remain the same, the syntax and a bit of the structure of your code will change somewhat. +
+
+ Let's take the familiar factorial function, which calculates
+ You may recall mathematical notation using the symbol
+ Factorial involves multiplication rather than addition, so we use the product symbol
+
+
+def factorial(n):
+ # Check for negative numbers
+ if n < 0:
+ print("Factorials are only defined on non-negative integers.")
+ return
+ # Base Case: 0! or 1! is 1
+ if n <= 1:
+ return 1
+ # Recursive Step: n * (n-1)!
+ return n * factorial(n - 1)
+
+number = 5
+print(str(number) + "! is " + str(factorial(number)))
+
+
+
+ Many Python programs organize related functions into classes. The same factorial function can be placed inside a class as a method instead of as a function. When this is done, you need to create an instance of the class in order to call the method. Below, we create the class
+
+class MTools:
+ def factorial(self, n):
+ # Check for negative numbers
+ if n < 0:
+ print("Factorials are only defined on non-negative integers.")
+ return
+ # Base Case: 0! or 1! is 1
+ if n <= 1:
+ return 1
+ # Recursive Step: n * (n-1)!
+ return n * self.factorial(n - 1)
+
+def main():
+ # Create an instance of the class and call the method
+ mtools_instance = MTools()
+ number = 5
+ print(str(number) + "! is " + str(mtools_instance.factorial(number)))
+
+main()
+
+ + See if you can spot the differences in the Java version below. +
+
+
+public class MTools {
+ public static int factorial(int n) {
+ // Check for negative numbers
+ if (n < 0) {
+ System.out.println("Factorials are only defined on non-negative integers.");
+ return -1; // Return -1 to indicate error
+ }
+ // Base Case: 0! or 1! is 1
+ if (n <= 1) {
+ return 1;
+ }
+ // Recursive Step: n * (n-1)!
+ return n * factorial(n - 1);
+ }
+
+ public static void main(String[] args) {
+ int number = 5;
+ System.out.println(number + "! is " + factorial(number));
+ }
+}
+
+
+ Notice the key differences from Python: instead of
+ In many recursive algorithms, the recursive calls need extra information that the original caller shouldn't have to provide. For example, to recursively process an array, you need to keep track of the index of the current position. This extra information clutters the public-facing signature by forcing users to provide implementation details they shouldn't actually need to know about. +
+
+ First, let's see what happens if we try to write a recursive array sum function without using a helper method. In this approach, the user must provide the starting index, which is awkward and exposes implementation details.
+class ArrayProcessor:
+ def sum_array(self, arr, index):
+ """
+ This version forces users to provide the index parameter.
+ This is inconvenient and exposes implementation details.
+ """
+ # Base case: we've processed all elements
+ if index >= len(arr):
+ return 0
+
+ # Recursive step: current element + sum of remaining elements
+ return arr[index] + self.sum_array(arr, index + 1)
+
+def main():
+ processor = ArrayProcessor()
+ numbers = [1, 2, 3, 4, 5]
+ # Users must remember to start at index 0 - this is confusing!
+ result = processor.sum_array(numbers, 0)
+ print("The sum of " + str(numbers) + " is " + str(result))
+
+main()
+
+
+
+public class ArrayProcessor {
+ public static int sumArray(int[] arr, int index) {
+ // Base case: we've processed all elements
+ if (index >= arr.length) {
+ return 0;
+ }
+
+ // Recursive step: current element + sum of remaining elements
+ return arr[index] + sumArray(arr, index + 1);
+ }
+
+ public static void main(String[] args) {
+ int[] numbers = {1, 2, 3, 4, 5};
+ // Users must remember to start at index 0 - this is confusing!
+ int result = sumArray(numbers, 0);
+ System.out.println("The sum of [1, 2, 3, 4, 5] is " + result);
+ }
+}
+
+ + Both versions force users to understand and provide implementation details they shouldn't need to know about. Now let's see how helper methods solve this problem by providing a clean, user-friendly interface. Notice how the public method only requires the array itself, and the hidden recursive logic tracks the current index position. +
+
+
+class ArrayProcessor:
+ def sum_array(self, arr):
+ """
+ Public method that provides a clean interface for summing array elements.
+ Users only need to provide the array - no implementation details required.
+ """
+ if not arr: # Handle empty array
+ return 0
+ # Start the recursion at index 0
+ return self._sum_helper(arr, 0)
+
+ def _sum_helper(self, arr, index):
+ """
+ Private helper method that does the actual recursive work.
+ Tracks the current index position through the array.
+ """
+ # Base case: we've processed all elements
+ if index >= len(arr):
+ return 0
+
+ # Recursive step: current element + sum of remaining elements
+ return arr[index] + self._sum_helper(arr, index + 1)
+
+def main():
+ processor = ArrayProcessor()
+ numbers = [1, 2, 3, 4, 5]
+ result = processor.sum_array(numbers)
+ print("The sum of " + str(numbers) + " is " + str(result))
+
+main()
+
+
+ The same helper method pattern can be applied in Java, as shown in
+import java.util.Arrays;
+
+public class ArrayProcessor {
+ public static int sumArray(int[] arr) {
+ // Handle empty array
+ if (arr.length == 0) {
+ return 0;
+ }
+ // Start the recursion at index 0
+ return sumHelper(arr, 0);
+ }
+
+ private static int sumHelper(int[] arr, int index) {
+ // Base case: we've processed all elements
+ if (index >= arr.length) {
+ return 0;
+ }
+
+ // Recursive step: current element + sum of remaining elements
+ return arr[index] + sumHelper(arr, index + 1);
+ }
+
+ public static void main(String[] args) {
+ int[] numbers = {1, 2, 3, 4, 5};
+ int result = sumArray(numbers);
+ System.out.println("The sum of " + Arrays.toString(numbers) + " is " + result);
+ }
+}
+
+
+ Compare these improved versions with the earlier problematic ones. Notice how much cleaner the method calls become:
+ This helper method pattern is invaluable when your recursive algorithm needs to track additional state details (like array positions, accumulated values, or depth counters) that the original caller shouldn't need to know about or care about. It's a fundamental pattern and technique you'll likely use frequently in recursive problem solving. +
+
+ The consequence of running out of call stack space, is a concept you may have already encountered in Python. Java handles this in a very similar way to Python, both throwing an error when the call stack depth is exceeded.
+
+ In both languages, if you write a recursive function that doesn't have a base case or that just recurses too deeply, you'll eventually hit this limit. When this happens, Python will raise a
+ The following Python code in
+ def cause_recursion_error():
+ """
+ This function calls itself without a base case, guaranteeing an error.
+ """
+ cause_recursion_error()
+
+ print("Calling the recursive function... this will end in an error!")
+
+ # The line below will start the infinite recursion.
+ # Python will stop it and raise a RecursionError automatically.
+ # Each call adds a new layer to the program's call stack.
+ # Eventually, the call stack runs out of space, causing the error.
+ cause_recursion_error()
+
+
+ The following Java code in
+ public class Crash {
+ public static void causeStackOverflow() {
+ // The line below will start the infinite recursion.
+ // Java will stop it and raise a StackOverflowError automatically.
+ // Each call adds a new layer to the program's call stack.
+ // Eventually, the call stack runs out of space, causing the error.
+ causeStackOverflow();
+ }
+ // A main method is required to run the Java program.
+ public static void main(String[] args) {
+ System.out.println("Calling the recursive method... this will end in an error!");
+
+ causeStackOverflow();
+ }
+ }
+
+ Recursion solves problems by defining a base case and a recursive step; each call reduces the problem size until the base case is reached.
+Java methods must declare visibility, static/instance context, return type, and parameter types; e.g.,
The recursive logic in Java mirrors Python conceptually, but Java uses curly braces
The helper method pattern hides implementation details (like array indices) from callers, providing clean public interfaces while managing recursive state privately.
+Deep or unbounded recursion can exhaust the call stack: Python raises
Neither Java nor Python guarantees tail call optimization, so programmers should use iterative solutions for algorithms that would require very deep recursion.
+Recursive methods in Java must specify return types explicitly, unlike Python's dynamic typing, which affects how you handle error cases and return values.
+Which method signature and behavior best match a typical Java recursive factorial implementation?
+No. While this handles negative numbers, the base case is incorrect - factorial of 0 should be 1, not 0.
No. Printing results is fine for testing, but a proper factorial method should return the computed value.
Correct. This matches the standard recursive factorial definition in Java.
No. While this logic is close, it doesn't handle the case when n = 1, and using long as return type when int parameter is used creates inconsistency.
Why use a private helper method (e.g.,
Because it allows Java to automatically optimize the recursion for faster execution.
No. Java does not automatically optimize recursion just because you use a helper method.
To keep the public API simple while encapsulating extra recursion state (such as the current index) inside a private method.
Correct. This keeps the interface clean while hiding internal details from the caller.
Because public methods cannot take more than one parameter in recursive calls.
No. Public methods can take multiple parameters; this is about interface clarity, not parameter limits.
To eliminate the need for a base case by handling termination in the helper method automatically.
No. The helper method still needs an explicit base case to stop recursion.
Which statement about recursion limits and errors is accurate?
+When the call stack is exhausted, Python raises a
Correct. This difference in exception types and the lack of built-in tail call optimization is a key distinction between the two languages.
+Java automatically applies tail call optimization to recursive methods marked as
No. Java does not perform automatic tail call optimization, regardless of whether methods are marked as final.
+Declaring a recursive method as
No. The
The JVM can detect simple recursive patterns and automatically convert them to iterative loops to prevent stack overflow.
+No. The JVM does not automatically convert recursive methods to iterative ones. This optimization must be done manually by the programmer.
+
+ File handling is an integral part of programming. Most programming languages have the ability to create, read from, write to, and delete, files. In Python, most built-in libraries are available without needing to explicitly import additional packages, but some libraries like
+ import math
+ print(math.sqrt(25)) # notice the lower case 'm' in math
+
+
+ Delete the first line that says
+ import java.lang.Math;
+
+ public class SquareRoot {
+ public static void main(String[] args) {
+ System.out.println(Math.sqrt(25)); // notice the upper case 'M' in Math
+ }
+ }
+
+
+ Note the use of
+ Much like the
+ The
+ The
+ Finally, these last two classes provide error handling and must be used in tandem with the
+ We will now create a
+import java.io.File;
+
+public class CreateFile {
+ public static void main(String[] args) {
+ // First, create a File object that represents "myfile.txt"
+ File myFile = new File("myfile.txt");
+ // Next, print the file path (just the filename.)
+ System.out.println(myFile);
+ }
+}
+
+
+
+ Now let's learn how to make a file in Java. In Python. files can be made using the
+
+ filename = "newfile.txt"
+ print("Attempting to write to '" + filename + "' using 'w' mode...")
+ try:
+ with open(filename, 'w') as f:
+ f.write("This file was created using 'w' mode.")
+ print("SUCCESS: The file '" + filename + "' was created or overwritten.")
+ except Exception as e:
+ # This would only catch other unexpected errors
+ print("An unexpected error occurred during write: " + str(e))
+
+
+
+
+ import java.io.File;
+ import java.io.IOException;
+
+ public class CreateFile {
+ public static void main(String[] args) {
+ File myFile = new File("newfile.txt");
+ try {
+ if (myFile.createNewFile()) {
+ System.out.println("The file " + myFile.getName() + " was created successfully.");
+ } else {
+ System.out.println("The file " + myFile.getName() + " already exists.");
+ }
+ } catch (IOException e) {
+ // This code runs if an IOException occurs
+ System.out.println("An error occurred while creating the file.");
+ e.printStackTrace(); // This prints the stack trace for more detailed error info
+ }
+ }
+ }
+
+
+ You may have noticed the use of another method from the File class;
+ Let’s take a look at how we can use Python to understand how read file contents in Java. In order to read files generally you iterate through each line in the file and read the line's content. In Java, you read files in a very similar way, however in Java we will use the
+ Consider
+ 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 ++
+ filename = "myfile.txt"
+ try:
+ # Attempt to open the file in read mode ('r')
+ with open(filename, "r") as file_reader:
+ # Iterate over each line in the file
+ for line in file_reader:
+ print(line.strip())
+ except:
+ #catches if the file doesn't exist or can't be written to
+ print("file could not be opened")
+
+
+
+ import java.io.File;
+ import java.io.FileNotFoundException; // This import is necessary to handle the exception if the file is not found
+ import java.util.Scanner;
+ public class ReadFile {
+ public static void main (String[] args) {
+ String filename = "myfile.txt";
+ try (Scanner fileReader = new Scanner(new File(filename))) { // try to open the file and create a Scanner object
+ while (fileReader.hasNextLine()) { // while there is a next line in the file
+ String data = fileReader.nextLine();
+ System.out.println(data);
+ }
+ }
+ catch (FileNotFoundException e) { // and catch the exception if the file is not found
+ System.out.println("Error: The file '" + filename + "' was not found.");
+ }
+ }
+ }
+
+
+ You may have noticed that there are some new methods you haven't seen yet. The
+ The
+
+ Next, we will create a
+
+ In this next step, we will use the
+
+ The
+ Next, we will again add the required try/catch blocks utilizing the
+
+ And that's it! We will add our code to the foundational code for a complete program. First,
+ ++
+ try:
+ with open("myfile.txt", "w") as my_writer:
+ my_writer.write("File successfully updated!")
+ print("File successfully written to.")
+ except OSError:
+ print("An error occurred.")
+ import traceback
+ traceback.print_exc()
+
+
+
+ ++
+ import java.io.FileWriter;
+ import java.io.IOException;
+
+ public class WriteFile {
+ public static void main(String[] args) {
+ try {
+ FileWriter myWriter = new FileWriter("myfile.txt");
+ myWriter.write("File successfully updated!");
+ myWriter.close();
+ System.out.println("File successfully written to.");
+ } catch (IOException e) {
+ System.out.println("An error occurred.");
+ e.printStackTrace();
+ }
+ }
+ }
+ ~
+
+ If a file does not already exist (for example,
+ Speaking of overwriting data, what if we want to append text to the end of any text already in
+ Now, when we use
+ ++
+ import java.io.FileWriter;
+ import java.io.IOException;
+
+ public class WriteFile {
+ public static void main(String[] args) {
+ try {
+ FileWriter myWriter = new FileWriter("myfile.txt", true); // true enables append mode
+ myWriter.write("File successfully updated!");
+ myWriter.close();
+ System.out.println("File successfully written to.");
+ } catch (IOException e) {
+ System.out.println("An error occurred.");
+ e.printStackTrace();
+ }
+ }
+ }
+
+
+ Then if we run the program twice, the contents of
+ This doesn't look very good! If we want each additional write to appear on a new line? A simple solution is to use the
+ Running the code with the newline character twice will result in the following contents in myfile.txt as
+ Lastly, we will take a look at using Java to delete a file. This is pretty straight-forward and follows the structure used to create files.
+ import java.io.File;
+ import java.io.IOException;
+
+ public class CreateFile {
+ public static void main(String[] args) {
+ File myFile = new File("myfile.txt");
+ try {
+ if (myFile.createNewFile()) {
+ System.out.println("The file " + myFile.getName() + " was created successfully.");
+ } else {
+ System.out.println("The file " + myFile.getName() + " already exists.");
+ }
+ } catch (IOException e) {
+ // This code runs if an IOException occurs
+ System.out.println("An error occurred while creating the file.");
+ e.printStackTrace(); // This prints the stack trace for more detailed error info
+ }
+ }
+ }
+
+
+ And finally,
+import java.io.File;
+import java.io.IOException;
+
+public class DeleteFile {
+ public static void main(String[] args) {
+ try {
+ File myFile = new File("myfile.txt");
+
+ // Create the file (does nothing if it already exists)
+ myFile.createNewFile();
+ System.out.println("File created: " + myFile.getName());
+
+ // Delete the file
+ if (myFile.delete()) {
+ System.out.println("Deleted " + myFile.getName());
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+}
+
+
+
+ Note that this is almost identical to the code within the
To work with files in Java, you must import specific classes like
You can create a new file using
Reading from files is done using a
To write to a file, use a
You can delete a file using the
Which import is needed to create and manipulate files in Java?
+import java.util.File;
+No,
import java.io.File;
+Correct!
import java.file.Input;
+No, this is not a valid import for file operations.
+import java.system.io.*;
+No, there is no such package in Java.
+What does
It throws an exception.
+No, it only throws an exception for access errors, not for existing files.
+false
+Correct! It returns
true
+No,
null
+No,
Which method checks if a file has more lines to read using a Scanner?
+nextLine()
+No,
hasMore()
+No, this is not a method of
hasNextLine()
+Correct! This checks if there is another line available to read.
+canReadLine()
+No, this is not a standard method in the Scanner class.
++ Making mistakes is a very natural part of learning Java—or any other programming language. In fact, mistakes are an absolutely essential part of the learning process! So, try not to feel discouraged when you encounter errors in your code. Instead, view each mistake as an opportunity to deepen your understanding. Every programmer, no matter how experienced, encounters errors in their code. The key is to learn how to identify and correct these errors while also learning from them. +
++ The good news is that most errors happen for just a few common reasons, and once you recognize the patterns, they become much easier to fix. This chapter focuses on those typical mistakes and how to understand and correct them. +
++ One of the best ways to correct these errors is to slow down and test your code in small pieces. Write a few lines, compile, and check the output before moving on. If something goes wrong, read the first error message very carefully, and focus on fixing one problem at a time. Often, solving the first error helps fix others that follow. +
+
+ A simple debugging technique is to use
+ Consider the following example where we have a method that multiplies a number by two. We can add print statements to help us debug the code and verify that the method is being called correctly and returning the expected result as shown below in
+ // DebugExample.java
+ public class DebugExample {
+
+ public static void main(String[] args) {
+ int number = 10;
+ int result = multiplyByTwo(number);
+ // Debugging: print the result to verify the method worked
+ System.out.println("Result after multiplying: " + result);
+ }
+
+ public static int multiplyByTwo(int value) {
+ // Debugging: print the input value to check it's being passed correctly
+ System.out.println("multiplyByTwo received: " + value);
+ return value * 2;
+ }
+ }//End of class
+
+
+ In
+ Useful tools in the built-in Java debugger can help you step through your code, inspect variables, and evaluate expressions at runtime. Familiarizing yourself with these tools can greatly enhance your debugging efficiency. +
++ Above all, when you encounter an error, be patient with yourself. Every mistake you make is an opportunity to learn. +
+
+ A common mistake in Java is to forget that every statement must end with a semicolon (
+ // Histo.java
+ import java.util.Scanner; // Imports Scanner
+
+ public class Histo { // Class declaration
+
+ public static void main(String[] args) { // Main method declaration
+ Scanner data = null // The error will point here
+ System.out.println("This line will not compile.");
+ }// End of main method
+ }//End of class
+
+
+ The error "';' expected" on line 7 of
+ In Python, you can use a variable without declaring it first, but in Java, you must declare all variables before using them.
+
+ import java.util.ArrayList; // Import necessary class
+
+ public class Main { // Class declaration
+
+ // Main method declaration
+ public static void main(String[] args) {
+
+ // Attempt to use 'count' without declaration
+ count = new ArrayList<Integer>(10);
+
+ // This line won't be reached due to the error.
+ System.out.println("ArrayList created.");
+
+ } // End of main method
+ } // End of class
+
+
+ The 'cannot find symbol' error for the variable
+ In Python, many classes are available by default. However, in Java, you must explicitly import most classes from external packages that you want to use
+ // Histo.java
+ public class Histo { // Class declaration
+
+ public static void main(String[] args) { // Main method declaration
+ // This line will cause the "cannot find symbol" error
+ Scanner data = null;
+
+ } // End of main method
+ } // End of class
+
+
+ You may notice that this error message looks similar to the previous one
+ Unlike Python, where you can create a new object without explicitly using a keyword, Java requires the
+ // Histo.java // The filename for this example
+ import java.io.File; // Import the File class, needed for file operations
+ import java.util.Scanner; // Import the Scanner class, now correctly imported
+
+ public class Histo { // Class declaration
+
+ public static void main(String[] args) { // Main method declaration
+
+ Scanner data = Scanner(new File("test.dat")); // This Line will cause an error
+
+ } // End of main method
+ } // End of class
+
+
+ The error message in
+ Java is a statically typed language, meaning you must specify the type of objects that can be stored in a container like an
+ // UncheckedWarningDemo.java
+ import java.util.ArrayList; // Import the ArrayList class
+
+ public class UncheckedWarningDemo { // Class declaration
+
+ public static void main(String[] args) { // Main method declaration
+
+ // This is where the potential for unchecked operations begins.
+ ArrayList rawList = new ArrayList();
+
+ rawList.add("Hello");
+ //This is the line that will throw an error
+ System.out.println("Element added: " + rawList.get(0));
+ } // End of main method
+ } // End of class
+
+
+ This is a compiler warning, not an error, indicating a potential type safety issue. It occurs because you are calling the
+ When
In Java, every variable must be declared with its type before use; undeclared variables cause compilation errors.
+Java requires explicit import statements for classes from external packages (e.g.,
The
Every Java statement must end with a semicolon (
Java uses generics for type safety in containers like
Compiler error messages may sometimes be misleading; understanding common mistakes helps quickly identify the root cause.
+What happens if you use a variable in Java without declaring it first?
+The compiler gives an error indicating the variable cannot be found.
Correct! Java requires all variables to be declared before use.
The variable is automatically declared as type
No. Java does not implicitly declare variables.
The program compiles but throws an error at runtime.
No. This is a compile-time error.
Java ignores the variable and continues compiling.
No. Java will stop compiling with an error.
Why must you include import statements for classes like
Because these classes belong to external packages and are not automatically available.
Correct! Java requires explicit imports for external classes.
Because Java does not support standard input without imports.
No. Standard input is supported but needs the
Because the classes are only available in Python, not Java.
No. This is a Java-specific requirement.
Because the compiler ignores unknown classes without imports.
No. It causes a compile error instead.
What warning occurs when you use an
An "unchecked" warning indicating potential type safety issues.
Correct! Using raw types disables generic type checks.
A syntax error.
No. This is a compiler warning, not an error.
A runtime exception.
No. It only warns about possible runtime errors.
A logical error in the program.
No. The warning points out type safety concerns.
- This book assumes that you are already familiar with the
-- --
-
- -- -
- -- Data Types -
-- -
- -- Loops -
-- -
- -- Reading user input -
-- -
-- Conditionals -
-
- Once we have the basics of Java behind us we will move on to look at the features of Java that are both unique and powerful. -
- --- --
-
- -- -
- -- Classes -
-- -
- -- Interfaces -
-- -
- -- Collections -
-- -
-- Graphical User Interface Programming -
-- -
-- Generic Programming -
-
- Thank you to Beryl Hoffman for contributing to this section from her CSAwesome: AP Java Programming book. -
-
- The tool that we use to compile a Java source file into a Java class file
- is called a
- You can learn Java by just using the interactive coding panels called
-
- However, it’s a good idea to also try a Java IDE to build code outside of - this e-book, especially to try coding with user input which Active Code - cannot do. There are many Java IDEs available. -
-- There are a lot of online cloud IDEs where you can code online in many - programming languages. Most are free to start, but offer different - features for a price. These are great options if you are using a - Chromebook or you cannot install software on your computer or you want an - easy no-installation option. Some of the Active Code samples in this - e-book also include a link to online IDEs. These projects can be copied to - make your own copy. -
- -Here are some popular online IDEs:
--
- CodeHS (
- PickCode (
- Replit (
- JuiceMind (
- Github provides many free opportunities for students and teachers (
- Students will need to sign up for a free Github account (
- VSCode (
- DrJava (from
- BlueJ (
- jGRASP (
- IntelliJ (
- Netbeans (
- Eclipse (
- Python is a nice language for beginning programming for several reasons. - First the syntax is sparse, and clear. - Second, the underlying model of how objects and variables work is very consistent. - Third, you can write powerful and interesting programs without a lot of work. - However, Python is representative of one kind of language, called a dynamic language. - You might think of Python as being fairly informal. - There are other languages, like Java and C++ that are more formal. -
- -
- These languages have some advantages of their own.
- First, is speed: Java and C++ code will generally give better performance than Python code. (See
- In one sense Python is representative of a whole class of languages, sometimes referred to as “scripting languages.” Other languages in the same category as Python are Ruby and Perl. - Java is representative of what I will call industrial strength languages. - Industrial strength languages are good for projects with several people working on the project where being formal and careful about what you do may impact lots of other people. - Languages in this category include Rust, C++, C#, and Ada. -
- -- Programming languages will always change. - As the field of computer science advances there will be new programming languages and you will need to learn them. - It is important to learn several programming languages so that you know what to expect. - There are certain features that most programming languages have in common; variables, loops, conditionals, functions. - And there are some features that are unique. - If you know what is common in languages that is a good place to start. -
-
-
- Although Python code is generally slower than Java and C++ code, in practice Python programs can achieve equivalent performance.
- This can be done by compiling Python code to C code (see:
- It is easier to learn to create interesting programs in Java than in C or C++, for several reasons: -
- --
- Java includes a larger standard library than C or C++, which means that sophisticated programs can be created in Java without including external dependencies. - Java has over 4,000 different classes included in the Java 14 Standard Edition. - We could not begin to scratch the surface of these classes even if we devoted all of class time! However, we will cover many useful and powerful features of the Java standard library this semester. -
-- Java incorporates automatic garbage collection of memory, whereas C and C++ programs typically include some degree of manual memory management. - This makes programming in those languages more challenging. -
-
- C++’s syntax is more complicated than Java’s, making it more difficult to learn.
- For example, C++ supports a feature called operator overloading, which makes it possible to change the behavior of operators like
- Certainly, C and C++ are important languages, and are worth learning. - But for these and other reasons, we’ve decided to use Java for this course. - Learning Java will be a good preparation for learning these and other languages! -
- - -- One of the great things about Python is that all of the basic data types are objects. - Integers are objects, floating point numbers are objects, lists are objects, everything. - In Java that is not the case. - In Java, some of the most basic data types like integers and floating point numbers are not objects. - The benefit of having these primitive data types be non-objects is that operations on the primitives are fast. - The problem is that it became difficult for programmers to combine objects and non-objects in the way that we do in Python. - So, eventually all the non-object primitives ended up with Objectified versions. -
- -- In older versions of Java, it was the programmers responsibility to convert back and forth from a primitive to an object whenever necessary. - This process of converting a primitive to an object was called “boxing.” The reverse process is called “unboxing.” In Java 5, the compiler became smart enough to know when to convert back and forth and is called “autoboxing.” In this book, we will typically use the Object version of all the numeric data types and let the compiler do its thing. -
- -- Let’s look at a simple Python function which converts a Fahrenheit temperature to Celsius. - If this program were run on the command-line, you would enter the temperature when prompted – the Javascript pop-up for input is only an artifact of the digital textbook. -
- - -
-def main():
- fahr = int(input("Enter the temperature in F: "))
- cel = (fahr - 32) * 5.0/9.0
- print("the temperature in C is: ", cel)
-main()
- - Next, lets look at the Java equivalent. If this program were run on the command-line, you would enter the temperature when prompted – the “Input for Program” text box is only an artifact of the digital textbook. -
- - -
-import java.util.Scanner;
-public class TempConv {
- public static void main(String[] args) {
- Double fahr;
- Double cel;
- Scanner in;
- in = new Scanner(System.in);
- System.out.println("Enter the temperature in F: ");
- fahr = in.nextDouble();
- cel = (fahr - 32) * 5.0/9.0;
- System.out.println("The temperature in C is: " + cel);
- }
-}
- - There are several new concepts introduced in this example. We will look at them in the following order: -
- --
- Import -
-- Variable Declaration -
-- Input/Output and the Scanner Class -
-- In Java, you can use any class that is available without having to import the class, subject to two very important conditions: -
- --
- The
- You must use the full name of the class -
-
- Your first question might be how do the
-
- Java knows about all the classes that are defined in .java and .class files in your current working directory. -
-- Java knows about all the classes that are shipped with Java. -
-
- Java knows about all the classes that are included in your
-
- A .jar file that contains Java classes -
-- Another directory that contains Java class files -
-
- You can think of the import statement in Java as working a little bit like the
- So, what exactly does the import statement do? What it does is tell the compiler that we are going to use a shortened version of the class’s name. In this example we are going to use the class
- Here is where we run into one of the most important differences between Java and Python. Python is a
- In the example above, lines 5—7 contain variable declarations. Specifically we are saying that
- For Python programmers, the following error is likely to be even more common. Suppose we forgot the declaration for
TempConv.java:13: cannot find symbol symbol : variable cel location: class TempConv cel = (fahr - 32) * 5.0/9.0; ^ TempConv.java:14: cannot find symbol symbol : variable cel location: class TempConv System.out.println("The temperature in C is: " + cel); ^ 2 errors
-
- When you see the first kind of error, where the symbol is on the left side of the equals sign, it usually means that you have not declared the variable. If you have ever tried to use a Python variable that you have not initialized the second error message will be familiar to you. The difference here is that we see the message before we ever try to test our program. More common error messages are discussed in the section
- The general rule in Java is that you must decide what kind of an object your variable is going to reference and then you must declare that variable before you use it. There is much more to say about the static typing of Java, but for now this is enough. -
-
- In the previous section we created a
- On line 11 we use the
- The table below shows some commonly used methods of the
- Strings in Java and Python are quite similar. Like Python, Java strings are immutable. However, manipulating strings in Java is not quite as obvious since Strings do not support an indexing or slicing operator. That is not to say that you can’t index into a Java string, you can. You can also pull out a substring just as you can with slicing. The difference is that Java uses method calls where Python uses operators. -
- -- In fact, this is the first example of another big difference between Java and Python. Java does not support any operator overloading. Table 3 maps common Python string operations to their Java counterparts. For the examples shown in the table we will use a string variable called “str” -
- -- Next, let’s look at a program which reads numbers from a file and produces a histogram showing the frequency of the numbers. The data file we will use has one number between 0 and 9 on each line of the file. Here is a simple Python program that creates and prints a histogram. -
- - -
-def main():
- count = [0]*10
- data = open('test.dat')
- for line in data:
- count[int(line)] = count[int(line)] + 1
- idx = 0
- for num in count:
- print(idx, " occured ", num, " times.")
- idx += 1
-main()
- - Test running the program. It will read this data: -
-- 1 - 2 - 3 - 9 - 1 -
-Lets review what is happening in this little program. First, we create a list and initialize the first 10 positions in the list to be 0. Next we open the data file called ‘test.dat’. Third, we have a loop that reads each line of the file. As we read each line we convert it to an integer and increment the counter at the position in the list indicated by the number on the line we just read. Finally we iterate over each element in the list, printing out both the position in the list and the total value stored in that position. -
- -
-To write the Java version of this program we will have to introduce several new Java concepts. First, you will see the Java equivalent of a list, called an
-
-
-
-The
-
-
-The
-
-
-The
-Here is the Java code needed to write the exact same program: -
- -
- The Java code below contains the first mention of the
-import java.util.Scanner;
-import java.util.ArrayList;
-import java.io.File;
-import java.io.IOException;
-public class Histo {
- public static void main(String[] args) {
- Scanner data = null;
- ArrayList<Integer> count;
- Integer idx;
- try {
- data = new Scanner(new File("test.dat"));
- }
- catch ( IOException e) {
- System.out.println("Unable to open data file");
- e.printStackTrace();
- System.exit(0);
- }
- count = new ArrayList<Integer>(10);
- for (Integer i = 0; i < 10; i++) {
- count.add(i,0);
- }
- while(data.hasNextInt()) {
- idx = data.nextInt();
- count.set(idx,count.get(idx)+1);
- }
- idx = 0;
- for(Integer i : count) {
- System.out.println(idx + " occured " + i + " times.");
- idx++;
- }
- }
-}
- - Before going any further, I suggest you try to compile the above program and run it on some test data that you create. -
- -
- Now, let’s look at what is happening in the Java source. As usual, we declare the variables we are going to use at the beginning of the method. In this example we are declaring a
- Technically, you don’t have to declare what is going to be in an array list. The compiler will allow you to leave the
Note: Histo.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.-
- Without the
- Lines 13—20 are required to open the file. Why so many lines to open a file in Java? The additional code mainly comes from the fact that Java forces you to reckon with the possibility that the file you want to open is not going to be there. If you attempt to open a file that is not there you will get an error. A try/catch construct allows us to try things that are risky, and gracefully recover from an error if one occurs. The following example shows the general structure of a try/catch block. -
- -try { Put some risky code in here, like opening a file } catch (Exception e) { If an error happens in the try block an exception is thrown. We will catch that exception here! }
-
- Notice that in line 16 we are catching an
- On line 22 we create our
- The syntax of this for loop probably looks very strange to you, but in fact it is not too different from what happens in Python using range. In fact
-
-
-
-
-
- The next loop (lines 27–30) shows a typical Java pattern for reading data from a file. Java while loops and Python while loops are identical in their logic. In this case, we will continue to process the body of the loop as long as
- Line 29 illustrates another important difference between Python and Java. Notice that in Java we can not write
- The last loop in this example is similar to the Python for loop where the object of the loop is a Sequence. In Java we can use this kind of for loop over all kinds of sequences, which are called Collection classes in Java. The for loop on line 33
- As I said at the outset of this section, we are going to use Java
-import java.util.Scanner;
-import java.io.File;
-import java.io.IOException;
-public class HistoArray {
- public static void main(String[] args) {
- Scanner data = null;
- Integer[] count = {0,0,0,0,0,0,0,0,0,0};
- Integer idx;
- try {
- data = new Scanner(new File("test.dat"));
- }
- catch ( IOException e) {
- System.out.println("Unable to open data file");
- e.printStackTrace();
- System.exit(0);
- }
- while(data.hasNextInt()) {
- idx = data.nextInt();
- count[idx] = count[idx] + 1;
- }
- idx = 0;
- for(Integer i : count) {
- System.out.println(idx + " occured " + i + " times.");
- idx++;
- }
- }
-}
-
- The main difference between this example and the previous example is that we declare
- Just as Python provides the dictionary when we want to have easy access to key-value pairs, Java also provides us a similar mechanism. Rather than the dictionary terminology, Java calls these objects Maps. Java provides two different implementations of a map, one is called the
- Lets stay with a simple frequency counting example, only this time we will count the frequency of words in a document. A simple Python program for this job could look like this: -
- - -
-def main():
- data = open('alice30.txt')
- wordList = data.read().split()
- count = {}
- for w in wordList:
- w = w.lower()
- count[w] = count.get(w,0) + 1
- keyList = sorted(count.keys())
- for k in keyList:
- print("%-20s occurred %4d times" % (k, count[k]))
-main()
-
- This program reads the file
- Down, down, down. Would the fall NEVER come to an end! - 'I wonder how many miles I've fallen by this time?' she said aloud. 'I must - be getting somewhere near the centre of the earth. - Let me see: that would be four thousand miles down, I think--' - (for, you see, Alice had learnt several things of this sort in her lessons - in the schoolroom, and though this was not a VERY good opportunity for - showing off her knowledge, as there was no one to listen to her, still it - was good practice to say it over) '--yes, that's about the right distance - --but then I wonder what Latitude or Longitude I've got to?' - (Alice had no idea what Latitude was, or Longitude either, - but thought they were nice grand words to say.)
- Notice that the structure of the program is very similar to the numeric histogram program. -
- - -
-import java.util.Scanner;
-import java.util.ArrayList;
-import java.io.File;
-import java.io.IOException;
-import java.util.TreeMap;
-public class HistoMap {
- public static void main(String[] args) {
- Scanner data = null;
- TreeMap<String,Integer> count;
- Integer idx;
- String word;
- Integer wordCount;
- try {
- data = new Scanner(new File("alice30.txt"));
- }
- catch ( IOException e) {
- System.out.println("Unable to open data file");
- e.printStackTrace();
- System.exit(0);
- }
- count = new TreeMap<String,Integer>();
- while(data.hasNext()) {
- word = data.next().toLowerCase();
- wordCount = count.get(word);
- if (wordCount == null) {
- wordCount = 0;
- }
- count.put(word,++wordCount);
- }
- for(String i : count.keySet()) {
- System.out.printf("%-20s occured %5d times\n", i, count.get(i) );
- }
- }
-}
- - Improve the program above to remove the punctuation. -
-All variables must be declared with their type before use in Java due to static typing.
-Java has primitive types (
The
Java Strings are immutable like Python, but use methods like
Maps (
What is the correct way to declare an ArrayList that will hold String objects in Java?
-ArrayList<String> list;
-Good job!
-ArrayList list;
-No, you must specify the type of objects it will hold using generics.
-ArrayList[String] list;
-No, the syntax for generics uses < > brackets, not [ ].
-ArrayList() list;
-No, this is not a valid declaration.
-The process of automatically converting between primitive types and their Object wrapper classes in Java is called:
-Autoboxing
-Right! Good job!
-Casting
-No, casting is manually converting a type.
-Overloading
-No, overloading refers to methods, not type conversion.
-Unboxing
-No, unboxing is the reverse of autoboxing.
-Which method would you use to get the character at position 3 in a Java String called str?
-str.charAt(3)
-Correct!
-str[3]
-No, Java does not support this type of indexing.
-str.get(3)
-No, this method does not exist in Java.
-str.substring(3)
-No, this will return a substring starting at position 3, not a single character.
-- Conditional statements in Python and Java are very similar. - In Python we have three patterns: -
- -- In Python the simple if statement is written as: -
- -
-score = 95
-if score >= 90:
- print("Excellent work!")
-
-
- In Java, this same pattern requires two changes: the condition must be in parentheses (), and the code block must be enclosed in curly braces {}.
-
- public class SimpleIfExample {
- public static void main(String[] args) {
- int score = 70;
- if (score <= 70) {
- System.out.println("Needs work!");
- }
- }
- }
-
- - Once again you can see that in Java the curly braces define a block rather than indentation. - In Java the parenthesis around the condition are required because it is technically a function that evaluates to True or False. -
-The Java equivalent follows the same syntactical rules as before.
-
- age = 16
- if age >= 18:
- print("You can vote.")
- else:
- print("You are not yet eligible to vote.")
-
-
- public class IfElseExample {
- public static void main(String[] args) {
- int age = 16;
- if (age >= 18) {
- System.out.println("You can vote.");
- } else {
- System.out.println("You are not yet eligible to vote.");
- }
- }
- }
-
- - Java does not have an elif pattern like Python. - In Java you can get the functionality of an elif statement by nesting if and else. - Here is a simple example in both Python and Java. -
- - -
-grade = int(input('enter a grade'))
-if grade < 60:
- print('F')
-elif grade < 70:
- print('D')
-elif grade < 80:
- print('C')
-elif grade < 90:
- print('B')
-else:
- print('A')
- -In Java we have a couple of ways to write this -
- - -
-public class ElseIf {
- public static void main(String args[]) {
- int grade = 85;
- if (grade < 60) {
- System.out.println('F');
- } else {
- if (grade < 70) {
- System.out.println('D');
- } else {
- if (grade < 80) {
- System.out.println('C');
- } else {
- if (grade < 90) {
- System.out.println('B');
- } else {
- System.out.println('A');
- }
- }
- }
- }
- }
- }
- -We can get even closer to the elif statement by taking advantage of the Java rule that a single statement does not need to be enclosed in curly braces. Since the if is the only statement used in each else we can get away with the following. -
- - -
-public class ElseIf {
- public static void main(String args[]) {
- int grade = 85;
- if (grade < 60) {
- System.out.println('F');
- } else if (grade < 70) {
- System.out.println('D');
- } else if (grade < 80) {
- System.out.println('C');
- } else if (grade < 90) {
- System.out.println('B');
- } else System.out.println('A');
- }
-}
-
-Java also supports a
-public class SwitchUp {
- public static void main(String args[]) {
- int grade = 85;
- int tempgrade = grade / 10;
- switch(tempgrade) {
- case 10:
- case 9:
- System.out.println('A');
- break;
- case 8:
- System.out.println('B');
- break;
- case 7:
- System.out.println('C');
- break;
- case 6:
- System.out.println('A');
- break;
- default:
- System.out.println('F');
- }
- }
- }
-
-The
-The conditionals used in the if statement can be boolean variables, simple comparisons, and compound boolean expressions. -
- -
-Java also supports the boolean expression.
- You have already seen a couple of examples of iteration and looping in Java. - So this section will just serve as a reference for the differences in syntax. -
- -
-for i in range(10):
- print(i)
-
- - In Java, we would write this as: -
- -
-public class DefiniteLoopExample {
- public static void main(String[] args) {
- for (Integer i = 0; i < 10; i++ ) {
- System.out.println(i);
- }
- }
-}
-
-
- Recall that the
range(stop) -range(start,stop) -range(start,stop,step)- -
- The Java
for (start clause; stop clause; step clause) {
- statement1
- statement2
- ...
-}
-
- - If you want to start at 100, stop at 0 and count backward by 5, the Python loop would be written as: -
- -
-for i in range(100, -1, -5):
- print(i)
-
- - In Java, we would write this as: -
- -
-public class DefiniteLoopBackward {
- public static void main(String[] args) {
- for (Integer i = 100; i >= 0; i -= 5) {
- System.out.println(i);
- }
- }
-}
-
-
- In Python, the
- In Python, we can iterate over a list as follows: -
- -
-l = [1, 1, 2, 3, 5, 8, 13, 21]
-for fib in l:
- print(fib)
-
-
- In Java we can iterate over an
-import java.util.ArrayList;
-
-public class ForEachArrayListExample {
- public static void main(String[] args) {
- ArrayList<Integer> l = new ArrayList<Integer>();
- l.add(1);
- l.add(1);
- l.add(2);
- l.add(3);
- l.add(5);
- l.add(8);
- l.add(13);
- l.add(21);
- for (Integer i : l) {
- System.out.println(i);
- }
- }
-}
-
- - This example stretches the imagination a bit, and in fact points out one area where Java's primitive arrays are easier to use than an array list. - In fact all primitive arrays can be used in a for each loop. -
- -
-public class ForEachArrayExample {
- public static void main(String[] args) {
- int l[] = {1,1,2,3,5,8,13,21};
- for(int i : l) {
- System.out.println(i);
- }
- }
-}
-
- - To iterate over the characters in a string in Java do the following: -
- -
-public class StringIterationExample {
- public static void main(String[] args) {
- String t = "Hello World";
- for (char c : t.toCharArray()) {
- System.out.println(c);
- }
- }
-}
-
-
-i = 5
-while i > 0:
- print(i)
- i = i - 1
-
- - In Java we add parenthesis and curly braces. Here is the same countdown loop in Java: -
-
-public class WhileLoopExample {
- public static void main(String[] args) {
- int i = 5;
- while (i > 0) {
- System.out.println(i);
- i = i - 1;
- }
- }
-}
-
-
-public class DoWhileExample {
- public static void main(String[] args) {
- int i = 10;
- do {
- System.out.println("This runs once, i = " + i);
- } while (i < 5);
- }
-}
-
- - Java has some very handy naming conventions. -
- --
- Class names always start with an upper case letter.
- For example,
- Method names always start with a lower case letter, and use camelCase to represent multiword method names.
- for example
- Instance variables of a class start with a lower case letter and use camelCase -
-
- Constants are in all upper case letters.
- for example
- In Python, you can use a variable without declaring it first, but in Java, you must declare all variables before using them. - If you try to use a variable that has not been declared, the Java compiler will give you an error message like this: -
-
- import java.util.ArrayList; // Import necessary class
-
- public class Main { // Class declaration
-
- // Main method declaration
- public static void main(String[] args) {
-
- // Attempt to use 'count' without declaration
- count = new ArrayList<Integer>(10);
-
- // This line won't be reached due to the error.
- System.out.println("ArrayList created.");
-
- } // End of main method
- } // End of class
-
-
- The 'cannot find symbol' error for the variable
- In Python, many classes are available by default. However, in Java, you must explicitly import most classes from external packages that you want to use. -
-
- // Histo.java
- public class Histo { // Class declaration
-
- public static void main(String[] args) { // Main method declaration
- // This line will cause the "cannot find symbol" error
- Scanner data = null;
-
- } // End of main method
- } // End of class
-
-
- You may notice that this error message looks similar to the previous one, however, it has an entirely different cause. In Java, classes like
- Unlike Python, where you can create a new object without explicitly using a keyword, Java requires the
- // Histo.java // The filename for this example
- import java.io.File; // Import the File class, needed for file operations
- import java.util.Scanner; // Import the Scanner class, now correctly imported
-
- public class Histo { // Class declaration
-
- public static void main(String[] args) { // Main method declaration
-
- Scanner data = Scanner(new File("test.dat")); // This Line will cause an error
-
- } // End of main method
- } // End of class
-
-
- This error message occurs when you forget to use the
- A common mistake in Java is to forget that every statement must end with a semicolon (
- // Histo.java
- import java.util.Scanner; // Imports Scanner
-
- public class Histo { // Class declaration
-
- public static void main(String[] args) { // Main method declaration
- Scanner data = null // The error will point here
- System.out.println("This line will not compile.");
- }// End of main method
- }//End of class
-
-
- The error "';' expected" on line 7 of
- Java is a statically typed language, meaning you must specify the type of objects that can be stored in a container like an
- // UncheckedWarningDemo.java
- import java.util.ArrayList; // Import the ArrayList class
-
- public class UncheckedWarningDemo { // Class declaration
-
- public static void main(String[] args) { // Main method declaration
-
- // This is where the potential for unchecked operations begins.
- ArrayList rawList = new ArrayList();
-
- rawList.add("Hello");
- //This is the line that will throw an error
- System.out.println("Element added: " + rawList.get(0));
- } // End of main method
- } // End of class
-
-
- This is a compiler warning, not an error, indicating a potential type safety issue. It occurs because you are calling the
- When
This is a quick reference guide for Java syntax and concepts.
+