[Java] Precautions for type conversion

【Overview】

Here are some notes about type conversion in the Java Silver range.

【Explanation】

Precautions when using base numbers for variables

First, let's review the base numbers lightly.

――Binary number: In the computer, data is handled by 0 and 1. —— Decimal number: Expressed as a number from 0 to 9 that humans often use --Hexary numbers: 0 ~ 9 and A ~ F used for color codes etc.

Based on the above, I will review how to convert with the following code.

Convert the decimal number "28" to a decimal number

28 / 2 =14 remainder 0
14 / 2 =7 remainder 0
7 / 2 =3 remainder 1
3 / 2 =1 remainder 1

★☆★☆★☆★☆★☆★☆★☆★★☆★☆★☆★☆★☆★☆

28 / 2 =14 remainder 0
14 / 2 =7 remainder 0 ↑
7 / 2 =3 remainder 1 ↑
3 / 2 =1 remainder 1 ↑
Read as shown by the arrow →→→

★☆★☆★☆★☆★☆★☆★☆★★☆★☆★☆★☆★☆★☆

//Binary number
11100 

Convert a binary number to a decimal number

For the value expressed in binary, write 1, 2, 4, 8, 16, 32, 64 .. from the right bit as shown below, and add up the parts where the bit is "1".

//In the following cases, it becomes "146".
010010010 = 146

 0   1  0  0   1   0  0  1  0 
256 128 64 32  16  8  4  2  1 //Find the place where the above value is "1"
128 + 16 + 2 = 146 //Sum the corresponding values.

Sample code

I try to output a binary variable e in byte type, but I get a compile error for the above reason.

public static void main(String[] args) {
        //Explicit cast
        int a = 100;
        //Convert to short type
        short b = (short) a;

        //byte type-128 ~Since it is a data type that handles values ​​up to 127, an error will occur if the range is exceeded.
        //Compilable for int type literals within byte
        byte c = 127;
        //Compile error due to int type literal that does not fit in the byte range
        byte d = 128;
        //Compile error because it becomes 256 in decimal number
        byte e = 0b10000000;

        System.out.println(e);
    }

Assign a long type literal to an Int type variable [Compile error]

As shown below, long type literals cannot be assigned to Int type variables.

public static void main(String[] args) {
        int f = 2 * 3L;

        System.out.println(f);
    }
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Type mismatch:Cannot convert from long to int

Assign a double type literal to a float type variable [Compile error]

As shown below, I am trying to assign a float type that can use less than that to the double type variable "g", so this also causes a compile error.

public static void main(String[] args) {
        float g = 10.0;

        System.out.println(g);
    }
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Type mismatch:Cannot convert from double to float

[Summary]

Be aware of these behaviors as a test measure.

Also, since the range that can be handled by byte and short is narrow, remember the range accurately.

--byte is -128 ~ 127 --short is 32768 to 32767

References / Articles

-[Thorough capture Java SE 11 Silver problem collection [1Z0-815] correspondence](https://www.amazon.co.jp/%E5%BE%B9%E5%BA%95%E6%94%BB%E7 % 95% A5Java-SE-11-Silver% E5% 95% 8F% E9% A1% 8C% E9% 9B% 86-1Z0-815/dp/4295007625/ref = pd_lpo_14_img_0/356-5238196-1895250? _Encoding = UTF8 & pd_rd_i = 4295007625 & pd_rd_r = 1c21865b-a9fe-43e8-ac01-46170e44abca & pd_rd_w = 9El0Q & pd_rd_wg = hh5Hc & pf_rd_p = 4b55d259-ebf0-4306-905a-7762d1b93740 & pf_rd_r = 72AVCH

Recommended Posts

[Java] Precautions for type conversion
Java type conversion
[Java] Date type conversion
[Java] List type / Array type conversion
[Java] Type conversion speed comparison
Java Primer Series (Type Conversion)
java (use class type for field)
[Easy-to-understand explanation! ] Reference type type conversion in Java
[JAVA] Stream type
[Java ~ Variable definition, type conversion ~] Study memo
[Java] Enumeration type
Java Optional type
Java study # 3 (type conversion and instruction execution)
2017 IDE for Java
Java double type
[Basic knowledge of Java] About type conversion
Java-automatic type conversion
Java 8 LocalDateTime type conversion stuff (String, java.util.Date)
[Java] Calculation mechanism, operators and type conversion
Java for statement
Type conversion from java BigDecimal type to String type
[Introduction to Java] About type conversion (cast, promotion)
Java type conversion (String, int, Date, Calendar, etc.)
[Java] for statement, while statement
Flexible data type conversion mechanism of O / R mapping library Lightsleep for Java 8
[Java] Full-width ⇔ half-width conversion
[Java] Package for management
[Java] for statement / extended for statement
[Java] Data type ①-Basic type
Uri → String, String → Uri type conversion
Countermeasures for Java OutOfMemoryError
[Java, Kotlin] Type Variance
Java class type field
Type determination in Java
Java study # 1 (typical type)
NLP for Java (NLP4J) (2)
(Memo) Java for statement
NLP for Java (NLP4J) (1)
[Java] About enum type
Endian conversion with JAVA
Java update for Scala users
[Java] Basic statement for beginners
Try functional type in Java! ①
Books used for learning Java
[Java Bronze] Learning memo (interface, static method, type conversion, etc.)
2018 Java Proficiency Test for Newcomers-Basics-
Java study # 7 (branch syntax type)
Java thread safe for you
Precautions for updating Realm object
[Java] Summary of for statements
Java for beginners, data hiding
[Java] Tips for writing source
Java installation location for mac
Java application for beginners: stream
Java while and for statements
C # cheat sheet for Java engineers
New grammar for Java 12 Switch statements
[For beginners] Summary of java constructor
AWS SDK for Java 1.11.x and 2.x
Rock-paper-scissors game for beginners in Java
Java for beginners, expressions and operators 1