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

Questions & Answers

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.
Bharti
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
Daniel
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
Maciej
characteristics of micro business
Abigail
for teaching engĺish at school how nano technology help us
Anassong
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
NANO
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
s.
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.
Tarell
what is the actual application of fullerenes nowadays?
Damian
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.
Tarell
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.
Virgil
is Bucky paper clear?
CYNTHIA
carbon nanotubes has various application in fuel cells membrane, current research on cancer drug,and in electronics MEMS and NEMS etc
NANO
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.
Harper
Do you know which machine is used to that process?
s.
how to fabricate graphene ink ?
SUYASH Reply
for screen printed electrodes ?
SUYASH
What is lattice structure?
s. Reply
of graphene you mean?
Ebrahim
or in general
Ebrahim
in general
s.
Graphene has a hexagonal structure
tahir
On having this app for quite a bit time, Haven't realised there's a chat room in it.
Cied
what is biological synthesis of nanoparticles
Sanket Reply
what's the easiest and fastest way to the synthesize AgNP?
Damian Reply
China
Cied
types of nano material
abeetha Reply
I start with an easy one. carbon nanotubes woven into a long filament like a string
Porter
many many of nanotubes
Porter
what is the k.e before it land
Yasmin
what is the function of carbon nanotubes?
Cesar
I'm interested in nanotube
Uday
what is nanomaterials​ and their applications of sensors.
Ramkumar Reply
what is nano technology
Sravani Reply
what is system testing?
AMJAD
Got questions? Join the online conversation and get instant answers!
QuizOver.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?

Ask