Navigate Select ESC Close

#8 Type Conversion in Java

2023-01-16 Science & Technology
552.5k
6.2k
116
Telusko
Telusko
2.8m subscribers

Unlock all features

FREE: Get instant access to 10 AI summaries, chats, or transcripts per day.

Description

Check out our courses: Industry-Ready Java Spring Boot, React & Gen AI -Live: https://go.telusko.com/industry-ready-course Coupon: TELUSKO10 (10% Discount) AI-Powered DevOps with AWS- Live V3: https://go.telusko.com/ai-powered-devops-with-AWS-v3 Coupon: TELUSKO10 (10% Discount) Master Java Spring Development : https://go.telusko.com/masterjava Coupon: TELUSKO20 (20% Discount) For More Queries WhatsApp or Call on : +919008963671 website : https://courses.telusko.com/ Java Spring:- https://go.telusko.com/Udemyjavaspring Java:- https://go.telusko.com/udemyteluskojava Spring: https://go.telusko.com/udemyteluskospring Java For Programmers:- https://go.telusko.com/javaProgrammers Python : https://go.telusko.com/udemyteluskopython Git : https://go.telusko.com/udemyteluskogit Docker : https://go.telusko.com/udemyteluskodocker Instagram : https://www.instagram.com/navinreddyofficial/ Linkedin : https://in.linkedin.com/in/navinreddy20 WhatsApp : https://go.telusko.com/whatsapp TELUSKO Android App : https://go.telusko.com/TELUSKOAPP TELUSKO IOS App : https://apple.co/3SsgmU2 Discord : https://discord.gg/D8hWe9BqfF In this lecture we are discussing: 1)What is type conversion or type casting ? 2)Different ways to casting? a)implicit type casting or automatic type casting b)explicit type casting 3)What is effect of implicit and explicit type casting? a) Narrowing conversion 4)Type promotion when we do operation #1 What is type conversion or type casting ? -- type conversion or type casting is the process of converting a value from one data type to another data type. e.g int num=5; long l=num; #2 Different ways to casting a) Implicit type casting :- It is way to in which compiler automatically convert smaller size data type in larger. e.g int num=4; long l=num; //now num value converted to long b) Explicit type casting :- manually when programmer cast one data type into other is known as explicit type casting. e.g float fl=4.5f; int num=fl; -- num value become 4; syntax for conversion: type1 x=value; //higher size type2 y=(datatype of type2)x; #3 What is effect of type casting ? -- one effect is narrowing conversion i.e Narrowing conversions can be done from a larger data type to a smaller data type, but they can result in loss of precision or data. e.g float fl=5.6f; int num=fl; loss of 0.6 precision now value of num is 5. Note: if you want convert -- you get error . e.g int i=5; byte b=i; //give error --in most cases conversion of higher datatype to lower data type give error 1. incompatible types: possible lossy conversion from long to byte 2. incompatible types: possible lossy conversion from double to int 3. incompatible types: possible lossy conversion from float to int 4. incompatible types: possible lossy conversion from double to byte 5. incompatible types: possible lossy conversion from float to byte 6. incompatible types: possible lossy conversion from double to short 7. incompatible types: possible lossy conversion from float to short 8. incompatible types: possible lossy conversion from double to long 9. incompatible types: possible lossy conversion from float to long 10. incompatible types: possible lossy conversion from double to char -- these are some cases #4 Type promotion :- when we do arithmetic operation on two different data types, java will promote the smaller data type to the larger data type int / int = int int / float = float int * float = float short * short = int short * int = int short * long = long byte * byte = int System.out.println(5.2/3); byte b=120; byte c=120; System.out.println(a*b); //14400 Github repo : https://github.com/navinreddy20/Javacode.git More Learning : Java :- https://bit.ly/3x6rr0N Python :- https://bit.ly/3GRc7JX Django :- https://bit.ly/3MmoJK6 JavaScript :- https://bit.ly/3tiAlHo Node JS :- https://bit.ly/3GT4liq Rest Api :-https://bit.ly/3MjhZwt Servlet :- https://bit.ly/3Q7eA7k Spring Framework :- https://bit.ly/3xi7buh Design Patterns in Java :- https://bit.ly/3MocXiq Docker :- https://bit.ly/3xjWzLA Blockchain Tutorial :- https://bit.ly/3NSbOkc Corda Tutorial:- https://bit.ly/3thbUKa Hyperledger Fabric :- https://bit.ly/38RZCRB NoSQL Tutorial :- https://bit.ly/3aJpRuc Mysql Tutorial :- https://bit.ly/3thpr4L Data Structures using Java :- https://bit.ly/3MuJa7S Git Tutorial :- https://bit.ly/3NXyCPu Donation: PayPal Id : navinreddy20 https://www.telusko.com

Top Comments (10)

@abhi-ku-sh-l9x 2024-05-10

No one on youtube can match this guys level in Java

63
@technoflask03 2025-05-16

Great and helpful video. One thing I want to mention is, while casting from bigger type to smaller type, like int -> byte. the modulo is just one part of the process. lets say, the int value is 245, and we want to store it in byte. so the range of byte is -128 to 127. So the non negative range is, 0 to 127, i.e. 128 values. So what java does is, 245 % 128 = 117. after that it wraps around to the negative side of values, so it will start from -128, and go 117 values. -128 + 117 = -11. And that will be the output if you try to store 245 in a "byte" datatype. This process is called 2's complement wrapping, and is done in case value overflows the variable. Which is true in case of 245 storing in byte.

21
@C_o_d_e_Help 2023-11-17

U & code with Harry are amazing teachers 😅😅

43
@sasikiran759 2024-07-14

Give definitions about every topic it will be helpful to people to crack the interview pls try if possible?

10
@rajansharma9066 2024-05-03

you really teach good, the concepts, which we need as a engineer just not syntax of any programming language

10
@Sakshi_Umbarkar 2024-07-11

Your teaching skill is amazing..😊

5
@nikhilinamdar7342 2024-07-29

Awesome, the concepts explained are concise and to the point

1
@Harshalzzzz 2023-10-24

Great explanation👍

9
@Manna-Yuvatha 2024-12-22

Sir does this playlist cover all core java concepts..because your explanation was awesome.....

0
@shubham04jha 2025-07-29

remember that type promotion like the one mentioned at the end only occurs when both the operands are either byte char or short. they get converted to int before operation. bit the same does not hold for: int a = 100_000; long b = a*a; wont store 100_000_00_000;

0

Unlock the Data Inside
Turn Videos into Knowledge

  • Get FREE 10/day: transcripts, summaries, chats
  • Chat with videos, export text & PDF
  • $1 free API credit for RAG, chatbots & research

Free forever plan • All features unlocked

App screenshot