type casting in java

why can't we assign typecast a boolean into an int ,when internally the representation of boolean is in the form of an int variable holding 0 or 1,i.e. if u see the decompiler after compiling your program ,it stores the boolean variables in the form of integer variables,so then obviously when jvm internally is converting boolean into int,then why is not allowing the type casting into int.
e.g
boolean ui=true;
int yu=(int)ui;

so why is this an error..

Replies

  • Anoop Kumar
    Anoop Kumar
    Can you post the compiled file which shows Boolean is stored as 1/0?
    Here is code compiled code of following method in Eclipse.

    public static void main(String[] args) {
            Boolean b = true;
            int i = 10;
        }
    
    ---------------------------------
    
    // Method descriptor #15 ([Ljava/lang/String;)V
      // Stack: 1, Locals: 3
      public static void main(java.lang.String[] args);
        0  iconst_1
        1  invokestatic java.lang.Boolean.valueOf(boolean) : java.lang.Boolean [16]
        4  astore_1 [b]
        5  bipush 10
        7  istore_2 [i]
        8  return
          Line numbers:
            [pc: 0, line: 6]
            [pc: 5, line: 7]
            [pc: 8, line: 8]
          Local variable table:
            [pc: 0, pc: 9] local: args index: 0 type: java.lang.String[]
            [pc: 5, pc: 9] local: b index: 1 type: java.lang.Boolean
            [pc: 8, pc: 9] local: i index: 2 type: int
  • micheal john
    micheal john
    Boolean cannot be cast to Integer, because Boolean is not child of Number abstract class

    public final class Boolean implements java.io.Serializable,
                                          Comparable
    {
    ...
    }
    
  • Anoop Kumar
    Anoop Kumar
    micheal john
    Boolean cannot be cast to Integer, because Boolean is not child of Number abstract class

    public final class Boolean implements java.io.Serializable,
                                          Comparable
    {
    ...
    }
    
    Perfect answerđź‘Ť
  • radha gogia
    radha gogia
    see in the pic that the decompiler actually shows that the boolean is internally taken as int.

    upload_2014-10-7_19-15-6
  • Shashank Moghe
    Shashank Moghe
    #-Link-Snipped-# gave the perfect answer as to why it cannot typecast. Besides, if you want to get that done, you can always use IF and comparison to do so.
  • Anoop Kumar
    Anoop Kumar
    #-Link-Snipped-# : 1st, JD is not perfect , it just try to decode bytecode logic.
    2nd, JD is identifying boolean as int which is wrong. If you observe the Eclipse code I have given, boolean is being recognise as "Boolean" type.
    Anyway, #-Link-Snipped-# has given the answer.
  • micheal john
    micheal john
    Shashank Moghe
    #-Link-Snipped-# gave the perfect answer as to why it cannot typecast. Besides, if you want to get that done, you can always use IF and comparison to do so.
    Which version of java does your Java Decompiler supports,
    Decompilers tries to reconstruct the java file but the exact one(original).
  • radha gogia
    radha gogia
    Anoop Kumar
    #-Link-Snipped-# : 1st, JD is not perfect , it just try to decode bytecode logic.
    2nd, JD is identifying boolean as int which is wrong. If you observe the Eclipse code I have given, boolean is being recognise as "Boolean" type.
    Anyway, #-Link-Snipped-# has given the answer.
    okk i got it,but how much size is actually allocated for a boolean type variable,can we say it in terms of 1 byte or 1 bit,becoz i am getting different answers from various sources ,i studied on stackoverflow,there i got that actually boolean internally gets converted to int ,so little confusions are there.
  • Anoop Kumar
    Anoop Kumar
    Please see Oracle docs: Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)
    boolean: The boolean data type has only two possible values: true and false. 
    Use this data type for simple flags that track true/false conditions. 
    This data type represents one bit of information, but its "size" isn't something that's precisely defined.
    Here is your discussion about casting :
    #-Link-Snipped-#
  • radha gogia
    radha gogia
    Anoop Kumar
    Please see Oracle docs: Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)
    boolean: The boolean data type has only two possible values: true and false.
    Use this data type for simple flags that track true/false conditions.
    This data type represents one bit of information, but its "size" isn't something that's precisely defined.
    Here is your discussion about casting :
    #-Link-Snipped-#
    I read it ,but in the documentation it is written that The byte data type can be useful for saving memory in large Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics), where the memory savings actually matters,but it is true that implicitly byte,short,char all are converted to int ,then how come it saves memory..

    Also ,I agree now that size of boolean is undefined,it is totally jvm dependent but still i dnt understand that actually why the decompiler showed int.
    and does it mean that decompiler is wrong in many cases
  • Anoop Kumar
    Anoop Kumar
    radha gogia
    but it is true that implicitly byte,short,char all are converted to int ,then how come it saves memory..
    I never occurred to read this. If your statement is true then byte and int will occupy same memory while execution, it is true??

    Not sure about Byte for array, here is #-Link-Snipped-#discussion, I need to find out more about it.
  • radha gogia
    radha gogia
    Anoop Kumar
    I never occurred to read this. If your statement is true then byte and int will occupy same memory while execution, it is true??

    Not sure about Byte for array, here is #-Link-Snipped-#discussion, I need to find out more about it.
    actually sir this thing i got to realise through the working of decompiler.,becoz i loaded my class in the decompiler,i saw there that the char ,byte ,short variables were treated as int variables.

You are reading an archived discussion.

Related Posts

In the two pics ,the first pic shows that when i declare 2 character variables, and i see the decompiler,it shows that internally jvm takes these two character literals as...
Anyone here uses Google Sheets for maintaining their excel data online? I have multiple spreadsheets and want to combine their data into a single spreadsheet. In the main Google Sheet,...
LG G3 Stylus was launched today in India at a price of Rs. 21,500. So, a CEan requested to start a discussion about the comparison between LG G3 Stylus and...
At a time when people around the world were busy discussing Windows 10, some folks at Microsoft’s research wing were busy finalising a proof-of-concept technology that would transform any room...
The construction of the world's biggest telescope or TMT (Thirty Meter Telescope) on the Hawaii Island near the Mauna Kea Volcano has begun. That's right. With India, Japan, US, Canada...