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

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