Monday, July 21, 2025

Java Learn

You're asking about **type conversion** in Java - how to convert between different data types like `String`, `int`, `double`, and `float`. Here's a comprehensive guide:

---

### **1. String to int (String → int)**
Use `Integer.parseInt()` or `Integer.valueOf()`:
```java
String str = "123";
int num = Integer.parseInt(str);  // Method 1 (returns primitive int)
int num2 = Integer.valueOf(str);  // Method 2 (returns Integer object)
```

### **2. int to String (int → String)**
Multiple ways:
```java
int num = 123;
String str1 = String.valueOf(num);  // Method 1 (recommended)
String str2 = Integer.toString(num); // Method 2
String str3 = "" + num;             // Method 3 (concatenation trick)
```

---

### **3. String to double (String → double)**
```java
String str = "3.14";
double d = Double.parseDouble(str);  // Method 1 (primitive double)
double d2 = Double.valueOf(str);     // Method 2 (Double object)
```

### **4. double to String (double → String)**
```java
double d = 3.14;
String str1 = String.valueOf(d);   // Method 1
String str2 = Double.toString(d);  // Method 2
String str3 = "" + d;              // Method 3
```

---

### **5. String to float (String → float)**
```java
String str = "3.14f";
float f = Float.parseFloat(str);  // Method 1 (primitive float)
float f2 = Float.valueOf(str);    // Method 2 (Float object)
```

### **6. float to String (float → String)**
```java
float f = 3.14f;
String str1 = String.valueOf(f);  // Method 1
String str2 = Float.toString(f);  // Method 2
```

---

### **7. int to double (int → double)**
```java
int num = 100;
double d = num;  // Implicit conversion (widening)
```

### **8. double to int (double → int)**
```java
double d = 3.99;
int num = (int) d;  // Explicit cast (narrowing) → result: 3 (truncates decimal)
```

---

### **Common Pitfalls & Tips**
1. **NumberFormatException** - Occurs if String isn't a valid number:
   ```java
   try {
       int num = Integer.parseInt("123abc"); // Throws error
   } catch (NumberFormatException e) {
       System.out.println("Invalid number format!");
   }
   ```

2. **Precision Loss** - When converting `double`/`float` to `int`, decimals are truncated.

3. **Use `valueOf()` vs `parseXXX()`**:
   - `parseInt()`/`parseDouble()` → returns primitive type
   - `valueOf()` → returns wrapper class (Integer, Double etc.)

---

### **Quick Reference Table**
| Conversion          | Method                          |
|---------------------|---------------------------------|
| String → int        | `Integer.parseInt(str)`         |
| int → String        | `String.valueOf(num)`           |
| String → double     | `Double.parseDouble(str)`       |
| double → String     | `Double.toString(d)`           |
| String → float      | `Float.parseFloat(str)`         |
| float → String      | `Float.toString(f)`            |
| int → double        | Implicit (`double d = num;`)    |
| double → int        | Explicit cast (`(int) d`)      |

Would you like me to:
1. Show more complex examples combining these conversions?
2. Explain wrapper classes (Integer, Double etc.)?
3. Demonstrate how to handle conversion errors properly?
4. Cover other data type conversions?

No comments: