Converting a variable from one type to another

learn more… | top users | synonyms

1
vote
1answer
41 views

Removing dependencies on subclass-specific behavior

I have a Message class which can contain multiple types of payloads (or sometimes no payload), each derived from a common Payload class. However, this becomes problematic because the Message class ...
5
votes
2answers
305 views

Is there a name for the number of values a variable can take? [closed]

For example, a bit or a boolean can be either 0 or 1 so the number 2 is associated with it. Similarly, for a byte which is 8 bits, the maximum number of different assignments would be 2^8. Is there a ...
14
votes
9answers
14k views

Type Casting variables in PHP: Is there a practical example?

PHP, as most of us know, has weak typing. For those who don't, PHP.net says: PHP does not require (or support) explicit type definition in variable declaration; a variable's type is determined ...
1
vote
1answer
164 views

When is type coercion a good idea?

When do you want (on purpose) to coerce type in JavaScript? I mean, beyond comparing strings and numbers, when does it benefit something you're building? Examples of type coercion: "The answer is " ...
15
votes
4answers
650 views

What is casting supposed to mean?

When coding in low level languages like C I find that casting sometimes means 'reinterpret these bytes as if it had always been of this other type' and at other times as 'convert this value ...
1
vote
1answer
170 views

Loose typing not applied to objects

I have very little experience working with classes and object. I work in a loosely typed language, PHP. I was working with a SimpleXML object and ran into a problem where I was trying to do math with ...
7
votes
8answers
5k views

c style casts or c++ style casts

So, what do you use? int anInt = (int)aFloat; or int anInt = static_cast<int>(aFloat); // and its brethren And, more importantly, why?
-3
votes
1answer
195 views

Casting from string to integer and the vica versa [closed]

I am trying to cast in Java from string to integer and the other way around but the compiler is complaining about this. My question is: Is this the matter of compiler or Java programming language ...
1
vote
2answers
484 views

Type parameterized methods to avoid casting

In a library I am developing I have a factory method that looks a bit like: public <T> T instantiate(Type targetType) { return ...; } I am using a type parameterized method to avoid that ...
0
votes
2answers
161 views

Boolean-Integer Typecasting to Replace Conditional

When choosing a value based off of 2 boolean values in this format var foo:int; if (X){ foo = 50; } else if (Y){ foo = -50; } else { foo = 0; } I discovered that I can condense this ...
7
votes
2answers
440 views

implement cons function in Java - type safety question

I am working on a small functional library written in Java, which mimics the a functional style of programming. I am stuck with a undesirable type cast in one of my method definitions and would love ...
1
vote
2answers
552 views

C++ Typecasting VS performance

Let's say we're designing a video game. We have some sprites on the map and we want to call some method of the particular sprite at some particular position. We are using one broadly-known C++ ...
4
votes
2answers
248 views

Alternatives to type casting in your domain

In my Domain I have an entity Activity which has a list of ITasks. Each implementation of this task has it's own properties beside the implementation of ITask itself. Now each operation of the ...
2
votes
1answer
2k views

Java default Integer value is int

My code looks like this import java.util.Scanner; public class StudentGrades { public static void main(String[] argv) { Scanner keyboard = new Scanner(System.in); byte q1 = ...
7
votes
3answers
2k views

Is there a difference between casting and converting types in imperative programming languages?

The question came up in a discussion at StackOverflow. Is there a clean distinction between the two concepts cast and convert (concerning the type of an object), or are these two words describing ...