CS101 Study Guide

Unit 3: Java in Practice

3a. Write Java classes using file naming conventions

  • What is the difference between *.java and *.class files?
  • Within which class method does a Java program begin executing?
  • What is a reserved word?
  • Explain matching syntaxual pairs.

In Unit 2, we went through the basics of creating, compiling, and running Java programs. In doing so, we also learned the difference between source code and bytecode.

As you write your next programs, learn how to use indenting, it sounds like such a simple thing that has no impact at all on what the program does or how it will be compiled. It also has nothing to do with what bytecodes get generated. Yet, it can be critical to writing successful computer programs, especially as those programs become more complex. The same can be said of commenting. There is excellent material on both those topics in the readings.

When writing programs, it is important to create meaningful names (identifiers) for your classes, methods, and variables. Recognize that some identifiers are reserved to the language itself. Those cannot be used to name anything else. Programmer identifiers must be unique but are meaningless to the compiler. Still, they are essential to programming, from the human perspective. (Calling something xxx is no different from calling that same thing yyy, from the compiler's perspective.) Indenting, commenting, and meaningful identifiers all have to do with human-readable code. Such code is essential to debugging, finding and fixing syntax errors or logic errors in a program. Readable code is also important to maintenance, modification, and evolution. Do not be caught in the trap of thinking you are smart if nobody can understand the code you write. That is a mark of a non-professional and is easily spotted by technical team leaders. If you take that route, do not be surprised when you find yourself on the outer edge of the plate as the economy goes through its cycles. As a cycle brings on a decline, the plate shrinks. Non-Professionals fall off the plate (get laid off) at that point. Your job becomes insecure and you become less employable.

We spoke about Integrated Developer Environments (IDEs) and advanced text editors earlier. Those can help a great deal with a language's syntax. For example, they can identify matching syntaxual pairs such as ( ) , { }, and [ ]. These details are easy to miss as one translates human expressions of processes into something the computer can understand (a particular computer language). Let the computer help you with the details so that you can concentrate on what you are trying to accomplish.

Review Small Java Problems: Sections 1, 2, 6, 21 are for review, pay special attention to the rest.

3b. Use pre-written Java classes from various packages in the Java API

  • Name important Java language packages (libraries).
  • In what way does the order of import statements matter?
  • What can be made available in a package?
  • Explain the value of interfaces.

Packages are composed of several classes, interfaces, and other formal types. These make up callable libraries, some within the Java language itself, while others are written by third parties for specific purposes. 

Java packages organize classes into unique namespaces. Classes in the same package have access to private and protected members of all classes in that package. Packages can contain the following types: classes, interfaces, enumerations, and annotations. Programmers can group classes and interfaces according to various criteria depending on the system being created.

Java interfaces expand beyond what other languages provide. They are very useful for describing abstract data types. Interfaces offer method signatures, but not method contents. Classes implement interfaces by declaring the interface in an implements clause. It is in the class description that method bodies are provided for interfaces.

When we import a package, we gain access to the classes and interfaces that have been made available within the associated library

Review Importing Libraries in Java: This is a very short reading, be sure you understand its contents (the paragraphs about this learning outcome contain two links that offer additional details for those who wish to delve further).

3c. Explain and use primitive data-types in Java

  • What is a "data type"?
  • What is the difference between primitive data types and classes?
  • In general, how are data types represented in computer memory?
  • Name Java's eight primitive data types

Often you will hear that everything in Java is a class. But, that is not quite true. There are eight primitive, non-class, data types. These are: byte, short, int, boolean, char, float, double, long. Notice the case sensitivity of those terms. Be mindful, for instance, that int is not at all the same as Int. Int is a class that does indeed contain an integer ( int) but it also contains methods for manipulating that attribute. The same is true of the other primitives. As you may have noticed, objects take up far more memory than do primitives. Thus, at times when class methods are not needed, it is sometimes useful to use a primitive instead of its associated class.

Context is essential in computer programming, not only in human logic but also in the way computers represent data. At the human level, if we say A=B, what does that mean? What does A stand for relative to the application's goals, design, and implementation? Meaningful naming of variables helps with that situation. Inside the computer, A is stored as a series of binary digits. The data type assigned in the source code allows the compiler to mark the binary digits with a "flag" that explains how to interpret them.


3d. Declare and use variables of different data types while writing Java programs

  • What is the syntaxual difference between declaring primitive and non-primitive data types?
  • Why would one want to declare a non-primitive number vs. a primitive number?
  • What other kinds of data are there in Java besides primitive data types?
  • Could something called "Worker" be a data type in Java?

There are only two types of data in Java, primitives and objects. There are only eight primitives that can be defined. There are potentially an infinite number of objects that can be defined. The data type of an object is its class name. Java itself defines a fair number of these that have been found to be broadly useful. Third-party libraries define others for special purposes. Programmers can define their own.

Reusability is an important cause for writing good non-primitives, class definitions. If done well, these can be used in more than one program, improving as they go, while maintaining their basic functionality and interface. Studies have shown that it takes 25% more time to write reusable classes but that it is well worth the effort over the long term.

Something to keep in mind is that a primitive data type has a fixed number of bits associated with it. Therefore, a minimum amount of memory is occupied. There are no associated methods or other variables within the definition of primitive data types. Such is not the case with class objects. Classes have attributes (variables) and behaviors (methods, functions, subroutines). Depending on the type of variables contained, an object can vary in the amount of memory occupied. For example, an object may include a string of characters. That string can grow and shrink and grow again as the program proceeds. This may not be acceptable in low-memory embedded systems.

Review Primitive Data: Objects.

3e. Perform various mathematical operations using +, -. *, /, and % operators

  • Besides equals, =, what other assignment operators are there?
  • What are the arithmetic operators?
  • Do arithmetic operators only work on primitive numbers?
  • How can the sequence of arithmetic operations be changed from that imposed by operator precedence?

Assignment operators perform a fundamental role in computer programming. They copy memory contents from one location to another. Yet, arithmetic and assignment operators are often combined. This allows for a bit of shorthand in the code and a reduction in the number of bytecodes generated. It is interesting to note that the order of combination is important. For instance, x = ++y; does not yield the same value for x as does x = y++;. The latter copies the value of y to x and then increments y by one. The former increments the value of y by one and copies the result to x. The same is true for the -- operator.

Operator precedence is an important consideration. Without any other indication, an equation will be calculated based on the precedence of its operators. For instance, a unary minus, -x for instance, will be performed before something like a - x. So, you may see something like a - -x. In that case, the unary minus will be applied to x prior to that result being subtracted from a. Operator precedence can be overridden using parentheses. The innermost parenthesized equation components will be performed first. So, for example,  x - a * b will not yield the same result as (x - a) * b.

As you can see, computer programming is very detailed. Pay attention to those details and grow with them. Eventually, they will all fit together and make sense without a lot of overt memorization.


3f. Use String class and its methods while writing Java programs

  • What is the basic definition of "string"?
  • What does "null" mean in regards to String objects?
  • What does it mean to cascade class operations?
  • To what does "immutable object" refer?

Although strings are very complex entities, we study them only in their simplest form. Think of a string as a vector (sequence) of 16-bit characters that can be manipulated in various ways. Each character is individually addressable. Java uses "String" as the class name for this kind of variable.

There is one way in which a String can be operated on using a numeric operation. thisString = thatString + someOtherString; places the concatenation of thisString and someOtherString into the memory location of thisString, (someOtherString's text is placed at the end of thisString's text.) Notice that it is possible that concatenation may yield a result that requires more memory than referenced by the target variable. In such a case, new memory has to be allocated. If shorter than the target variable, there is "leftover" memory that becomes unoccupied. In either case, something has to be done to make the orphaned memory available to the program. Languages such as C/C++ require the programmer to write code to accomplish this. Java, on the other hand, handles such situations automatically. The process is called "garbage collection".

String objects are immutable. That means that a String's behaviors do not modify that String's attributes. Rather, they return a new string. Keep that in mind since it is something many programmers forget. If you do forget this, your programs will compile and run. But, they may yield unexpected results.

Become familiar with the various methods associated with the String class. They make common string operations easy to implement and save a lot of programming effort.


Unit 3 Vocabulary

This vocabulary list includes the terms listed above that you will need to know to successfully complete the final exam.

  • bits
  • case sensitivity
  • characters
  • classes
  • commenting
  • concatenation
  • data type
  • debugging
  • garbage collection
  • identifiers
  • immutable
  • implements clause
  • import
  • indenting
  • integer
  • interfaces
  • library
  • logic errors
  • memory
  • methods
  • objects
  • operators
  • operator precedence 
  • packages
  • primitives
  • primitive data type
  • source code
  • string
  • syntax
  • syntax errors
  • unary minus
  • variables