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 = ;
int b = ;
// 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 = ;
int b = ;
// 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 = ;
int b = ;
// 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 = ;
int b = ;
// 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 = ;
int b = ;
// 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 = ;
int b = ;
// 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 = ;
int b = ;
// 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 = ;
int b = ;
// 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 = ;
int b = ;
// 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 + "%");
}
}
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 = ;
// 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 = ;
// 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 = ;
// 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);
}
}
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 = ;
String priceInput = ;
// 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 = ;
String priceInput = ;
// 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 = ;
String priceInput = ;
// 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 = ;
String priceInput = ;
// 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 = ;
String priceInput = ;
// 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 = ;
String priceInput = ;
// 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 = ;
String priceInput = ;
// 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 = ;
String priceInput = ;
// 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 = ;
String priceInput = ;
// 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);
}
}
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);
}
}
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);
}
}
Exercise: ParseErrors.java
Handle invalid input: what happens with NumberFormatException?