A. Compilation: To create and run Java programs in your computer, you have to download and install "Java" - what have you downloaded and installed? why a PC and a MacBook have to download different packages?
Answers: 1. The first part of the package is JVM. JVM communicates with OS and runs Java "byte code". Because a PC and a MacBook run different OS, their JVMs are different. That's why you have to download different packages for PC and Mac. 2. The second part of the package is a Compiler. The compiler turns your Java programs(.java files) into "byte code"(.class files) so they can run in JVM. The compiler works behind the scene as you edit your programs(source code), the process of turning your source code into byte code is called compilation. 3. The third part of the package is Java Standard Library, which has many built-in useful classes and methods such as String, Scanner or Math.sqrt() etc.
B. Compile-time Errors and Run-time Exceptions
- The compiler may detect errors, mistakes or ambiguous definitions while processing the source code. It will report these errors to programers so they can correct. These are compile-time errors. Examples are type mismatching, undefined variables, invalid method call etc. If these errors are not correct, compiler will not be able to turn the source code into byte code, so the program won't run at all.
- If a program has no compile-time errors, compiler will run the code. During the execution of code, there might be some exceptions such as dividing by 0, wrong input format, array or string index out of bound etc. These errors happen while the program is running, are called run-time exceptions.
- The checked and unchecked exceptions both happen at run time, while the program is in execution. If the program specifies clearly what to do when an exception happens, (i.e. how to handle this exception) it is a checked exception. For unchecked exceptions, nothing is specified so it is up to JVM to give some error messages if the exception rises.
Checked and Unchecked Exceptions:
C. Packages: Now that we understand the underline platform which supports a Java program, find out:
- How are Java programs organized into packages? What is the default package?
- When you program, how to use standard classes provided in Java library?
- What's the structure of a Java class?
Answers: 1. Java standard library classes are organized in packages and sub packages such as java. util. *, or java.lang.*. Here the top level package is "java" while "util" and "lang" are two sub packages. There are many classes in each sub package. 2. To use the standard library classes, you need to import the sub packages or classes in your class. By default, java.lang.* is always imported to any custom class. 3. A java class is put in "default" package if you don't create one. This default package is under your "project" folder in Eclipse or Codiva.
D. Publicity Modifiers: Any Java class or class members can have a publicity modifier: public, protected, private, or default (not specified). What does these modifier mean?
E. Java input/output
- Scanner class is not required by AP CSA.
- System.out.printf is not required by AP CSA, only System.out.print and System.out.println.
- What will "System.out.println(4+6+"lb");" print out? How about "System.out.println("$"+4+6);"?
- What are escape sequences?
Answer: System.out.println("Hello World!"); will never print out double quote ". If you need to print " as part of the output, you can use escape sequences. An escape sequence is a backslash \ followed by another character, Java use these to print out some special characters. For instance, \" prints ", \n prints a new line and \\ prints backslash \ itself.
Multiple Choice Questions: #1-8 of Chapter 2(Barron's). Please check your answers AFTER you solve the problems. Post here if you have more questions about the correct answers.