ตั้งค่าการอ่าน

ค่าเริ่มต้น

  • เลื่อนอัตโนมัติ
    Java

    ลำดับตอนที่ #3 : Java I - Type Conversion

    • อัปเดตล่าสุด 27 ธ.ค. 61


    Implicit Type Conversion

    Type Conversion

              We learned about concatenating Strings, but can we concatenate an integer and a string? You can, using type conversion. In Java, types can be converted both implicitly and explicitly. In the example, 23 is converted to "23". This is because int gets converted to String implicitly when you add them together.

    Rules of Calculation

              Before learning about implicit type conversion between an int and a double, there's one important rule to be careful of. Calculations between two identical types will return the same type. Therefore, 5 / 2 returns 2, not 2.5.

    Int and Double Calculations

              What happens when we do calculations with an int and a double? In this case, the int will be implicitly converted to a double, and the result will be a double. So, both 5 / 2.0 and 5.0 / 2 will be 2.5. Since integers can be written as decimals (like 5 and 5.0), Java automatically converts them.


    Explicit Type Conversion

    Calculating int Type Numbers

              We've learned that 5 / 2, a calculation of integers, results in 2.  What should we do if we want to find a more accurate value when dividing an integer by another integer?

    Cast

              Explicit type conversion, also known as casting, solves that for us. You can convert an integer to a double by casting it, like in the image below ( you only have to cast one of the integers to a double, as we learned in the previous lesson).

    Final Project

              It's time for the final project. You'll create a program that prints your name, age, height, weight, and BMI. Let's give it a try!





    ติดตามเรื่องนี้
    เก็บเข้าคอลเล็กชัน

    ผู้อ่านนิยมอ่านต่อ ดูทั้งหมด

    loading
    กำลังโหลด...

    อีบุ๊ก ดูทั้งหมด

    loading
    กำลังโหลด...

    ความคิดเห็น

    ×