Type Casting
Introduction
In this post, I’ll explain the types of casting in Java and PHP. Type casting is important because whether it’s for data validation, processing it, or performing external API data conversions– type casting is what allows us to do that in many cases.
The what
Type casting is converting one value to another. There’s two types of it in PHP and Java since they’re statically typed languages. Without going too deep into the weeds, statically typed languages mean that the type of a variable (primitives; int
, bool
, string
, float
, etc) is known when it’s compiled. Practically, the data type of a variable is fixed once it is declared and cannot change throughout the program’s execution.
But more importantly, type safety ensures the compiler enforces strict rules. This depends on the internal implementation of the language itself, because of this distinction, PHP is considered weakly typed in comparison to Java. But both are considered strict in their enforcement when compared to dynamicaly typed languages, like Javascript. Because type safety helps catch errors more explicitly in PHP and Java, there is less guessing about how your program will operate depeding on the implementation. In this sense, PHP and Java are nice like that.
Implicit vs Explicit Coercion
Implicit Coercion is when the programming language itself performs the conversion of data types on its own. This happens automatically in the background. It’s like visiting a friend’s house for the first time. You notice everyone is barefoot inside. Without anyone explicitly telling you, you take off your shoes at the door. You take off your footwear implicitly based on the observed behavior of your friend as the host. The house rules, like the programming language, dictate the conversion, in this case, removing shoes, even though you weren’t directly instructed.
Explicit Coercion is when we intentionally use built-in functions that force a conversion. Most commonly working with JSON. It’s like specifically asking the waiter for a fork and knife at a restaurant when your meal arrives.
There’s an argument about what constitutes implicit and explicit behavior in programming. This arises because the distinction can be subjective, depending on the programmer’s prior knowledge.
In PHP
Above, $number
we declare a string that contains a number. Adding it to 5 with +
implicitly coerces it to an integer (10
). The second part explicitly casts $number to an integer before addition using (int)
.
In PHP, type casting is less common because it performs implicit type coercion, meaning it automatically converts types during operations. However, explicit casting can be useful for clarity or to force a specific conversion.
In Java
double num ;
int rounded ; // Cast double to integer (rounded = 3)
String numberString ;
int parsedNumber ; // Parse string to integer (parsedNumber = 10)
Above, num
is a double. Casting it to int
with truncates the decimal part in the rounded
variable. The second part converts a string numberString
to an integer using Integer.parseInt()
. This way, our methods handle potential parsing errors.
There’s more to type casting than what I wrote in this article, but if you’d like to read more. Below are some links. As always, I hope you have a great day!