<< Chapter < Page Chapter >> Page >

No other value can be stored in an instance of the type short . For example, you cannot store the value 35,000 in an instance of the type short in Java. If you need to store that value, you will have to use some type other than short .

Sort of like an odometer

The short type is an integer type. Integer types are somewhat analogous to the odometer in your car (the thing that records how many miles the car has been driven) .

For example, depending on the make and model of car, there is a specified set of values that can appear in the odometer. The value that appears in the odometer depends on how many miles your car has been driven. It is fairly common for an odometer to be able to store and to display the set of all positive values ranging from zero to 99,999.

If your odometer is designed to store that set of values and if you drive your car more than 99,999 miles, it is likely that the odometer will roll over and start back at zero after you pass the 99,999-mile mark. In other words, that particular type of odometer does not have the ability to store a value of 100,000 or more miles. Once you pass the 99,999 mark, the data stored in the odometer is corrupt. It no longer represents the true number of miles for which the car has been driven.

A word or two about operations

Assume that you have two instances of the type short in a Java program. Here are some of the operations that you can perform on those instances:

  • You can add them together.
  • You can subtract one from the other.
  • You can multiply one by the other.
  • You can divide one by the other.
  • You can compare one with the other to determine which is algebraically larger.

There are other operations that are allowed as well. In fact, there is a well defined set of operations that you are allowed to perform on those instances, and that set of operations is defined in the specification for the type short .

What if you want to do something different?

If you want to perform an operation that is not allowed by the type specification, then you will have to find another way to accomplish that purpose.

For example, some programming languages allow you to raise whole-number types to a power (examples: four squared, six cubed, nine to the fourth power, etc.) . However, that operation is not allowed by the Java specification for the type short . If you need to do that operation with a data value of the Java short type, you must find another way to do it.

Two major categories of type

Java data types can be subdivided into two major categories:

  • Primitive types
  • User-Defined (class) types

The remainder of this lesson will deal with primitive types.

Primitive types

Java, C++, and C# are extensible programming languages.

This means that there is a core component to the language that is always available. Beyond this, individual programmers can extend the language to provide new capabilities.

The primitive types are the types that are part of the core language.

Four categories of primitive types

I am going to subdivide the topic of primitive types into four categories:

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




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