Data Types
Type Conversion
Changing Data Types
A web form returns the user's age as text: "25". To check if they're old enough to vote (age >= 18), you need to convert "25" to the number 25. Type conversion bridges the gap between data formats.
Int to double (precise division)
When dividing integers, you might want a decimal result.
public class IntToDouble {
public static void main(String[] args) {
int a = 7;
int b = 3;
// Integer division - truncates!
int intResult = a / b;
System.out.println("Integer division: " + a + "/" + b + " = " + intResult);
// Convert to double for precise division
double doubleResult = (double) a / b;
System.out.println("Double division: " + a + "/" + b + " = " + doubleResult);
// Practical: calculate percentage
int score = 85;
int total = 100;
double percentage = (double) score / total * 100;
System.out.println("Percentage: " + percentage + "%");
}
}
public class IntToDouble {
public static void main(String[] args) {
int a = 5;
int b = 3;
// Integer division - truncates!
int intResult = a / b;
System.out.println("Integer division: " + a + "/" + b + " = " + intResult);
// Convert to double for precise division
double doubleResult = (double) a / b;
System.out.println("Double division: " + a + "/" + b + " = " + doubleResult);
// Practical: calculate percentage
int score = 85;
int total = 100;
double percentage = (double) score / total * 100;
System.out.println("Percentage: " + percentage + "%");
}
}
public class IntToDouble {
public static void main(String[] args) {
int a = 10;
int b = 3;
// Integer division - truncates!
int intResult = a / b;
System.out.println("Integer division: " + a + "/" + b + " = " + intResult);
// Convert to double for precise division
double doubleResult = (double) a / b;
System.out.println("Double division: " + a + "/" + b + " = " + doubleResult);
// Practical: calculate percentage
int score = 85;
int total = 100;
double percentage = (double) score / total * 100;
System.out.println("Percentage: " + percentage + "%");
}
}
public class IntToDouble {
public static void main(String[] args) {
int a = 7;
int b = 2;
// Integer division - truncates!
int intResult = a / b;
System.out.println("Integer division: " + a + "/" + b + " = " + intResult);
// Convert to double for precise division
double doubleResult = (double) a / b;
System.out.println("Double division: " + a + "/" + b + " = " + doubleResult);
// Practical: calculate percentage
int score = 85;
int total = 100;
double percentage = (double) score / total * 100;
System.out.println("Percentage: " + percentage + "%");
}
}
public class IntToDouble {
public static void main(String[] args) {
int a = 7;
int b = 4;
// Integer division - truncates!
int intResult = a / b;
System.out.println("Integer division: " + a + "/" + b + " = " + intResult);
// Convert to double for precise division
double doubleResult = (double) a / b;
System.out.println("Double division: " + a + "/" + b + " = " + doubleResult);
// Practical: calculate percentage
int score = 85;
int total = 100;
double percentage = (double) score / total * 100;
System.out.println("Percentage: " + percentage + "%");
}
}
a ← 7, b ← 3, intResult ← 2, doubleResult ← 2.3333333333333335
1public class IntToDouble {2 public static void main(String[] args) {3 int a→ 7 = 7; //@a=5, 104 int b→ 3 = 3; //@b=2, 45 6 // Integer division - truncates!7 int intResult→ 2 = a7 / b3;8 System.out.println("Integer division: " + a7 + "/" + b3 + " = " + intResult2);9 10 // Convert to double for precise division11 double doubleResult→ 2.3333333333333335 = (double) a / b3;12 System.out.println("Double division: " + a7 + "/" + b3 + " = " + doubleResult2.3333333333333335);13 14 // Practical: calculate percentage15 int score→ 85 = 85;16 int total→ 100 = 100;17 double percentage→ 85.0 = (double) score / total100 * 100;18 System.out.println("Percentage: " + percentage85.0 + "%");19 }outputInteger division: 7/3 = 2 Double division: 7/3 = 2.3333333333333335 Percentage: 85.0%
a ← 5, b ← 3, intResult ← 1, doubleResult ← 1.6666666666666667
1public class IntToDouble {2 public static void main(String[] args) {3 int a→ 5 = 5;4 int b→ 3 = 3;5 6 // Integer division - truncates!7 int intResult→ 1 = a5 / b3;8 System.out.println("Integer division: " + a5 + "/" + b3 + " = " + intResult1);9 10 // Convert to double for precise division11 double doubleResult→ 1.6666666666666667 = (double) a / b3;12 System.out.println("Double division: " + a5 + "/" + b3 + " = " + doubleResult1.6666666666666667);13 14 // Practical: calculate percentage15 int score→ 85 = 85;16 int total→ 100 = 100;17 double percentage→ 85.0 = (double) score / total100 * 100;18 System.out.println("Percentage: " + percentage85.0 + "%");19 }outputInteger division: 5/3 = 1 Double division: 5/3 = 1.6666666666666667 Percentage: 85.0%
a ← 10, b ← 3, intResult ← 3, doubleResult ← 3.3333333333333335
1public class IntToDouble {2 public static void main(String[] args) {3 int a→ 10 = 10;4 int b→ 3 = 3;5 6 // Integer division - truncates!7 int intResult→ 3 = a10 / b3;8 System.out.println("Integer division: " + a10 + "/" + b3 + " = " + intResult3);9 10 // Convert to double for precise division11 double doubleResult→ 3.3333333333333335 = (double) a / b3;12 System.out.println("Double division: " + a10 + "/" + b3 + " = " + doubleResult3.3333333333333335);13 14 // Practical: calculate percentage15 int score→ 85 = 85;16 int total→ 100 = 100;17 double percentage→ 85.0 = (double) score / total100 * 100;18 System.out.println("Percentage: " + percentage85.0 + "%");19 }outputInteger division: 10/3 = 3 Double division: 10/3 = 3.3333333333333335 Percentage: 85.0%
a ← 7, b ← 2, intResult ← 3, doubleResult ← 3.5, score ← 85, total ← 100
1public class IntToDouble {2 public static void main(String[] args) {3 int a→ 7 = 7;4 int b→ 2 = 2;5 6 // Integer division - truncates!7 int intResult→ 3 = a7 / b2;8 System.out.println("Integer division: " + a7 + "/" + b2 + " = " + intResult3);9 10 // Convert to double for precise division11 double doubleResult→ 3.5 = (double) a / b2;12 System.out.println("Double division: " + a7 + "/" + b2 + " = " + doubleResult3.5);13 14 // Practical: calculate percentage15 int score→ 85 = 85;16 int total→ 100 = 100;17 double percentage→ 85.0 = (double) score / total100 * 100;18 System.out.println("Percentage: " + percentage85.0 + "%");19 }outputInteger division: 7/2 = 3 Double division: 7/2 = 3.5 Percentage: 85.0%
a ← 7, b ← 4, intResult ← 1, doubleResult ← 1.75, score ← 85, total ← 100
1public class IntToDouble {2 public static void main(String[] args) {3 int a→ 7 = 7;4 int b→ 4 = 4;5 6 // Integer division - truncates!7 int intResult→ 1 = a7 / b4;8 System.out.println("Integer division: " + a7 + "/" + b4 + " = " + intResult1);9 10 // Convert to double for precise division11 double doubleResult→ 1.75 = (double) a / b4;12 System.out.println("Double division: " + a7 + "/" + b4 + " = " + doubleResult1.75);13 14 // Practical: calculate percentage15 int score→ 85 = 85;16 int total→ 100 = 100;17 double percentage→ 85.0 = (double) score / total100 * 100;18 System.out.println("Percentage: " + percentage85.0 + "%");19 }outputInteger division: 7/4 = 1 Double division: 7/4 = 1.75 Percentage: 85.0%
Integer division truncates. Convert to double for precise results.
Double to int (truncation)
Sometimes you need a whole number from a decimal.
public class DoubleToInt {
public static void main(String[] args) {
double price = 19.99;
// Cast truncates (drops decimals)
int truncated = (int) price;
System.out.println("Price: " + price);
System.out.println("Truncated: " + truncated);
// Math.round() rounds to nearest
long rounded = Math.round(price);
System.out.println("Rounded: " + rounded);
// Floor and ceiling
int floor = (int) Math.floor(price);
int ceiling = (int) Math.ceil(price);
System.out.println("Floor: " + floor);
System.out.println("Ceiling: " + ceiling);
// Practical: convert dollars to cents (avoid floating point)
int cents = (int) Math.round(price * 100);
System.out.println("Cents: " + cents);
}
}
public class DoubleToInt {
public static void main(String[] args) {
double price = 25.50;
// Cast truncates (drops decimals)
int truncated = (int) price;
System.out.println("Price: " + price);
System.out.println("Truncated: " + truncated);
// Math.round() rounds to nearest
long rounded = Math.round(price);
System.out.println("Rounded: " + rounded);
// Floor and ceiling
int floor = (int) Math.floor(price);
int ceiling = (int) Math.ceil(price);
System.out.println("Floor: " + floor);
System.out.println("Ceiling: " + ceiling);
// Practical: convert dollars to cents (avoid floating point)
int cents = (int) Math.round(price * 100);
System.out.println("Cents: " + cents);
}
}
public class DoubleToInt {
public static void main(String[] args) {
double price = 9.95;
// Cast truncates (drops decimals)
int truncated = (int) price;
System.out.println("Price: " + price);
System.out.println("Truncated: " + truncated);
// Math.round() rounds to nearest
long rounded = Math.round(price);
System.out.println("Rounded: " + rounded);
// Floor and ceiling
int floor = (int) Math.floor(price);
int ceiling = (int) Math.ceil(price);
System.out.println("Floor: " + floor);
System.out.println("Ceiling: " + ceiling);
// Practical: convert dollars to cents (avoid floating point)
int cents = (int) Math.round(price * 100);
System.out.println("Cents: " + cents);
}
}
price ← 19.99, truncated ← 19, rounded ← 20, floor ← 19, ceiling ← 20
1public class DoubleToInt {2 public static void main(String[] args) {3 double price→ 19.99 = 19.99; //@price=25.50, 9.954 5 // Cast truncates (drops decimals)6 int truncated→ 19 = (int) price;7 System.out.println("Price: " + price19.99);8 System.out.println("Truncated: " + truncated19);9 10 // Math.round() rounds to nearest11 long rounded→ 20 = Math.round(price19.99);12 System.out.println("Rounded: " + rounded20);13 14 // Floor and ceiling15 int floor→ 19 = (int) Math.floor(price19.99);16 int ceiling→ 20 = (int) Math.ceil(price19.99);17 System.out.println("Floor: " + floor19);18 System.out.println("Ceiling: " + ceiling20);19 20 // Practical: convert dollars to cents (avoid floating point)21 int cents→ 1999 = (int) Math.round(price19.99 * 100);22 System.out.println("Cents: " + cents1999);23 }outputPrice: 19.99 Truncated: 19 Rounded: 20 Floor: 19 Ceiling: 20 Cents: 1999
price ← 25.5, truncated ← 25, rounded ← 26, floor ← 25, ceiling ← 26
1public class DoubleToInt {2 public static void main(String[] args) {3 double price→ 25.5 = 25.50;4 5 // Cast truncates (drops decimals)6 int truncated→ 25 = (int) price;7 System.out.println("Price: " + price25.5);8 System.out.println("Truncated: " + truncated25);9 10 // Math.round() rounds to nearest11 long rounded→ 26 = Math.round(price25.5);12 System.out.println("Rounded: " + rounded26);13 14 // Floor and ceiling15 int floor→ 25 = (int) Math.floor(price25.5);16 int ceiling→ 26 = (int) Math.ceil(price25.5);17 System.out.println("Floor: " + floor25);18 System.out.println("Ceiling: " + ceiling26);19 20 // Practical: convert dollars to cents (avoid floating point)21 int cents→ 2550 = (int) Math.round(price25.5 * 100);22 System.out.println("Cents: " + cents2550);23 }outputPrice: 25.5 Truncated: 25 Rounded: 26 Floor: 25 Ceiling: 26 Cents: 2550
price ← 9.95, truncated ← 9, rounded ← 10, floor ← 9, ceiling ← 10
1public class DoubleToInt {2 public static void main(String[] args) {3 double price→ 9.95 = 9.95;4 5 // Cast truncates (drops decimals)6 int truncated→ 9 = (int) price;7 System.out.println("Price: " + price9.95);8 System.out.println("Truncated: " + truncated9);9 10 // Math.round() rounds to nearest11 long rounded→ 10 = Math.round(price9.95);12 System.out.println("Rounded: " + rounded10);13 14 // Floor and ceiling15 int floor→ 9 = (int) Math.floor(price9.95);16 int ceiling→ 10 = (int) Math.ceil(price9.95);17 System.out.println("Floor: " + floor9);18 System.out.println("Ceiling: " + ceiling10);19 20 // Practical: convert dollars to cents (avoid floating point)21 int cents→ 995 = (int) Math.round(price9.95 * 100);22 System.out.println("Cents: " + cents995);23 }outputPrice: 9.95 Truncated: 9 Rounded: 10 Floor: 9 Ceiling: 10 Cents: 995
Casting to int truncates (drops decimals). Use Math.round() to round.
String to number (parse input)
User input is text. Convert to numbers for calculation.
public class Parse {
public static void main(String[] args) {
// Simulated user input
String ageInput = "25";
String priceInput = "19.99";
// Parse string to number
int age = Integer.parseInt(ageInput);
double price = Double.parseDouble(priceInput);
System.out.println("Age string: " + ageInput);
System.out.println("Age number: " + age);
System.out.println("Age + 1 = " + (age + 1));
System.out.println("Price string: " + priceInput);
System.out.println("Price number: " + price);
System.out.println("With tax: " + (price * 1.1));
// Also works for other types
boolean active = Boolean.parseBoolean("true");
System.out.println("Active: " + active);
}
}
public class Parse {
public static void main(String[] args) {
// Simulated user input
String ageInput = "30";
String priceInput = "19.99";
// Parse string to number
int age = Integer.parseInt(ageInput);
double price = Double.parseDouble(priceInput);
System.out.println("Age string: " + ageInput);
System.out.println("Age number: " + age);
System.out.println("Age + 1 = " + (age + 1));
System.out.println("Price string: " + priceInput);
System.out.println("Price number: " + price);
System.out.println("With tax: " + (price * 1.1));
// Also works for other types
boolean active = Boolean.parseBoolean("true");
System.out.println("Active: " + active);
}
}
public class Parse {
public static void main(String[] args) {
// Simulated user input
String ageInput = "17";
String priceInput = "19.99";
// Parse string to number
int age = Integer.parseInt(ageInput);
double price = Double.parseDouble(priceInput);
System.out.println("Age string: " + ageInput);
System.out.println("Age number: " + age);
System.out.println("Age + 1 = " + (age + 1));
System.out.println("Price string: " + priceInput);
System.out.println("Price number: " + price);
System.out.println("With tax: " + (price * 1.1));
// Also works for other types
boolean active = Boolean.parseBoolean("true");
System.out.println("Active: " + active);
}
}
public class Parse {
public static void main(String[] args) {
// Simulated user input
String ageInput = "25";
String priceInput = "9.95";
// Parse string to number
int age = Integer.parseInt(ageInput);
double price = Double.parseDouble(priceInput);
System.out.println("Age string: " + ageInput);
System.out.println("Age number: " + age);
System.out.println("Age + 1 = " + (age + 1));
System.out.println("Price string: " + priceInput);
System.out.println("Price number: " + price);
System.out.println("With tax: " + (price * 1.1));
// Also works for other types
boolean active = Boolean.parseBoolean("true");
System.out.println("Active: " + active);
}
}
public class Parse {
public static void main(String[] args) {
// Simulated user input
String ageInput = "25";
String priceInput = "100.00";
// Parse string to number
int age = Integer.parseInt(ageInput);
double price = Double.parseDouble(priceInput);
System.out.println("Age string: " + ageInput);
System.out.println("Age number: " + age);
System.out.println("Age + 1 = " + (age + 1));
System.out.println("Price string: " + priceInput);
System.out.println("Price number: " + price);
System.out.println("With tax: " + (price * 1.1));
// Also works for other types
boolean active = Boolean.parseBoolean("true");
System.out.println("Active: " + active);
}
}
ageInput ← 25, priceInput ← 19.99, age ← 25, price ← 19.99, active ← true
1public class Parse {2 public static void main(String[] args) {3 // Simulated user input4 String ageInput→ 25 = "25"; //@ageInput="30", "17"5 String priceInput→ 19.99 = "19.99"; //@priceInput="9.95", "100.00"6 7 // Parse string to number8 int age→ 25 = Integer.parseInt(ageInput25);9 double price→ 19.99 = Double.parseDouble(priceInput19.99);10 11 System.out.println("Age string: " + ageInput25);12 System.out.println("Age number: " + age25);13 System.out.println("Age + 1 = " + (age25 + 1));14 15 System.out.println("Price string: " + priceInput19.99);16 System.out.println("Price number: " + price19.99);17 System.out.println("With tax: " + (price19.99 * 1.1));18 19 // Also works for other types20 boolean active→ true = Boolean.parseBoolean("true");21 System.out.println("Active: " + activetrue);22 }outputAge string: 25 Age number: 25 Age + 1 = 26 Price string: 19.99 Price number: 19.99 With tax: 21.989 Active: true
ageInput ← 30, priceInput ← 19.99, age ← 30, price ← 19.99, active ← true
1public class Parse {2 public static void main(String[] args) {3 // Simulated user input4 String ageInput→ 30 = "30";5 String priceInput→ 19.99 = "19.99";6 7 // Parse string to number8 int age→ 30 = Integer.parseInt(ageInput30);9 double price→ 19.99 = Double.parseDouble(priceInput19.99);10 11 System.out.println("Age string: " + ageInput30);12 System.out.println("Age number: " + age30);13 System.out.println("Age + 1 = " + (age30 + 1));14 15 System.out.println("Price string: " + priceInput19.99);16 System.out.println("Price number: " + price19.99);17 System.out.println("With tax: " + (price19.99 * 1.1));18 19 // Also works for other types20 boolean active→ true = Boolean.parseBoolean("true");21 System.out.println("Active: " + activetrue);22 }outputAge string: 30 Age number: 30 Age + 1 = 31 Price string: 19.99 Price number: 19.99 With tax: 21.989 Active: true
ageInput ← 17, priceInput ← 19.99, age ← 17, price ← 19.99, active ← true
1public class Parse {2 public static void main(String[] args) {3 // Simulated user input4 String ageInput→ 17 = "17";5 String priceInput→ 19.99 = "19.99";6 7 // Parse string to number8 int age→ 17 = Integer.parseInt(ageInput17);9 double price→ 19.99 = Double.parseDouble(priceInput19.99);10 11 System.out.println("Age string: " + ageInput17);12 System.out.println("Age number: " + age17);13 System.out.println("Age + 1 = " + (age17 + 1));14 15 System.out.println("Price string: " + priceInput19.99);16 System.out.println("Price number: " + price19.99);17 System.out.println("With tax: " + (price19.99 * 1.1));18 19 // Also works for other types20 boolean active→ true = Boolean.parseBoolean("true");21 System.out.println("Active: " + activetrue);22 }outputAge string: 17 Age number: 17 Age + 1 = 18 Price string: 19.99 Price number: 19.99 With tax: 21.989 Active: true
ageInput ← 25, priceInput ← 9.95, age ← 25, price ← 9.95, active ← true
1public class Parse {2 public static void main(String[] args) {3 // Simulated user input4 String ageInput→ 25 = "25";5 String priceInput→ 9.95 = "9.95";6 7 // Parse string to number8 int age→ 25 = Integer.parseInt(ageInput25);9 double price→ 9.95 = Double.parseDouble(priceInput9.95);10 11 System.out.println("Age string: " + ageInput25);12 System.out.println("Age number: " + age25);13 System.out.println("Age + 1 = " + (age25 + 1));14 15 System.out.println("Price string: " + priceInput9.95);16 System.out.println("Price number: " + price9.95);17 System.out.println("With tax: " + (price9.95 * 1.1));18 19 // Also works for other types20 boolean active→ true = Boolean.parseBoolean("true");21 System.out.println("Active: " + activetrue);22 }outputAge string: 25 Age number: 25 Age + 1 = 26 Price string: 9.95 Price number: 9.95 With tax: 10.945 Active: true
ageInput ← 25, priceInput ← 100.00, age ← 25, price ← 100.0, active ← true
1public class Parse {2 public static void main(String[] args) {3 // Simulated user input4 String ageInput→ 25 = "25";5 String priceInput→ 100.00 = "100.00";6 7 // Parse string to number8 int age→ 25 = Integer.parseInt(ageInput25);9 double price→ 100.0 = Double.parseDouble(priceInput100.00);10 11 System.out.println("Age string: " + ageInput25);12 System.out.println("Age number: " + age25);13 System.out.println("Age + 1 = " + (age25 + 1));14 15 System.out.println("Price string: " + priceInput100.00);16 System.out.println("Price number: " + price100.0);17 System.out.println("With tax: " + (price100.0 * 1.1));18 19 // Also works for other types20 boolean active→ true = Boolean.parseBoolean("true");21 System.out.println("Active: " + activetrue);22 }outputAge string: 25 Age number: 25 Age + 1 = 26 Price string: 100.00 Price number: 100.0 With tax: 110.00000000000001 Active: true
Number to string (format output)
Convert numbers to strings for display or concatenation.
public class ToString {
public static void main(String[] args) {
int count = 42;
double price = 19.99;
boolean active = true;
// Explicit conversion
String countStr = String.valueOf(count);
String priceStr = String.valueOf(price);
String activeStr = String.valueOf(active);
System.out.println("Count as string: '" + countStr + "'");
System.out.println("Price as string: '" + priceStr + "'");
System.out.println("Active as string: '" + activeStr + "'");
// Integer wrapper method
String intStr = Integer.toString(count);
System.out.println("Using Integer.toString: '" + intStr + "'");
// Concatenation converts automatically
String message = "You have " + count + " items";
System.out.println(message);
// Format with specific decimal places
String formatted = String.format("Price: $%.2f", price);
System.out.println(formatted);
}
}
count ← 42, price ← 19.99, active ← true, countStr ← 42, priceStr ← 19.99
1public class ToString {2 public static void main(String[] args) {3 int count→ 42 = 42;4 double price→ 19.99 = 19.99;5 boolean active→ true = true;6 7 // Explicit conversion8 String countStr→ 42 = String.valueOf(count42);9 String priceStr→ 19.99 = String.valueOf(price19.99);10 String activeStr→ true = String.valueOf(activetrue);11 12 System.out.println("Count as string: '" + countStr42 + "'");13 System.out.println("Price as string: '" + priceStr19.99 + "'");14 System.out.println("Active as string: '" + activeStrtrue + "'");15 16 // Integer wrapper method17 String intStr→ 42 = Integer.toString(count42);18 System.out.println("Using Integer.toString: '" + intStr42 + "'");19 20 // Concatenation converts automatically21 String message→ You have 42 items = "You have " + count42 + " items";22 System.out.println(messageYou have 42 items);23 24 // Format with specific decimal places25 String formatted→ Price: $19.99 = String.format("Price: $%.2f", price19.99);26 System.out.println(formattedPrice: $19.99);27 }outputCount as string: '42' Price as string: '19.99' Active as string: 'true' Using Integer.toString: '42' You have 42 items Price: $19.99
Numbers automatically convert when concatenated with strings using +.
Implicit vs explicit conversion
Java automatically converts in some cases, but not others.
public class Implicit {
public static void main(String[] args) {
// Widening: automatic (no data loss possible)
int i = 100;
long l = i; // int → long (automatic)
float f = i; // int → float (automatic)
double d = i; // int → double (automatic)
System.out.println("int to long: " + l);
System.out.println("int to float: " + f);
System.out.println("int to double: " + d);
// Narrowing: requires cast (may lose data)
double bigDouble = 123.456;
// int x = bigDouble; // ERROR: won't compile
int x = (int) bigDouble;
System.out.println("double to int (cast): " + x);
// Mixed expressions widen automatically
int quantity = 3;
double priceEach = 19.99;
double total = quantity * priceEach; // int widened to double
System.out.println("Total: " + total);
}
}
i ← 100, l ← 100, f ← 100.0, d ← 100.0, bigDouble ← 123.456, x ← 123
1public class Implicit {2 public static void main(String[] args) {3 // Widening: automatic (no data loss possible)4 int i→ 100 = 100;5 long l→ 100 = i; // int → long (automatic)6 float f→ 100.0 = i; // int → float (automatic)7 double d→ 100.0 = i; // int → double (automatic)8 9 System.out.println("int to long: " + l100);10 System.out.println("int to float: " + f100.0);11 System.out.println("int to double: " + d100.0);12 13 // Narrowing: requires cast (may lose data)14 double bigDouble→ 123.456 = 123.456;15 // int x = bigDouble; // ERROR: won't compile16 int x→ 123 = (int) bigDouble; //#?explicit_cast17 System.out.println("double to int (cast): " + x123);18 19 // Mixed expressions widen automatically20 int quantity→ 3 = 3;21 double priceEach→ 19.99 = 19.99;22 double total→ 59.97 = quantity3 * priceEach19.99; // int widened to double23 System.out.println("Total: " + total59.97);outputint to long: 100 int to float: 100.0 int to double: 100.0 double to int (cast): 123 Total: 59.97
Exercise: ParseErrors.java
Handle invalid input: what happens with NumberFormatException?