My learning notes.
Creating Old Maid in an attempt to understand object orientation. There is such a description in it
int pos;
pos = (int) (Math.random() * number);
(int)(Math.random() * number);What is it? It became.
#### **`pos = Math.random() *I thought that number was impossible and tried it, but it was an error`**
```random() *I thought that number was impossible and tried it, but it was an error
## I looked it up
From the conclusion, the "cast operator" is probably the basis, but was it skipped? ..
It prevents errors that occur when trying to assign data types of different data!
Specifically, I wrote:
> It is necessary to always convert the data type using the cast operator, such as converting from a large type to a small type.
The large size here refers to the "number of bits".
|Data type|meaning|size|
|:-----------------|------------------ |------------------ |
| byte |integer| 8bit |
| short |integer| 16bit |
| int |integer| 32bit |
| long |integer| 64bit |
| float |Floating point| 32bit |
| double |Floating point| 64bit |
| char |1 character| 16bit |
| boolean |Boolean value| 1bit |
** ~ Addendum ~ **
** → Implicit type conversion **
| → | → | → |→ |→ | →|
|:-----------------|------------------ |------------------ |------|------|----|
| byte | short/char | int |long|float|double|
| ← | ← | ← | ←|← |← |
** ← Type conversion by cast **
When expressing with double (64bit)-> int (32bit)
(Large) → (Small), so I have to write it explicitly.
```java
int pos;
pos = (int) (Math.random() * number);
Recommended Posts