<< Chapter < Page Chapter >> Page >

Two String operands

If both operands are references to objects of type String , the plus operator creates and returns a new String object that contains the concatenated values of the two operands.

One String operand and one of another type

If one operand is a reference to an object of type String and the other operand is of some type other than String , the plus operator causes a new String object to come into existence.

This new String object is a String representation of the non-String operand (such as a value of type int ) ,

Then it concatenates the two String objects, producing another new String object, which is the concatenation of the two.

How is the new String operand representing the non-string operand created?

The manner in which it creates the new String object that represents the non-String operand varies with the actual type of the operand.

A primitive operand

The simplest case is when the non-String operand is one of the primitive types. In these cases, the capability already exists in the core programminglanguage to produce a String object that represents the value of the primitive type.

A boolean operand

For example, if the operand is of type boolean , the new String object that represents the operand will either contain the word true or the word false.

A numeric operand

If the operand is one of the numeric types, the new String object will be composed of some of the following:

  • numeric characters
  • a decimal point character
  • minus characters
  • plus character
  • other characters such as E or e

These characters will be arranged in such a way as to represent the numeric value of the operand to a human observer.

In this program ...

In this program, a numeric double value, a numeric int value, and a boolean value were concatenated with a pair of slash characters to produce a String object containing the following:

3.5/9/true

When a reference to this String object was passed as a parameter to the println method, the code in that method extracted the character string from the String object, and displayed that character string on the screen.

The toString method

If one of the operands to the plus operator is a reference to an object, the toString method is called on the reference to produce a string that represents the object. The toString method may be overridden by the author of the class from which the object was instantiated toproduce a String that faithfully represents the object.

Back to Question 7

Answer 6

C. 4 -3

Explanation 6

A rounding algorithm

The method named doIt in this program illustrates an algorithm that can be used with a numeric cast operator (int) to cause double values to be rounded to the nearest integer.

Different than truncation toward zero

Note that this is different from simply truncating to the next integer closer to zero (as was illustrated in Question 5 ) .

Back to Question 6

Answer 5

D. 3 -3

Explanation 5

Truncates toward zero

When a double value is cast to an int , the fractional part of the double value is discarded.

This produces a result that is the next integer value closer to zero.

This is true regardless of whether the double is positive or negative. This is sometimes referred to as its "truncation toward zero" behavior.

Questions & Answers

the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
what is cell divisoin?
Aron Reply
Diversity of living thing
ISCONT
what is cell division
Aron Reply
Cell division is the process by which a single cell divides into two or more daughter cells. It is a fundamental process in all living organisms and is essential for growth, development, and reproduction. Cell division can occur through either mitosis or meiosis.
AI-Robot
What is life?
Allison Reply
life is defined as any system capable of performing functions such as eating, metabolizing,excreting,breathing,moving,Growing,reproducing,and responding to external stimuli.
Mohamed
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask