File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Ordinal Coparisons Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Ordinal Coparisons Question" Watch "Ordinal Coparisons Question" New topic
Author

Ordinal Coparisons Question

Lydia Zhang
Greenhorn

Joined: Feb 24, 2002
Posts: 29
Hi guys,
I don't underestand why this code works? How to compare char to int, char to float?


Lydia
Francisco A Guimaraes
Ranch Hand

Joined: Mar 20, 2002
Posts: 182
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<br />SCJP<br />please use the [code][/code] tags when showing code.Click <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank" rel="nofollow">here</a> to see an example.
Francisco A Guimaraes
Ranch Hand

Joined: Mar 20, 2002
Posts: 182
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

    Joined: Feb 24, 2002
    Posts: 29
    Thanks Francisco!
    But I'm still confused of how to promote the char 'A' to int or float? Can you give me an example?

    Jose Botella
    Ranch Hand

    Joined: Jul 03, 2001
    Posts: 2120
    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.


    SCJP2. Please Indent your code using UBB Code
    Lydia Zhang
    Greenhorn

    Joined: Feb 24, 2002
    Posts: 29
    Thanks Jose! The binary is my weakest point, I really don't know how to display a number into binary... I'll review it.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Ordinal Coparisons Question