<< 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."


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.

Questions & Answers

what is Nano technology ?
Bob Reply
write examples of Nano molecule?
The nanotechnology is as new science, to scale nanometric
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
what school?
biomolecules are e building blocks of every organics and inorganic materials.
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
sciencedirect big data base
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
characteristics of micro business
for teaching engĺish at school how nano technology help us
Do somebody tell me a best nano engineering book for beginners?
s. Reply
there is no specific books for beginners but there is book called principle of nanotechnology
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
what is the actual application of fullerenes nowadays?
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
what is the Synthesis, properties,and applications of carbon nano chemistry
Abhijith Reply
Mostly, they use nano carbon for electronics and for materials to be strengthened.
is Bucky paper clear?
carbon nanotubes has various application in fuel cells membrane, current research on cancer drug,and in electronics MEMS and NEMS etc
so some one know about replacing silicon atom with phosphorous in semiconductors device?
s. Reply
Yeah, it is a pain to say the least. You basically have to heat the substarte up to around 1000 degrees celcius then pass phosphene gas over top of it, which is explosive and toxic by the way, under very low pressure.
Do you know which machine is used to that process?
how to fabricate graphene ink ?
for screen printed electrodes ?
What is lattice structure?
s. Reply
of graphene you mean?
or in general
in general
Graphene has a hexagonal structure
On having this app for quite a bit time, Haven't realised there's a chat room in it.
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!

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?