diff --git a/source/ch2_firstjavaprogram.ptx b/source/ch2_firstjavaprogram.ptx
index d911d4b..2b61e68 100644
--- a/source/ch2_firstjavaprogram.ptx
+++ b/source/ch2_firstjavaprogram.ptx
@@ -1,506 +1,540 @@
-
-
-
Java Programs
-
- Classes and Objects
-
-
- object-oriented programming OOP
- Depending on how deep your knowledge of Python and programming in general is, you may or may not be familiar with classes and objects. These two important Object-Oriented Programming (OOP) concepts will briefly be discussed. If you already have a good understanding of classes and objects in Python, this section may be skipped.
-
-
-
- object
- attribute
- instance variable
- method
- Objects in the context of programming are instances of classes. Objects contain attributes (also referred to as instance variables), which are data that describe the object or are associated with the object, and methods, which are special functions used by the object. Methods are typically actions the object can perform, or can be used to make changes to the object's attributes.
-
-
-
- class
- constructor
- Classes can be thought of as being similar to blueprints or a recipe; they hold details of how to create an instance of an object. Classes contain a special method called a constructor that is used to create an instance of an object. Once the object is created, it will use the class definition to define its attributes and call methods.
-
-
-
- The best way to understand classes and objects is to see them in action. Let's define a Dog class in Python:
-
-
-
-
- class Dog:
- def __init__(self, name, breed, fur_color):
- self.name = name
- self.breed = breed
- self.fur_color = fur_color
- self.trained = False
- print("Dog named " + self.name + " created!")
-
- def bark(self):
- print(self.name + " says woof!")
-
- def sit(self):
- if self.trained:
- print(self.name + " sits.")
- else:
- print(self.name + " has not been trained.")
-
- def train(self):
- self.trained = True
-
-
-
-
- Let's unpack what is going on in this code. The first line is where we declare the class definition and name it Dog. Next, we have a special method called __init__. This __init__ method is the constructor and is required for every Python class definition. Within the __init__ method, attributes are defined. As you can see, the attributes name, breed, and fur_color must be defined when creating a Dog object using this class definition, but the trained attribute is defined within the constructor and is initialized as False. We can also have the __init__ method run any code, such as the print statement informing us that a Dog object was created.
-
-
-
- The next three blocks of code are the class's methods. These include bark(self), sit(self), and train(self). As you can see, the class defines attributes (the variables in the __init__ method) and methods for instances of the Dog class.
-
-
-
- self
- Within each method, and for each attribute, you will notice the use of self. This is required in Python. self simply indicates that an attribute or method is being used for a specific instance of an object created with a class.
-
-
-
- Next, we will use this class to create a new Dog object. We will call this new Dog object my_dog:
-
-
-
-
- class Dog:
- def __init__(self, name, breed, fur_color):
- self.name = name
- self.breed = breed
- self.fur_color = fur_color
- self.trained = False
- print("Dog named " + self.name + " created!")
-
- def bark(self):
- print(self.name + " says woof!")
-
- def sit(self):
- if self.trained:
- print(self.name + " sits.")
- else:
- print(self.name + " has not been trained.")
-
- def train(self):
- self.trained = True
-
-
- my_dog = Dog("Rex", "pug", "brown")
-
-
-
-
- In the final line of code, we have created an object called my_dog. We have initialized its attributes, setting name to Rex, breed to pug, and fur_color to brown.
-
-
-
- Now that we have created a Dog object using the class we defined, we can utilize the class's methods:
-
-
-
-
- class Dog:
- def __init__(self, name, breed, fur_color):
- self.name = name
- self.breed = breed
- self.fur_color = fur_color
- self.trained = False
- print("Dog named " + self.name + " created!")
-
- def bark(self):
- print(self.name + " says woof!")
-
- def sit(self):
- if self.trained:
- print(self.name + " sits.")
- else:
- print(self.name + " has not been trained.")
-
- def train(self):
- self.trained = True
-
+
+ First Java Program
- my_dog = Dog("Rex", "pug", "brown")
- my_dog.bark()
- my_dog.sit()
-
-
-
-
- When running the code above, the line Rex has not ben trained. will appear in the output when calling the sit() method. Try adding a one or more lines of code so that Rex sits. appears in the output!
+ 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.
-
-
-
- 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 in the next section.
-
-
-
-
-
- Lets look at a Java Program
-
- 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:
-
+
+
+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:
-
+
+ Next, let's look at a more "complicated" version of hello world for Python:
+
- >>> main() "Hello World!" >>>
-
- Now lets look at the same program written in Java:
-
+
+
+def main():
+ print ("Hello World!")
+main()
+
+
+
+
+
+ This is a bit more complicated, but it is still very simple. We have defined a function called main and then we call that function.
+ Java code will look more like this more complicated version of hello world than the simpler version.
+
+
+
+ Now lets look at the same program written in Java:
+
-
-
+
+
public class Hello {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
-
-
+
+
+
-
- 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.
-
+
+ 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.
+
-
- interpreter
- compile
- The first question you probably have about this little program is “How do I run it?” Running a Java program is not as simple as running a Python program. The first thing you need to do with a Java program is compile it. The first big difference between Java and Python is that Python is an interpreted language. We could run our Python programs in the Python interpreter and we were quite happy to do that. Java makes running programs a two step process. First we must type the hello world program into a file and save that file using the name Hello.java The file name must be the same as the public class you define in the file. Once we have saved the file we compile it from the command line as follows:
-
+
+ What This Means for You:
+ Coming from Python, you'll notice that Java is more "verbose" - you write more to say the same thing.
+ Java catches type errors before your program runs (this is actually helpful!).
+ Java's structure is more rigid, but this makes large programs easier to maintain.
+ The extra syntax becomes natural with practice.
+
+
+
+ What Stays the Same:
+ The core programming concepts you already know - if-else logic, for and while loops, variables, and functions, all work similarly in Java.
+ You're not re-learning how to program again, you are just learning a new way to express the same ideas.
+
+
+ Compiling and Running Java Programs
-
-
+
+ interpreter
+ interpreted language
+ compile
+ A natural question that you may have about this little program is "How would I run it on my own computer?" Running a Java program on your computer is not as simple as running a Python program because the first thing you need to do with a Java program is to compile it, which is not needed in Python. Python is an interpreted language. We could run our Python programs in the Python interpreter and we can be quite happy to do only that. Java makes running programs a two-step process. First, we must type the hello world program into a file and save that file using the filename Hello.java because the file name must be the same as the public class that you define in the file. Once we have saved the file, we compile it either in our IDE or from the command line as follows:
+
+
+
+
$ javac Hello.java
$ ls -l Hello.*
-rw-r--r-- 1 bmiller bmiller 391 Jul 19 17:47 Hello.class
-rw-r--r-- 1 bmiller bmiller 117 Jul 19 17:46 Hello.java
-
-
+
+
+
+
+ byte code
+ javac
+ The command javac compiles our Java source code into compiled byte code and saves it in a file called Hello.class.
+ Hello.class is a binary file, so you won't learn much if you try to examine the class file with an editor.
+ Hopefully you didn't make any mistakes, but if you did you may want to consult the section for helpful hints on compiler errors.
+
+
+
+ Now that we have compiled our Java source code we can run the compiled code using the java command.
+
+
+
+
+$ java Hello
+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:
+
+
+
+
+
+
+
+ JVMJava Virtual Machine
+ byte code
+ The job of the compiler is to turn your java code into language that the Java virtual machine (JVM) can understand.
+ We call the code that the JVM understands byte code.
+ The JVM interprets the byte code much like the Python interpreter interprets your Python.
+ However since byte code is much closer to the native language of the computer it can run faster.
+
+
+
+ When the compiler does the translation it can find many different kinds of errors.
+ For example, if you make a typo, the compiler will find the typo and point it out to you before you ever run the program.
+ We will look at some examples of compiler errors shortly.
+ Chances are you will create some on your own very soon, too.
+
+
+
-
- The command javac compiles our java source code into compiled byte code and saves it in a file called Hello.class.
- Hello.class is a binary file so you won’t learn much if you try to examine the class file with an editor.
- Hopefully you didn’t make any mistakes, but if you did you may want to consult the section for helpful hints on compiler errors.
-
+
+ Key Language Differences
-
- Now that we have compiled our java source code we can run the compiled code using the java command.
-
+
+ Now that you've seen your first Java program, let's examine the key differences between Java and Python more systematically. Some differences can be immediately observed in their syntax and structure.
+
+
+ Python vs Java: Key Differences
+
+
+ | Aspect |
+ Python |
+ Java |
+
+
+
+ | Variable Declaration |
+ x = 5 |
+ int x = 5; |
+
+
+
+ | Code Blocks |
+ Indentation (tabs/spaces) |
+ Curly braces { } |
+
+
+
+ | Line Endings |
+ Optional |
+ Semicolons ; required |
+
+
+
+ | Type Checking |
+ Dynamic (runtime) |
+ Static (compile time) |
+
+
+
+
-
-
-$ java Hello
-Hello World!
-$
-
-
+
+ The above differences are easy to identify and understand, but there are some more subtle differences that will take a bit more time to grasp. The most important of these is how Java organizes all code around classes and objects which we will cover in the next section.
+
-
- 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:
-
+
+ Understanding the Structure
+
+
+ But, before we move on, let's go back and look at our Hello World program carefully to see what we can learn about the Java language.
+ This simple example illustrates a few very important rules:
+
+
+
+
+ -
+
+ Every Java program must define a class, and all code is inside a class
+
+
+ -
+
+ Everything in Java must have a type
+
+
+ -
+
+ Every Java program must have a function called public static void main(String[] args)
+
+
+
+
+
+
+ Let's take the hello world example a line at a time to see how these rules are applied.
+ On line 1 we see that we are declaring a class called Hello:
+
+
+
+
+public class Hello {
+
+
+
+
+ code blocks
+ curly braces
+ As rule 1 says all Java code resides inside a class.
+ Unlike Python where a program can simply be a bunch of statements, Java programs must be inside a class.
+ So, we define a class Hello, which is not a very useful class because it has no instance variables, and only one method.
+ You will also notice the curly brace {.
+ In Java, blocks of code are identified by pairs of curly braces.
+ The block starts with a { and ends with a }.
+ You will notice that I indented my code that followed the left brace, but in Java this is only done by convention, it is not enforced.
+
+
+
+ On the next line we start our method definition.
+ The name of this method is:
+
+
+
+
+public static void main(String[] args)
+
+
-
-
- -
+
+ Why So Much Detail in main?
- Early detection of errors
+ Every part of public static void main(String[] args) is required and meaningful. We'll explore what public, static, and void mean as we go, but for now, just know this exact signature is required for Java programs to run.
-
+
+
+
+ We're covering a lot of syntax details here - don't worry about memorizing everything! The goal is to understand the overall structure and know that Java is more explicit about types and method signatures than Python.
+
+
+
+ Just digging in to this one line will take us deep into the world of Java, so we are going to start digging but we are not going to dig too deeply right away.
+ Much of what could be revealed by this one line is better understood through other examples, so be patient.
+
+
+
+ public
+ protected
+ private
+ The first word, public indicates to the Java compiler that this is a method that anyone can call.
+ We will see that Java enforces several levels of security on the methods we write, including public, protected, and private methods.
+
+
+
+ static
+ The next word, static tells Java that this is a method that is part of the class, but is not a method for any one instance of the class.
+ The kind of methods we typically wrote in Python required an instance in order for the method to be called.
+ With a static method, the object to the left of the . is a class, not an instance of the class.
+ For example, the way that we would call the main method directly is: Hello.main(parameter1).
+ For now, you can think of static methods the same way you think of methods in Python modules that don't require an instance, for example the math module contains many methods: sin, cos, etc.
+ You probably evaluated these methods using the names math.cos(90) or math.sin(60).
+
+
+
+ void
+ The next word, void tells the Java compiler that the method main will not return a value.
+ This is roughly analogous to omitting the return statement in a Python method.
+ In other words, the method will run to completion and exit but will not return a value that you can use in an assignment statement.
+ As we look at other examples we will see that every Java function must tell the compiler what kind of an object it will return.
+ This is in keeping with the rule that says everything in Java must have a type.
+ In this case we use the special type called void which means no type.
+
+
+
+ main
+ Next we have the proper name for the method: main.
+ The rules for names in Java are similar to the rules in Python.
+ Names can include letters, numbers, and the _.
+ Names in Java must start with a letter.
+
+
+
+ array
+ Finally, we have the parameter list for the method.
+ In this example we have one parameter.
+ The name of the parameter is args, however, because everything in Java must have a type, we also have to tell the compiler that the value of args is an array of strings.
+ For the moment you can just think of an array as being the same thing as a list in Python.
+ The practical benefit of declaring that the method main must accept one parameter and the parameter must be an array of strings is that if you call main somewhere else in your code and and pass it an array of integers or even a single string, the compiler will flag it as an error.
+
+
+
+ That is a lot of new material to digest in only a single line of Java! Let's press on and look at the next line:
+
+
+
+
+System.out.println("Hello World!");
+
+
+
+
+ dot notation
+ This line should look a bit more familiar to you.
+ Python and Java both use the dot notation for finding names.
+ In this example we start with System.
+ System is a class.
+ Within the system class we find the object named out.
+ The out object is the standard output stream for this program.
+ Having located the out object Java will now call the method named println(String s) on that object.
+ The println method prints a string and adds a newline character at the end.
+ Anywhere in Python that you used the print function you will use the System.out.println method in Java.
+
+
+
+ Now there is one more character on this line that is significant and that is the ; at the end.
+ In Java the ; signifies the end of a statement.
+ Java statements can spread across many lines, but the compiler knows it has reached the end of a statement when it encounters a ;.
+ In Python, it is not required (or recommend) to use semicolons in this way, but whitespace is meaningful.
+ In contrast, in Java semicolons are required to end statements, but whitespace is not considered meaningful.
+
+
+
+ The last two lines of the hello world program simply close the two blocks using }.
+ The first or outer block is the class definition.
+ The second or inner block is the function definition.
+
+
+
-
-
- Faster program execution
-
-
-
-
-
-
- JVM
- byte code
- The job of the compiler is to turn your java code into language that the Java Virtual Machine (JVM) can understand.
- We call the code that the JVM understands byte code.
- The JVM interprets the byte code much like the Python interpreter interprets your Python.
- However since byte code is much closer to the native language of the computer it can run faster.
-
-
-
- When the compiler does the translation it can find many different kinds of errors.
- For example, if you make a typo, the compiler will find the typo and point it out to you before you ever run the program.
- We will look at some examples of compiler errors shortly.
- Chances are you will create some on your own very soon, too.
-
-
-
- Now that we have run our hello world program, lets go back and look at it carefully to see what we can learn about the Java language.
- This simple example illustrates a few very important rules:
-
-
-
-
- -
-
- Every Java program must define a class, and all code is inside a class
-
-
+
+ Classes and Object-Oriented Programming
- -
-
- Everything in Java must have a type
-
-
+
+ class
+ object
+ object-oriented programming
+ OOP
+ If you've been programming in Python, you've likely worked with simple data types like numbers, strings, and lists, plus written functions to process them. Java takes a different approach by organizing code around classes and objects - a style called Object-oriented programming (OOP).
+
- -
-
- Every Java program must have a function called public static void main(String[] args)
-
-
-
-
+
+ blueprint
+ template
+ methods
+ Think of a class like a blueprint or template. Just as a house blueprint shows what rooms a house should have and where they go, a class defines what information an object should store and what actions it can perform. For example, we might create a Dog class that stores a dog's name and breed, and includes methods like bark() and sit().
+
-
- Lets take the hello world example a line at a time to see how these rules are applied.
- On line 1 we see that we are declaring a class called Hello:
-
+
+
+ If you've already worked with classes in Python, this will look familiar - the main difference is that Java requires ALL code to be inside classes, while Python makes classes optional.
+
+
+
+ The best way to understand classes and objects is to see them in action. Let's define a Dog class in Python:
+
-
-
-public class Hello {
-
-
-
-
- As rule 1 says all Java code resides inside a class.
- Unlike Python where a program can simply be a bunch of statements in a file, Java programs must be inside a class.
- So, we define a class Hello, which is not a very useful class because it has no instance variables, and only one method.
- You will also notice the curly brace {.
- In Java, blocks of code are identified by pairs of curly braces.
- The block starts with a { and ends with a }.
- You will notice that I indented my code that followed the left brace, but in Java this is only done by convention, it is not enforced.
-
-
-
- On the next line we start our method definition.
- The name of this method is:
-
-
-
-
-
-public static void main(String[] args)
-
-
+
+
+class Dog:
+ def __init__(self, name, breed, fur_color):
+ self.name = name
+ self.breed = breed
+ self.fur_color = fur_color
+ self.trained = False
+ print("Dog named " + self.name + " created!")
+
+ def bark(self):
+ print(self.name + " says woof!")
+
+ def sit(self):
+ if self.trained:
+ print(self.name + " sits.")
+ else:
+ print(self.name + " has not been trained.")
+
+ def train(self):
+ self.trained = True
+
+
-
- Everything on this line is significant, and helps in the identification of this method.
- For example the following lines look similar but are in fact treated by Java as completely different methods:
-
+
+ initializer
+ constructor
+ Let's unpack what is going on in this code. The first line is where we declare the class definition and name it Dog. Next, we have a special method called __init__. This __init__ method is the initializer or constructor, and it is required for every Python class definition. Within the __init__ method, attributes are defined. As you can see, the attributes name, breed, and fur_color must be defined when creating a Dog object using this class definition, but the trained attribute is defined within the constructor and is initialized as False. We can also have the __init__ method run any code, such as the print statement informing us that a Dog object was created, which we're including here just for demonstration purposes.
+
-
-
- -
-
- public void main(String[] args)
-
-
+
+ The next three blocks of code are the class's methods. These include bark(self), sit(self), and train(self). As you can see, the class defines attributes (the variables in the __init__ method) and methods for instances of the Dog class.
+
- -
-
- public static void main(String args)
-
-
+
+ self
+ Within each method, and for each attribute, you will notice the use of self. This is required in Python. The self parameter simply indicates that an attribute or method is being used for a specific instance of an object created with a class.
+
- -
-
- public static void main()
-
-
+
+ Next, we will use this class to create a new Dog object. We will call this new Dog object my_dog:
+
+
+
+
+class Dog:
+ def __init__(self, name, breed, fur_color):
+ self.name = name
+ self.breed = breed
+ self.fur_color = fur_color
+ self.trained = False
+ print("Dog named " + self.name + " created!")
+
+ def bark(self):
+ print(self.name + " says woof!")
+
+ def sit(self):
+ if self.trained:
+ print(self.name + " sits.")
+ else:
+ print(self.name + " has not been trained.")
+
+ def train(self):
+ self.trained = True
+
+
+my_dog = Dog("Rex", "pug", "brown")
+
+
+
+
+ In the final line of code, we have created an object called my_dog. We have initialized its attributes, setting name to Rex, breed to pug, and fur_color to brown.
+
+
+
+ Now that we have created a Dog object using the class we defined, we can utilize the class's methods:
+
+
+
+
+class Dog:
+ def __init__(self, name, breed, fur_color):
+ self.name = name
+ self.breed = breed
+ self.fur_color = fur_color
+ self.trained = False
+ print("Dog named " + self.name + " created!")
+
+ def bark(self):
+ print(self.name + " says woof!")
+
+ def sit(self):
+ if self.trained:
+ print(self.name + " sits.")
+ else:
+ print(self.name + " has not been trained.")
+
+ def train(self):
+ self.trained = True
+
+
+my_dog = Dog("Rex", "pug", "brown")
+my_dog.bark()
+my_dog.sit()
+
+
+
+
+
+ When running the code above, the line Rex has not been trained. will appear in the output when calling the sit() method. Try adding a one or more lines of code so that Rex sits. appears in the output!
+
+
+
+
+ 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 in the next section.
+
+
+
+ Later in this text, we will see how to make our own Java classes that are similar to the Python Dog class that we just defined. See for more details.
+
+
- -
-
- void main(String args)
-
-
-
-
-
-
- Just digging in to this one line will take us deep into the world of Java, so we are going to start digging but we are not going to dig too deeply right away.
- Much of what could be revealed by this one line is better understood through other examples, so be patient.
-
-
-
- public
- protected
- private
- The first word, public indicates to the Java compiler that this is a method that anyone can call.
- We will see that Java enforces several levels of security on the methods we write, including public, protected, and private methods.
-
-
- static
- The next word, static tells Java that this is a method that is part of the class, but is not a method for any one instance of the class.
- The kind of methods we typically wrote in Python required an instance in order for the method to be called.
- With a static method, the object to the left of the . is a class, not an instance of the class.
- For example, the way that we would call the main method directly is: Hello.main(parameter1).
- For now, you can think of static methods the same way you think of methods in Python modules that don’t require an instance, for example the math module contains many methods: sin, cos, etc.
- You probably evaluated these methods using the names math.cos(90) or math.sin(60).
-
-
- void
- The next word, void tells the Java compiler that the method main will not return a value.
- This is roughly analogous to omitting the return statement in a Python method.
- In other words, the method will run to completion and exit but will not return a value that you can use in an assignment statement.
- As we look at other examples we will see that every Java function must tell the compiler what kind of an object it will return.
- This is in keeping with the rule that says everything in Java must have a type.
- In this case we use the special type called void which means no type.
-
-
- main
- Next we have the proper name for the method: main.
- The rules for names in Java are similar to the rules in Python.
- Names can include letters, numbers, and the _.
- Names in Java must start with a letter.
-
-
-
- Finally, we have the parameter list for the method.
- In this example we have one parameter.
- The name of the parameter is args, however, because everything in Java must have a type, we also have to tell the compiler that the value of args is an array of strings.
- For the moment you can just think of an array as being the same thing as a list in Python.
- The practical benefit of declaring that the method main must accept one parameter and the parameter must be an array of strings is that if you call main somewhere else in your code and and pass it an array of integers or even a single string, the compiler will flag it as an error.
-
-
-
- That is a lot of new material to digest in only a single line of Java! Lets press on and look at the next line:
-
-
-
-
-
-System.out.println("Hello World!");
-
-
-
-
- This line should look a bit more familiar to you.
- Python and Java both use the dot notation for finding names.
- In this example we start with System.
- System is a class.
- Within the system class we find the object named out.
- The out object is the standard output stream for this program.
- Having located the out object Java will now call the method named println(String s) on that object.
- The println method prints a string and adds a newline character at the end.
- Anywhere in Python that you used the print function you will use the System.out.println method in Java.
-
-
-
- Now there is one more character on this line that is significant and that is the ; at the end.
- In Java the ; signifies the end of a statement.
- Java statements can spread across many lines, but the compiler knows it has reached the end of a statement when it encounters a ;.
- In Python, it is not required (or recommend) to use semicolons in this way, but whitespace is meaningful.
- In contrast, in Java semicolons are required to end statements, but whitespace is not considered meaningful.
- This is a very important difference to remember! In Java, the following statements are all legal and equivalent.
- I would not encourage you to write your code like this, but you should know that it is legal.
-
-
-
-
-
-System.out.println("Hello World");
-System.out.println("Hello World")
-;
-System.out.println
- (
- "Hello World"
- ) ;
-System.
- out.
- println("Hello World")
- ;
-
-
-
-
- The last two lines of the hello world program simply close the two blocks using }.
- The first or outer block is the class definition.
- The second or inner block is the function definition.
-
-
-
- If we wanted to translate the Java back to Python we would have something like the following class definition.
-
-
-
-
-
-class Hello(object):
- @staticmethod
- def main(args):
- print("Hello World!")
-
-
-
-
- Notice that we used the decorator @staticmethod to tell the Python interpreter that main is going to be a static method.
- The impact of this is that we don’t have to, indeed we should not, use self as the first parameter of the main method! Using this definition we can call the main method in a Python session like this:
-
-
-
-
-
->>> Hello.main("")
-Hello World!
->>>
-
-
-
Summary & Reading Questions
-
- -
-
Java programs must be compiled before execution, unlike Python which is interpreted directly.
-
- -
-
Every Java program must define a class, and all code must be inside that class.
-
- -
-
Each Java program must include a public static void main(String[] args) method as the entry point.
-
- -
-
Java enforces that every variable and method must have a clearly defined type, including void for methods that return nothing.
-
- -
-
Statements in Java must end with a semicolon (;), and whitespace is not syntactically meaningful.
-
- -
-
Java uses dot notation to access class members, such as System.out.println for output.
-
- -
-
The static keyword indicates that a method belongs to the class itself, rather than to an instance of the class.
-
-
+
+
+
+ -
+
Java programs must be compiled before execution, unlike Python which is interpreted directly.
+
+ -
+
Every Java program must define a class, and all code must be inside that class.
+
+ -
+
Each Java program must include a public static void main(String[] args) method as the entry point.
+
+ -
+
Java enforces that every variable and method must have a clearly defined type, including void for methods that return nothing.
+
+ -
+
Statements in Java must end with a semicolon (;), and whitespace is not syntactically meaningful.
+
+ -
+
Java uses dot notation to access class members, such as System.out.println for output.
+
+ -
+
The static keyword indicates that a method belongs to the class itself, rather than to an instance of the class.
+
+ -
+
Object-oriented programming organizes code around classes (blueprints) and objects (instances of classes).
+
+
+
+
@@ -508,74 +542,112 @@ Hello World!
- It must contain a method called main() with no parameters.
- No. The main method must include a specific parameter: String[] args.
+
+ It must contain a method called main() with no parameters.
+
+
+ No. The main method must include a specific parameter: String[] args.
+
- It must include a public static void main(String[] args) method inside a class.
- Correct! This is the required entry point for all Java applications.
+
+ It must include a public static void main(String[] args) method inside a class.
+
+
+ Correct! This is the required entry point for all Java applications.
+
- It must be saved with a .exe extension.
- No. Java source files use the .java extension and compile to .class.
+
+ It must be saved with a .exe extension.
+
+
+ No. Java source files use the .java extension and compile to .class.
+
- It must be run using the Python interpreter.
- No. Java uses its own compiler and Java Virtual Machine (JVM).
+
+ It must be run using the Python interpreter.
+
+
+ No. Java uses its own compiler and Java Virtual Machine (JVM).
+
+
What is the purpose of the javac command?
- To compile Java source code into bytecode
- Exactly! javac compiles .java files into .class files.
+
+ To compile Java source code into bytecode
+
+
+ Exactly! javac compiles .java files into .class files.
+
- To run a Python program
- No. Python programs are run with the python or python3 command.
+
+ To run a Python program
+
+
+ No. Python programs are run with the python or python3 command.
+
- To debug Java programs
- No. javac only compiles code. Debugging is a separate process.
+
+ To debug Java programs
+
+
+ No. javac only compiles code. Debugging is a separate process.
+
- To edit Java source files
- No. You use a text editor or IDE to edit Java files.
+
+ To edit Java source files
+
+
+ No. You use a text editor or IDE to edit Java files.
+
+
What symbol does Java use to indicate the end of a statement?
- #
+
+ #
No. # is used for comments in Python, not for statement termination.
- ;
+
+ ;
Correct! Java uses semicolons to mark the end of a statement.
- .
+
+ .
No. . is used for dot notation, not to end a statement.
- }
+
+ }
No. } is used to close code blocks.
@@ -583,6 +655,47 @@ Hello World!
+
+
+
+ In object-oriented programming, what is a class?
+
+
+
+
+ A blueprint or template for creating objects
+
+
+ Exactly! A class defines the structure and behavior that objects will have.
+
+
+
+
+ A specific instance of an object
+
+
+ No. That describes an object, not a class.
+
+
+
+
+ A method that creates variables
+
+
+ No. A class is not a method.
+
+
+
+
+ A type of loop structure
+
+
+ No. Classes are not related to loop structures.
+
+
+
+
+
\ No newline at end of file
diff --git a/source/ch9_commonmistakes.ptx b/source/ch9_commonmistakes.ptx
index 1a041b9..fa384e5 100644
--- a/source/ch9_commonmistakes.ptx
+++ b/source/ch9_commonmistakes.ptx
@@ -1,7 +1,7 @@
-
+
Common Mistakes