casting

How java converts int to boolean

ぐ巨炮叔叔 提交于 2020-02-02 04:10:31
问题 When i convert: int B=1; boolean A=B; It gives error: Incompatible types, which is true But when I write this code: int C=0; boolean A=C==1; it gives false while if I change value of C to 1 it gives true. I don't understand how compiler is doing it. 回答1: int C=0; boolean A=C==1; The compiler first gives C a zero. Variable : C Value : 0 Now The Assignment statement, We know that the assignment statement evaluates the right part first and the gives it to the left. The right part ==> C == 1 Here

Error casting a generic type to a concrete one

时光怂恿深爱的人放手 提交于 2020-02-01 10:07:31
问题 I have the following TypeScript function: add(element: T) { if (element instanceof class1) (<class1>element).owner = 100; } the problem is that I'm getting the following error: error TS2012: Cannot convert 'T' to 'class1' Any ideas? 回答1: There is no guarantee that your types are compatible, so you have to double-cast, as per the following... class class1 { constructor(public owner: number) { } } class Example<T> { add(element: T) { if (element instanceof class1) { (<class1><any>element).owner

Boxed nullable underlying type can be cast to enum but boxed enum type can't be cast to nullable type

二次信任 提交于 2020-02-01 04:40:06
问题 Boxed nullable underlying type can be cast to enum but boxed enum type can't be cast to nullable type. And similarly, Boxed nullable enum can be cast to underlying type but boxed underlying type can't be cast to nullable enum. Ok, I know "boxed nullable type" is not the best way to describe it, but it's for the sake of the question. I'm aware it's the underlying value type that's getting boxed. I will show it with examples. Assume I have an enum with int as the underlying type. enum Sex {

Does casting change the declared/reference type at run-time?

大兔子大兔子 提交于 2020-01-30 07:43:47
问题 First, let me be clear about what I mean by the declared type. Assume SuperBoss is a superclass of the class Boss. SuperBoss mrBond = new Boss(); SuperBoss is the declared type, and Boss is the actual type. Personally, I think the declared type is changed at run-time due to the following run-time exception: SuperBoss mrWayne = new SuperBoss(); ((Boss)mrWayne).randomMethod(); //Exception: java.lang.ClassCastException: SuperBoss cannot be cast to Boss I know this may seem trivial, but I'm going

Does casting change the declared/reference type at run-time?

坚强是说给别人听的谎言 提交于 2020-01-30 07:42:34
问题 First, let me be clear about what I mean by the declared type. Assume SuperBoss is a superclass of the class Boss. SuperBoss mrBond = new Boss(); SuperBoss is the declared type, and Boss is the actual type. Personally, I think the declared type is changed at run-time due to the following run-time exception: SuperBoss mrWayne = new SuperBoss(); ((Boss)mrWayne).randomMethod(); //Exception: java.lang.ClassCastException: SuperBoss cannot be cast to Boss I know this may seem trivial, but I'm going

Convert basic_string<unsigned char> to basic_string<char> and vice versa

眉间皱痕 提交于 2020-01-30 06:04:44
问题 From the following Can I turn unsigned char into char and vice versa? it appears that converting a basic_string<unsigned char> to a basic_string<char> (i.e. std::string ) is a valid operation. But I can't figure out how to do it. For example what functions could perform the following conversions, filling in the functionality of these hypothetical stou and utos functions? typedef basic_string<unsigned char> u_string; int main() { string s = "dog"; u_string u = stou(s); string t = utos(u); } I

Convert basic_string<unsigned char> to basic_string<char> and vice versa

北城余情 提交于 2020-01-30 06:04:38
问题 From the following Can I turn unsigned char into char and vice versa? it appears that converting a basic_string<unsigned char> to a basic_string<char> (i.e. std::string ) is a valid operation. But I can't figure out how to do it. For example what functions could perform the following conversions, filling in the functionality of these hypothetical stou and utos functions? typedef basic_string<unsigned char> u_string; int main() { string s = "dog"; u_string u = stou(s); string t = utos(u); } I

Convert basic_string<unsigned char> to basic_string<char> and vice versa

时间秒杀一切 提交于 2020-01-30 06:04:28
问题 From the following Can I turn unsigned char into char and vice versa? it appears that converting a basic_string<unsigned char> to a basic_string<char> (i.e. std::string ) is a valid operation. But I can't figure out how to do it. For example what functions could perform the following conversions, filling in the functionality of these hypothetical stou and utos functions? typedef basic_string<unsigned char> u_string; int main() { string s = "dog"; u_string u = stou(s); string t = utos(u); } I

Is an array considered as boolean true in php?

ぐ巨炮叔叔 提交于 2020-01-30 04:58:31
问题 I have a quick question here. I know that the cakePHP find('first') function returns an array containing the first result if found, false otherwise. My question is this, what if I were to write a check like this: if(result_is_array) // that means I have data { // do something } else // that means result is a boolean { // do something else } Instead of checking whether the result obtained from find('first') is an array or not, can I just say: $result = $this->MyModel->find('first'); if($result

Is an array considered as boolean true in php?

与世无争的帅哥 提交于 2020-01-30 04:58:11
问题 I have a quick question here. I know that the cakePHP find('first') function returns an array containing the first result if found, false otherwise. My question is this, what if I were to write a check like this: if(result_is_array) // that means I have data { // do something } else // that means result is a boolean { // do something else } Instead of checking whether the result obtained from find('first') is an array or not, can I just say: $result = $this->MyModel->find('first'); if($result