0:07 [Music]
0:10 Welcome back to CodeChef. Imagine you
0:11 have different bags for your school
0:14 books. A bigger bag can easily carry a
0:15 smaller book, but trying to squeeze a
0:17 big book into a tiny bag will cut off
0:20 part of it. That's exactly what happens
0:21 in Java when we move values between
0:24 different data types. It's called type
0:27 casting. Think of type casting like bags
0:30 and books. A small book always fits into
0:33 a bigger bag, safe and automatic. But a
0:35 large book doesn't fit into a smaller
0:37 bag unless you trim its pages, and that
0:39 means losing part of it. Java works the
0:42 same way. Sometimes conversions are
0:44 automatic, and sometimes you need to do
0:47 them manually. In Java, type casting
0:49 means converting one data type into
0:52 another. There are two types. Implicit
0:54 casting where Java automatically
0:56 converts smaller to larger types like
1:00 int to double and explicit casting where
1:02 you manually convert larger to smaller
1:04 types like double to int using parenthesis.
1:06 parenthesis.
1:09 Here's a quick example. int num equals
1:12 10. Double result equals num. That's
1:17 implicit casting. Then double pi= 3.14.
1:22 int whole equals in parenthesis int pi.
1:24 That's explicit casting. In the first
1:28 case, Java safely converts 10 into 10.0.
1:30 In the second case, 3.14 becomes three
1:33 and the decimal part is cut off. A few
1:36 quick tips. Implicit casting always goes
1:39 from smaller to larger types. Bite to
1:45 Explicit casting is required when going
1:48 backward. And remember, explicit casting
1:50 can cause data loss, just like trimming
1:53 a book to fit into a smaller bag. Now
1:56 it's your turn. Try the beginnerfriendly
1:58 code chef problem called type casting
2:00 basics. Practice converting between
2:03 integer, double, and character to
2:05 strengthen your understanding.
2:06 Today you learned that type casting
2:09 converts one type into another. Implicit
2:12 casting is automatic and safe. Explicit
2:14 casting requires parentheses and may
2:16 lose data. [Music]