CrazyEngineers
  • type casting in java

    radha gogia

    radha gogia

    @radha-BTDzli
    Updated: Oct 24, 2024
    Views: 1.3K
    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..
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Anoop Kumar

    MemberOct 6, 2014

    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
    Are you sure? This action cannot be undone.
    Cancel
  • micheal john

    MemberOct 6, 2014

    Boolean cannot be cast to Integer, because Boolean is not child of Number abstract class

    public final class Boolean implements java.io.Serializable,
                                          Comparable<Boolean>
    {
    ...
    }
    
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberOct 6, 2014

    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<Boolean>
    {
    ...
    }
    
    Perfect answer👍
    Are you sure? This action cannot be undone.
    Cancel
  • radha gogia

    MemberOct 7, 2014

    see in the pic that the decompiler actually shows that the boolean is internally taken as int.

    upload_2014-10-7_19-15-6
    Are you sure? This action cannot be undone.
    Cancel
  • Shashank Moghe

    MemberOct 7, 2014

    #-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.
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberOct 7, 2014

    #-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.
    Are you sure? This action cannot be undone.
    Cancel
  • micheal john

    MemberOct 7, 2014

    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).
    Are you sure? This action cannot be undone.
    Cancel
  • radha gogia

    MemberOct 7, 2014

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberOct 7, 2014

    Please see Oracle docs: <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html" target="_blank" rel="nofollow noopener noreferrer">Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)</a>
    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-#
    Are you sure? This action cannot be undone.
    Cancel
  • radha gogia

    MemberOct 8, 2014

    Anoop Kumar
    Please see Oracle docs: <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html" target="_blank" rel="nofollow noopener noreferrer">Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)</a>
    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 <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html" target="_blank" rel="nofollow noopener noreferrer">Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)</a>, 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
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberOct 8, 2014

    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.
    <reserved.>
    Are you sure? This action cannot be undone.
    Cancel
  • radha gogia

    MemberOct 9, 2014

    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.
    <reserved.>
    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.
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register