[Java] I checked it because it was erratic when converting the type. <Cast operator>

Introduction

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

[Java] I checked it because it was erratic when converting the type. <Cast operator>
I checked because the response was strange when debugging with Tomcat 8
When I wanted to create a method for Premium Friday, it was already in the Java 8 standard API
Why Java was the target language I hate
I couldn't run it after upgrading the Java version
Recorded because it was accepted by Oracle Java Silver
I was confused because there was a split in the Array
The story I was addicted to when setting up STS
When I was worried about static methods in java interface, I arrived in the order of name interpretation