Typecasting

From CodeCodex

Revision as of 23:33, 1 August 2006 by Jdh30 (Talk | contribs)

Implementations

C++

int x = 2
string y = (string)x

C#

int x = 2
string y = (string)x

Java

double doubleName = 3;

int intName = (int)doubleName;  
  // intName becomes == 3


double anotherDoubleName = 3.3;
int anotherIntName = (int)anotherDoubleName;  
  //anotherIntName becomes == 3, not 3.3

OCaml

OCaml's strong static type system disallows arbitrary conversions of values between types. Instead, one must use either the built-in conversion functions or write additional functions (e.g. for your own data structures). For example, the built-in string_of_int function converts an int to a string:

# string_of_int 1234;;
- : string = "1234"

Visual Basic .Net

dim x as integer = 2
dim y as string = ctype(x,string)