THE CONDITIONAL OPERATOR
Later in the course we will learn how to use conditional tests, that is, instructions that allow us to execute different blocks of code depending or not on the occurrence of certain conditions. The conditional operator although in a limited form enables us to achieve the same purpose. It is a ternary operator, that is, it uses three operands.
exp1 is a boolean expression. If the expression exp1 returns true then the second expression is executed, otherwise if false exp3. Let’s take an example.
EXAMPLE TERNARY OPERATOR
In the figure opposite I give an example. In this case z is assigned the value 50 because the conditional test (x < y ) exp1 returns true and then literal 50 (exp2) is executed and assigned to the variable z. Conditional testing is often used in Java; it makes code more compact than normal conditional blocks.
CONVERSIONS OF TYPE
Returning to the subject of the assignment operator, it is also important to check when the operands are of different types from each other. Java performs very strict controls on Data Type. When assigning one variable to another, the first thing Java can do is an Automatic Type Conversion.
Both conditions are met, int and double are compatible with each other, moreover the destination which is the type that is to the left of the = symbol is more capacious than the type that is to the right of the = symbol i.e. an int (source). Let us now see in the next image the compatibility of types.
THE CAST
Regarding automatic conversion this is possible if the types are compatible with each other and if the destination is larger than the source. When automatic conversion is not possible, cast or explicit type conversion is used.
(target-type)exp;
This is the form of a cast in java, you put the Data Type before the expression in parentheses. Let’s take an example.
In the example opposite we can see that the types are numeric; therefore, compatible but the destination (int) is less large than the source (double). To solve this problem, cast is used by losing the decimal part.
When we use cast to perform a conversion explicitly, we are using narrowing conversion as a result of which we actually
we may have a loss of information.
TYPE CONVERSIONS IN EXPRESSIONS
Within an expression we can use variables, operators, literals. If the types in the expressions are compatible with each other Java performs automatic conversions based on what are called promotion rules. Let’s see what these rules are:
All char, byte, short are converted to int. If there is a long within the expression, then the whole expression is converted to long. Same thing applies to float and double, that is, if there is a float in the expression the whole expression is converted to float, if there is a double the whole expression is converted to double. Let’s look at an example.
Since Java has promoted x and y to int the assignment to a short cannot be done. However, one can perform the cast by converting the integer to a short as seen in the figure.
LINKS TO PREVIOUS POSTS
LINK TO CODE ON GITHUB
EXECUTION OF THE EXAMPLE CODE
- Download the code from GITHUB, launch the JAR file with the following command in Visual Studio Code, locating in the directory containing the JAR.
java -jar –enable-preview CourseJava.jar
- Or run the main found in the file CorsoJava.java.
Leave A Comment