Wednesday, July 3, 2024
HomeLanguagesJavaWidening Primitive Conversion in Java

Widening Primitive Conversion in Java

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 class
public 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');
    }
}


Output

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 class
public 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');
    }
}


Output

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 Lazyroar and would like to contribute, you can also write an article and mail your article to review-team@neveropen.co.za. See your article appearing on the Lazyroar main page and help other Geeks.

 

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments