D
D
deleted-ITema3252015-03-27 20:44:24
Java
deleted-ITema325, 2015-03-27 20:44:24

Automatic conversion and explicit type conversion in JAVA?

Please tell us in plain language about type conversion.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bromzh, 2015-03-27
@deleted-ITema325

Explicit is when you convert a variable of one type to another in brackets (or through method calls). At the same time, it is not a fact that this transformation will take place and will take place correctly.
An automaton is when a type that is "larger" is involved in operations with "smaller" types and a conversion is needed. Java itself will be able to convert, since the data is not lost.
Example:

int a = 70000;
short b = 10;
int c = a + b; // Тут b автоматом преобразуется к типу int, так как компилятор знает, 
// что данные из него не потеряются при преобразовании.
short d = (short) (a + b);

If you remove the conversion of the entire sum to short, it will not compile, the types do not match. The variable "a" (or rather, the entire sum) must be converted to short, but the result of the sum will be int. short is less than int and data from "a" may be lost. You need to manually convert.
With classes, everything is about the same.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question