[Basic knowledge of Java] About type conversion

Introduction

This article is --Start learning Java ――I want to review the basics

For those who say.

The types and variables are described in the following articles. [Basic knowledge of Java] About types and variables

Basic type (primitive type) conversion

Here's how to convert a variable of one basic type to a variable of another basic type.

Basic type of conversion source Basic type of change destination that can be type converted
char int, long, float, double
byte short, int, long, float, double
short int, long, float, double
int long, float, double
long float, double
float double
double None

Small to large mold

The integer data type is byte < short < int < long It is a feeling of size. When converting from a small type to a large type, there are ** when the type is automatically converted ** and ** a cast where the programmer intentionally converts.

When the type is converted automatically

    byte a = 127;
    short b = a;
    System.out.println(b);//Output result = 127

    short c = 32767;
    long d = c;
    System.out.println(d);//Output result = 32767
A cast where the programmer consciously transforms

    byte a = 127;
    byte b = 127;
    System.out.println((long)a + b);//Output result = 254

    int c = 2147483647;
    int d = 2147483647;
    System.out.println((long)c + d);//Output result = 4294967294

Large to small mold

Be careful when converting from large to small types.

I get an error if I do the following.

    long a = Long.MAX_VALUE;//Substituting the maximum value of long
    System.out.println(a);//Output result = 9223372036854775807
    short b = a;//Type mismatch: cannot convert from long to short

There is a way to fix the error, but the contents of the variable will change. If the size exceeds the size that the data type can have, data will be truncated (data overflow), and the expected execution result will not be obtained.

    long a = Long.MAX_VALUE;//Substituting the maximum value of long
    System.out.println(a);//Output result = 9223372036854775807
    short b = (short) a;//Type conversion by cast
    System.out.println(b);//Output result =-1

Reference type conversion

This time, I will only explain the type conversion of class types. When the reference type of the conversion source is a class type, the reference type of the conversion destination and the rules of type conversion are as shown in the figure below.

Reference type of conversion destination Type conversion rules
Class type 変換先のClass typeが変換元のClass typeのスーパークラスであること。
Interface type 変換先のInterface typeのインタフェースを変換元のクラス型のクラスが実装していること。
Array type Type conversion is not possible.

** Reference type implicit type conversion ** Parent class type variable = new child class ();

//Parent class
class Oya {
  void greet() {
    String a = "Good morning";
      System.out.println(a);
  }
}

//Child class that inherits the parent class
class Kodomo extends Oya {
  void name() {
    String b = "Iniesta";
      System.out.println(b);
  }
  public static void main(String[] args) {
      Oya oy = new Kodomo();
      oy.greet();//Output result = good morning
      Oya oy2 = new Oya();
      oy2.greet();//Output result = good morning

      Kodomo ko = new Kodomo();
      ko.greet();//Output result = good morning
      ko.name();//Output result = Iniesta
      Kodomo ko2 = new Oya();//Error: Type mismatch: cannot convert from Oya to Kodomo

  }
}

As you can see in the error Child class type variable = new parent class (); You can not.

Conversion to string

It is possible to convert from all types to character types.

    Integer a1 = 123;
    String s1 = "Ah" + a1;
    System.out.println(s1);//Output result = A 123
    System.out.println(s1 instanceof String);//Output result = true


    int a2 = -123;
    String s2 = String.valueOf(a2);
    System.out.println(s2);//Output result =-123
    System.out.println(s2 instanceof String);//Output result = true

Finally

I still don't have a deep understanding of reference type conversion, so I will add it as soon as I understand it.

Then thank you ☕️

Recommended Posts

[Basic knowledge of Java] About type conversion
[Basic knowledge of Java] Scope of variables
Java type conversion
Java basic knowledge 1
[Java] Data type ①-Basic type
[Java] About enum type
[Java] Date type conversion
[Introduction to Java] About type conversion (cast, promotion)
Java review ③ (Basic usage of arrays / reference type)
[Java] List type / Array type conversion
Basic knowledge of SQL statements
[Java] Precautions for type conversion
[Java] Type conversion speed comparison
Introduction to Java for beginners Basic knowledge of Java language ①
About Java basic data types and reference type memory
Java Primer Series (Type Conversion)
About full-width ⇔ half-width conversion of character strings in Java
[Java] Difference between assignment of basic type variable and assignment of reference type variable
About fastqc of Biocontainers and Java
About Lambda, Stream, LocalDate of Java8
Basic knowledge of Ruby on Rails
Basic usage of java Optional Part 1
Basic processing flow of java Stream
[Java] Correct comparison of String type
Basic structure of Java source code
Basic knowledge
[Java Siler] About type inference by var
[Java] Implicit type cast (AOJ10-sum of numbers)
Java date data type conversion (Date, Calendar, String)
[Easy-to-understand explanation! ] Reference type type conversion in Java
Java study # 3 (type conversion and instruction execution)
[Java] Personal summary of conditional statements (basic)
Example of using ArAutoValueConverter (field type conversion)
[Java] Comparison of String type character strings
Java Summary of frequently searched type conversions
Java 8 LocalDateTime type conversion stuff (String, java.util.Date)
[Java] Calculation mechanism, operators and type conversion
Type conversion from java BigDecimal type to String type
About Java interface
[Java] About Java 12 features
Way of thinking when studying basic program knowledge
Java knowledge summary
Flexible data type conversion mechanism of O / R mapping library Lightsleep for Java 8
Unexpectedly unknown basic tips about StringBuilder [Java / C #]
[For beginners] Introduction to Java Basic knowledge of Java language ③ Array, selection structure, iteration structure
About the description order of Java system properties
About the idea of anonymous classes in Java
[Ruby] Basic knowledge of class instance variables, etc.
[JAVA] Stream type
Java basic grammar
About date / time type of Doma2 entity class
[Java] Enumeration type
Java basic grammar
Java Optional type
About var used in Java (Local Variable Type)
Something about java
Where about java
About Java features
[Java] Basic structure
About Java threads
[Java] About interface