Questions tagged [casting]

Casting is a process where an object type is explicitly converted into another type if the conversion is allowed. This process might lead to a change in value.

Some algorithms have significant performance differences for different data-types, for example, calculating the N-dimensional median is much is much faster for integers than float type data. Therefore, the use of casting can be important for code performance.

19752 questions
195
votes
3 answers

Any difference between type assertions and the newer `as` operator in TypeScript?

Is there any difference between what the TypeScript spec calls a type assertion: var circle = createShape("circle"); And the newer as operator: var circle = createShape("circle") as Circle; Both of which are typically used for…
mk.
  • 11,360
  • 6
  • 40
  • 54
195
votes
31 answers

How to check that a string is an int, but not a double, etc.?

PHP has an intval() function that will convert a string to an integer. However I want to check that the string is an integer beforehand, so that I can give a helpful error message to the user if it's wrong. PHP has is_int(), but that returns false…
Amandasaurus
  • 58,203
  • 71
  • 188
  • 248
194
votes
2 answers

How to cast/convert pointer to reference in C++

How can I pass a pointer (Object *ob) to a function which prototype is void foo(Object &) ?
Dewsworld
  • 13,367
  • 23
  • 68
  • 104
193
votes
3 answers

In Objective-C, what is the equivalent of Java's "instanceof" keyword?

I would like to check whether an object (e.g. someObject) is assignable (cast-able) to a variable of another type (e.g. SpecifiedType). In Java, I can write: someObject instanceof SpecifiedType A related question is finding whether the runtime type…
Dimitris
  • 682
  • 3
  • 14
  • 19
193
votes
13 answers

convert double to int

What is the best way to convert a double to an int? Should a cast be used?
user496949
  • 83,087
  • 147
  • 309
  • 426
191
votes
6 answers

Value of type 'T' cannot be converted to

This is likely a a novice question, but google surprisingly did not provide an answer. I have this rather artificial method T HowToCast(T t) { if (typeof(T) == typeof(string)) { T newT1 = "some text"; T newT2 = (string)t; …
Alex
  • 2,301
  • 2
  • 16
  • 15
189
votes
7 answers

Android: How can I Convert String to Date?

I store current time in database each time application starts by user. Calendar c = Calendar.getInstance(); String str = c.getTime().toString(); Log.i("Current time", str); In database side, I store current time as string (as you see in…
Hesam
  • 52,260
  • 74
  • 224
  • 365
184
votes
8 answers

Explicit casting from super-class to sub-class

public class Animal { public void eat() {} } public class Dog extends Animal { public void eat() {} public void main(String[] args) { Animal animal = new Animal(); Dog dog = (Dog) animal; } } The assignment Dog dog…
saravanan
  • 5,339
  • 7
  • 45
  • 52
182
votes
14 answers

How to insert a character in a string at a certain position?

I'm getting in an int with a 6 digit value. I want to display it as a String with a decimal point (.) at 2 digits from the end of int. I wanted to use a float but was suggested to use String for a better display output (instead of 1234.5 will be…
daverocks
  • 2,293
  • 5
  • 19
  • 23
179
votes
6 answers

Kotlin: How to work with List casts: Unchecked Cast: kotlin.collections.List to kotlin.colletions.List

I want to write a function that returns every item in a List that is not the first or the last item (a via point). The function gets a generic List<*> as input. A result should only be returned if the elements of the list are of the type…
Lukas Lechner
  • 7,881
  • 7
  • 40
  • 53
179
votes
10 answers

dynamic_cast and static_cast in C++

I am quite confused with the dynamic_cast keyword in C++. struct A { virtual void f() { } }; struct B : public A { }; struct C { }; void f () { A a; B b; A* ap = &b; B* b1 = dynamic_cast (&a); // NULL, because 'a' is not a…
Vijay
  • 65,327
  • 90
  • 227
  • 319
177
votes
3 answers

Cast Object to Generic Type for returning

Is there a way to cast an object to return value of a method? I tried this way but it gave a compile time exception in "instanceof" part: public static T convertInstanceOfObject(Object o) { if (o instanceof T) { return (T) o; }…
sedran
  • 3,498
  • 3
  • 23
  • 39
173
votes
3 answers

Cast object to interface in TypeScript

I'm trying to make a cast in my code from the body of a request in express (using body-parser middleware) to an interface, but it's not enforcing type safety. This is my interface: export interface IToDoDto { description: string; status:…
Elias Garcia
  • 6,772
  • 11
  • 34
  • 62
172
votes
21 answers

Assignment in an if statement

I have a class Animal, and its subclass Dog. I often find myself coding the following lines: if (animal is Dog) { Dog dog = animal as Dog; dog.Name; ... } For the variable Animal animal;. Is there some syntax that allows me to…
michael
  • 1,849
  • 2
  • 13
  • 5
170
votes
6 answers

In C, why do some people cast the pointer before freeing it?

I'm working on an old code base and pretty much every invocation of free() uses a cast on its argument. For example, free((float *)velocity); free((float *)acceleration); free((char *)label); where each pointer is of the corresponding (and…
SO Stinks
  • 3,258
  • 4
  • 32
  • 37