Widening Primitive Conversion in Java - GeeksforGeeks

18 Dec, 2021
Whenever we do use double quotes around a letter or string as we all know it is treated as a string but when we do use a single quote round letter alongside performing some computations then they are treated as integers values while printing for which we must have knowledge of ASCII table concept as in computer for every character being case sensitive there is a specific integer value assigned to it at the backend which pops out widening of primitive datatype conversion. Refer to this ASCII table and just peek out as you no longer need to remember the value corresponding to each just remember popular values as it is sequentially defined over some intervals such as “a”, “A” and so on.
Illustration:
Widening primitive conversion is applied to convert either or both operands as specified by the following rules. The result of adding Java chars, shorts, or bytes is an int datatype under the following conditions as listed:
If either operand is of type double, the other is converted to double.
Otherwise, if either operand is of type float, the other is converted to float.
Otherwise, if either operand is of type long, the other is converted to long.
Otherwise, both operands are converted to type int
Example 1:
Java
// Java Program to Illustrate// Widening Datatype Conversion// No Computations // Main classpublic class GFG {     // Main driver method    public static void main(String[] args)    {        // Printing values on console        System.out.print("Y"                         + "O");        System.out.print('L');        System.out.print('O');    }}
YOLO
Output explanation:
This will now print “YOLO” instead of “YO7679”. It is because the widening primitive conversion happens only when an operator like ‘+’ is present which expects at least an integer on both sides. Now let us adhere forward proposing another case as follows:
Example 2:
Java
// Java Program to Illustrate// Widening Datatype Conversion// Computations is Carried Out // Main classpublic class GFG {       // Main driver method    public static void main(String[] args)    {        // Printing values on console        System.out.print("Y"                         + "O");         // here computations is carried between letter        // literal        System.out.print('L' + 'O');    }}
YO155
Output explanation: 
When we use double quotes, the text is treated as a string and “YO” is printed, but when we use single quotes, the characters ‘L’ and ‘O’ are converted to int. This is called widening primitive conversion. After conversion to integer, the numbers are added ( ‘L’ is 76 and ‘O’ is 79) and 155 is printed.
This article is contributed by Anurag Rai. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
 
harshgundecha
solankimayank
kk773572498
Java-Data Types
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
HashMap in Java with Examples
Object Oriented Programming (OOPs) Concept in Java
Initialize an ArrayList in Java
Interfaces in Java
How to iterate any Map in Java
ArrayList in Java
Multidimensional Arrays in Java
Singleton Class in Java
Multithreading in Java
Set in Java