Win a copy of Terraform in Action this week in the Cloud forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Tim Cooke
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• Liutauras Vilda
Sheriffs:
• Jeanne Boyarsky
• Rob Spoor
• Bear Bibeault
Saloon Keepers:
• Jesse Silverman
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• Carey Brown
Bartenders:
• Piet Souris
• Al Hobbs
• salvin francis

# Ordinal Coparisons Question

Greenhorn
Posts: 29
• Number of slices to send:
Optional 'thank-you' note:
Hi guys,
I don't underestand why this code works? How to compare char to int, char to float?

Ranch Hand
Posts: 182
• Number of slices to send:
Optional 'thank-you' note:
when dealing with non-boolean primitives, you can compare any of them with any other, because the smaller operand is promoted to the type of the greater.
Francisco

Francisco A Guimaraes
Ranch Hand
Posts: 182
• Number of slices to send:
Optional 'thank-you' note:
I found a more precise rule in the JLS 5.6.2:

• 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.

• []�s
Francisco

Lydia Zhang
Greenhorn
Posts: 29
• Number of slices to send:
Optional 'thank-you' note:
Thanks Francisco!
But I'm still confused of how to promote the char 'A' to int or float? Can you give me an example?

Ranch Hand
Posts: 2120
• Number of slices to send:
Optional 'thank-you' note:
There is a chapter in JLS 5 about conversions and promotions.

Let's say 'A' char is 0041 in hex (two bytes)
To convert to a bigger type add enough zeros to its left up to the number of digits in the target type:
0000 0041 This is an int (4 bytes) with the same value as the char 'A'.
If the number is negative you will have to add 1s instead.
Review the binary, hexadecimal and two's complement
formats if needed.

Lydia Zhang
Greenhorn
Posts: 29
• Number of slices to send:
Optional 'thank-you' note:
Thanks Jose! The binary is my weakest point, I really don't know how to display a number into binary... I'll review it.

 You showed up just in time for the waffles! And this tiny ad: Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton