Java: Modifying Variable Content Using Unary Operators

Binary operators in Java are those that require two operands (variables or constants) to work. A unary operator is one in which you need just the operator and its associated operand. The following sections discuss unary operators, such as those used to increment or decrement the value in a variable.

Unary operators perform an operation on a single operand, such as incrementing or decrementing the value within the operand. You must use a variable, and not a constant, when working with a unary operator in most cases.

Incrementing and decrementing numbers

The most common unary operations are incrementing and decrementing numbers. In most cases, an application uses these operations to keep count of something. However, you can use them anytime you need to increase or decrease the value of a variable by one. In this example, you see how the increment (++) and decrement (--) operators work.

Creating objects

Throughout Java programming, you create objects of various types. Java applications typically require one or more objects in order to work. In fact, the Java application itself is an object. Anytime you see the word class in a listing, you’re talking about objects. Every Java application is a class, which means that every application is an object.

It’s important to realize just how objects work. When you create a class, what you’re really creating is a blueprint. The blueprint describes how to create an object, but it isn’t the object. To create an instance of a class (the object), you use the new operator.

The new operator tells Java to create an instance of the requested object using the class you specify as a blueprint. An application can create as many instances of a class as required, provided there are enough system resources (such as memory) to do so.

Casting one type to another

The act of casting transforms one type of variable into another type. It’s important to realize that casting isn’t some sort of magic. As far as the computer is concerned, all of your data is 1s and 0s. The translation takes place, in part, in how your application views the data.

When making a cast between disparate types, such as casting an integer type to a floating point type, the actual form of the data changes as well, but not in a way you need to worry about. (It all happens beneath the surface automatically.)

Casting can produce data loss. For example, if you cast a floating point type to an integer type, you’ll lose the decimal portion of the floating point number. However, the integer portion will remain intact.

When you cast a number held in a larger container, such as a long, to a smaller container, such as an int, you can lose the upper bits, and the actual value of the number can change.

In all cases, the cast occurs when you place the new type you want to use within parenthesis next to the original variable. For example, (float)MyInt would cast an int type to a float type.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com