<< Chapter < Page Chapter >> Page >

As indicated in Figure 1 , the + operator is also used for string concatenation, but that is another topic for another lesson.

Are the operators as innocuous as they seem?

At first glance, you might ask why there is a need to elaborate on or to clarify thisspecification. After all, aren't the arithmetic operators completely innocuous? The fact is that the arithmetic operators may not be as innocuous as they seem.There are several pitfalls involving these operators that can trap the unsuspecting Java programming student.

The subtraction operator is rather innocuous

There is very little to say about the - operator. It performs numeric subtraction as indicated in Figure 1. It is hard to get into trouble doingnumeric subtraction.

The other operators are not so innocuous

However, the addition, multiplication, division and remainder operators probably do deserve a little more scrutiny, particularly insofar as to how they behave for different types. (Note that the remainder operator is also known as the modulus operator.)

Integer division

Let's begin with a question. Which of the following outputs, if any, is produced by the program shown in Listing 1 ?

  • A. Compiler Error
  • B. Runtime Error
  • C. 33.666666
  • D. 34
  • E. 33
Listing 1 . Integer division.
public class Division01{ public static void main(String args[]){ new Worker().doWork();}//end main() }//end class definitionclass Worker{ public void doWork(){int myVar01 = 101; int myVar02 = 3;System.out.println(myVar01/myVar02); }//end doWork()}//end class definition

The first program

Since this the first program code that I have presented in this series of lessons, I will explain some aspects of the code before providing and explainingthe answer to the question.

A violation of specification number 18

To begin with, this program, (and many of the other programs that I will present in this series of lessons) , violates specification number 18 in the course description . That specification reads partiallyas follows:

"18. Visibility: In the AP Java subset, all classes are public."

Overkill

While that probably is a worthwhile requirement in large-scale project development, it is overkill for the simple programs that I will present in thisseries of lessons. (Note that the class named Worker in Listing 1 is not declared public .)

If I were to declare the Worker class public , it would be necessary for me to put it in a separate disk file named Worker.java . That would complicate my disk management efforts with regard to this series of lessons, and would also complicate your efforts in those cases whereyou want to copy, compile, experiment with, and execute the code.

All class definitions in the same disk file

Therefore, in most cases, I will omit the public declaration for all classes other than the driver class that contains the main method. That will allow me to putall classes belonging to a single program in a single disk file, and to provide a listing of the program in a form that you can easily copy into your IDE.

Having copied the program into your Java IDE, you can save the source code in a single disk file. Having done that, you can compile andexecute the program without having to create a separate disk file for each class definition in the program.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Ap computer science a, clarification of the java subset. OpenStax CNX. Oct 03, 2015 Download for free at https://legacy.cnx.org/content/col11279/1.5
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Ap computer science a, clarification of the java subset' conversation and receive update notifications?

Ask