> For the complete documentation index, see [llms.txt](https://www.computersciencea.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.computersciencea.com/unit-1-using-objects-and-methods/1.5-casting-and-range-of-variables.md).

# 1.5 Casting and Range of Variables

**Suggested Skills:** 2.A, 4.A

**LO 1.5.A** — Develop code to cast primitive values to different primitive types in arithmetic expressions and determine the value that is produced as a result.

* **1.5.A.1** — The casting operators `(int)` and `(double)` can be used to convert from a `double` value to an `int` value (or vice versa).
* **1.5.A.2** — Casting a `double` value to an `int` value causes the digits to the right of the decimal point to be truncated.
* **1.5.A.3** — Some code causes `int` values to be automatically cast (widened) to `double` values.
* **1.5.A.4** — Values of type `double` can be rounded to the nearest integer by `(int)(x + 0.5)` for non-negative numbers or `(int)(x - 0.5)` for negative numbers.

**LO 1.5.B** — Describe conditions when an integer expression evaluates to a value out of range.

* **1.5.B.1** — The constant `Integer.MAX_VALUE` holds the value of the largest possible `int` value. The constant `Integer.MIN_VALUE` holds the value of the smallest possible `int` value.
* **1.5.B.2** — Integer values in Java are represented by values of type `int`, which are stored using a finite amount (4 bytes) of memory. Therefore, an `int` value must be in the range from `Integer.MIN_VALUE` to `Integer.MAX_VALUE` inclusive.
* **1.5.B.3** — If an expression would evaluate to an `int` value outside of the allowed range, an **integer overflow** occurs. The result is an `int` value in the allowed range but not necessarily the value expected.

**LO 1.5.C** — Describe conditions that limit accuracy of expressions.

* **1.5.C.1** — Computers allot a specified amount of memory to store data based on the data type. If an expression would evaluate to a `double` that is more precise than can be stored in the allotted amount of memory, a **round-off error** occurs. The result will be rounded to the representable value. To avoid rounding errors that naturally occur, use `int` values.
  * ❌ **EXCLUSION** — Other special decimal data types that can be used to avoid rounding errors are outside scope.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://www.computersciencea.com/unit-1-using-objects-and-methods/1.5-casting-and-range-of-variables.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
