Type casting converts a value from one data type to another. In automation, you frequently convert between types — parsing a string from a web element into a number, or converting an int to a double for calculations.
Widening casting happens automatically when you assign a smaller type to a larger type. No data is lost because the larger type can hold all values of the smaller type.
byte → short → int → long → float → doublepublic class WideningCast {
public static void main(String[] args) {
int itemCount = 5;
double itemCountDouble = itemCount; // int → double (automatic)
System.out.println(itemCountDouble); // 5.0
int score = 85;
long scoreLong = score; // int → long (automatic)
System.out.println(scoreLong); // 85
char letter = 'A';
int ascii = letter; // char → int (gives ASCII value)
System.out.println(ascii); // 65
}
}Narrowing casting must be done explicitly by placing the target type in parentheses. Data may be lost — the decimal part is truncated, not rounded.
public class NarrowingCast {
public static void main(String[] args) {
double price = 49.99;
int priceInt = (int) price; // double → int (manual)
System.out.println(priceInt); // 49 — decimal truncated
double percentage = 87.6;
int rounded = (int) Math.round(percentage); // proper rounding
System.out.println(rounded); // 88
int bigNumber = 130;
byte small = (byte) bigNumber; // int → byte (overflow!)
System.out.println(small); // -126 — byte range is -128 to 127
}
}Narrowing a value outside the target range causes overflow — the value wraps around. (byte) 130 gives -126, not an error. Always check ranges before narrowing.
In test automation, you extract text from web elements and need to convert it to numbers for assertions. This is the most common type conversion you will perform.
public class StringConversion {
public static void main(String[] args) {
// String to int
String countText = "42";
int count = Integer.parseInt(countText);
System.out.println(count + 1); // 43 (math, not concat)
// String to double
String priceText = "$29.99";
double price = Double.parseDouble(priceText.replace("$", ""));
System.out.println(price); // 29.99
// Number to String
int score = 95;
String scoreText = String.valueOf(score);
System.out.println("Score: " + scoreText); // Score: 95
// Alternative: concatenate with empty string
String scoreText2 = "" + score;
System.out.println(scoreText2); // 95
}
}public class ParseErrors {
public static void main(String[] args) {
// This crashes at runtime
// String invalid = "abc";
// int num = Integer.parseInt(invalid); // NumberFormatException
// Common in automation — element text has spaces or symbols
String itemCount = " 5 items ";
int count = Integer.parseInt(itemCount.trim().split(" ")[0]);
System.out.println(count); // 5
}
}Q: What is the difference between widening and narrowing casting?
A: Widening converts a smaller type to a larger type (int → double) — it is automatic and safe because no data is lost. Narrowing converts a larger type to a smaller type (double → int) — it must be explicit with a cast operator and may lose data. The decimal portion is truncated in narrowing, not rounded.
Exercise 1: A web element shows "₹1,299.50" as text. Write code that extracts the numeric value as a double. Remove the currency symbol and comma before parsing.
Exercise 2: What does this output? Think first, then run:
``
double d = 9.7;
int i = (int) d;
System.out.println(i);
System.out.println((int) -9.7);
``