Tutorial by Examples: casting

123.5.to_s #=> "123.5" String(123.5) #=> "123.5" Usually, String() will just call #to_s. Methods Kernel#sprintf and String#% behave similar to C: sprintf("%s", 123.5) #=> "123.5" "%s" % 123.5 #=> "123.5" "%d&quot...
"123.50".to_i #=> 123 Integer("123.50") #=> 123 A string will take the value of any integer at its start, but will not take integers from anywhere else: "123-foo".to_i # => 123 "foo-123".to_i # => 0 However, there is a difference when ...
"123.50".to_f #=> 123.5 Float("123.50") #=> 123.5 However, there is a difference when the string is not a valid Float: "something".to_f #=> 0.0 Float("something") # ArgumentError: invalid value for Float(): "something"
PHP will generally correctly guess the data type you intend to use from the context it's used in, however sometimes it is useful to manually force a type. This can be accomplished by prefixing the declaration with the name of the required type in parenthesis: $bool = true; var_dump($bool); // bool...
The boolean type cannot be cast to/from any other primitive type. A char can be cast to/from any numeric type by using the code-point mappings specified by Unicode. A char is represented in memory as an unsigned 16-bit integer value (2 bytes), so casting to byte (1 byte) will drop 8 of those bits (...
Numeric primitives can be cast in two ways. Implicit casting happens when the source type has smaller range than the target type. //Implicit casting byte byteVar = 42; short shortVar = byteVar; int intVar = shortVar; long longVar = intvar; float floatVar = longVar; double doubleVar = floatVar...
As with primitives, objects can be cast both explicitly and implicitly. Implicit casting happens when the source type extends or implements the target type (casting to a superclass or interface). Explicit casting has to be done when the source type is extended or implemented by the target type (ca...
Intents can be used to broadcast messages to other components of your application (such as a running background service) or to the entire Android system. To send a broadcast within your application, use the LocalBroadcastManager class: Intent intent = new Intent("com.example.YOUR_ACTION"...
Arrays are objects, but their type is defined by the type of the contained objects. Therefore, one cannot just cast A[] to T[], but each A member of the specific A[] must be cast to a T object. Generic example: public static <T, A> T[] castArray(T[] target, A[] array) { for (int i = 0; i...
Suppose that you have a pointer to an object of a polymorphic class: Shape *ps; // see example on defining a polymorphic class ps = get_a_new_random_shape(); // if you don't have such a function yet, you // could just write ps = new Square...
If you know that a value is of a specific type, you can explicitly cast it to that type in order to use it in a context where that type is needed. object value = -1; int number = (int) value; Console.WriteLine(Math.Abs(number)); If we tried passing value directly to Math.Abs(), we would get a ...
If you aren't sure whether a value is of the type you think it is, you can safely cast it using the as operator. If the value is not of that type, the resulting value will be null. object value = "-1"; int? number = value as int?; if(number != null) { Console.WriteLine(Math.Abs(nu...
A value will automatically be cast to the appropriate type if the compiler knows that it can always be converted to that type. int number = -1; object value = number; Console.WriteLine(value); In this example, we didn't need to use the typical explicit casting syntax because the compiler knows...
If you need to know whether a value's type extends or implements a given type, but you don't want to actually cast it as that type, you can use the is operator. if(value is int) { Console.WriteLine(value + "is an int"); }
Suppose you have types like the following: interface IThing { } class Thing : IThing { } LINQ allows you to create a projection that changes the compile-time generic type of an IEnumerable<> via the Enumerable.Cast<>() and Enumerable.OfType<>() extension methods. IEnumerabl...
Type casting is done with either the as operator: var chair:Chair = furniture as Chair; Or by wrapping the value in Type(): var chair:Chair = Chair(furniture); If the cast fails with as, the result of that cast is null. If the cast fails by wrapping in Type(), a TypeError is thrown.
Arithmetic operations are performed elementwise on Numpy arrays. For arrays of identical shape, this means that the operation is executed between elements at corresponding indices. # Create two arrays of the same size a = np.arange(6).reshape(2, 3) b = np.ones(6).reshape(2, 3) a # array([0, 1...
A variable can be downcasted to a subtype using the type cast operators as?, and as!. The as? operator attempts to cast to a subtype. It can fail, therefore it returns an optional. let value: Any = "John" let name = value as? String print(name) // prints Optional("John") ...
The switch statement can also be used to attempt casting into different types: func checkType(_ value: Any) -> String { switch value { // The `is` operator can be used to check a type case is Double: return "value is a Double" // The `as` operator will ...
A pointer to a const object can be converted to a pointer to non-const object using the const_cast keyword. Here we use const_cast to call a function that is not const-correct. It only accepts a non-const char* argument even though it never writes through the pointer: void bad_strlen(char*); const...

Page 1 of 2