[go: up one dir, main page]

0% found this document useful (0 votes)
8 views1 page

IntegerCacheMechanism

Uploaded by

tellapuri.naresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views1 page

IntegerCacheMechanism

Uploaded by

tellapuri.naresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

In the code snippet you provided:

```java
Double b = 1273.0;
Double s = 1273.0;
System.out.println(b == s);
```

### Explanation:
1. **`==` Operator**: In Java, the `==` operator checks for **reference equality**
when used with objects. This means it checks whether `b` and `s` refer to the same
object in memory, not whether their values are the same.

2. **Double Caching**: Unlike `Integer` with a caching range of `-128` to `127`,


`Double` does **not** have a caching mechanism for values. Each `Double` object
created with the `new` keyword or using literal values results in distinct objects.

### Why the Output is `false`:


- When you assign `1273.0` to `b` and `s`, the `Double` objects are created
separately, and they are not cached.
- Therefore, `b` and `s` are two different objects in the heap, and the `==`
operator returns `false` because they do not refer to the same memory location.

### Uncommenting the `equals` Method:


If you use `b.equals(s)`, it will return `true` because the `equals()` method for
`Double` checks for **value equality**, not reference equality.

```java
System.out.println(b.equals(s)); // This will print true
```

### Summary:
- `b == s` checks if `b` and `s` are the same object, resulting in `false`.
- `b.equals(s)` checks if the values are equal, resulting in `true`.
================================

You might also like