type casting in java
e.g
boolean ui=true;
int yu=(int)ui;
so why is this an error..
Member • Oct 6, 2014
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
Member • Oct 6, 2014
public final class Boolean implements java.io.Serializable, Comparable<Boolean> { ... }
Member • Oct 6, 2014
Perfect answer👍micheal johnBoolean cannot be cast to Integer, because Boolean is not child of Number abstract class
public final class Boolean implements java.io.Serializable, Comparable<Boolean> { ... }
Member • Oct 7, 2014
Member • Oct 7, 2014
Member • Oct 7, 2014
Member • Oct 7, 2014
Which version of java does your Java Decompiler supports,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.
Member • Oct 7, 2014
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#-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.
Member • Oct 7, 2014
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 :
Member • Oct 8, 2014
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..Anoop KumarPlease 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-#
Member • Oct 8, 2014
I never occurred to read this. If your statement is true then byte and int will occupy same memory while execution, it is true??radha gogiabut it is true that implicitly byte,short,char all are converted to int ,then how come it saves memory..
Member • Oct 9, 2014
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.Anoop KumarI 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.>