Java tutorial for beginners (2020): #9 (wrapper class, parse, toString)


Welcome back ladies and gentlemen to my one-of-a-kind Java tutorial series. You can call me, bro and I’m here to help. Just remember to believe in yourself and never give up because you can do so much more than what you think you’re capable of. If you’re ready, then let’s get started. Everyone so in this lesson, we’re going to be talking about rapper classes If you remember from a few videos ago, we talked about primitive and reference variable types well a rapper is a construct that contains or raps think of wrapping paper a Primitive data type and converts it into an object There will be instances where we need to convert numbers into strings and then vice versa strings into numbers Currently we can’t perform any mathematical calculations on strings So here’s an example we have string number equals 100 All right, you think this would add 10 to 100 right to become 110 But let’s find out So, let’s print out the number So here we go 110 and oh my goodness what happened So when we added 10 on to the number variable it just tacked it on right at the end instead of increasing the value of the string by 10 So that’s gonna be a problem. So this number 100 is being read as a string a Series of characters instead of an actual number so we can’t perform any math on it right now We would want to convert it to a number first before performing any mathematical calculations and this is how you do that We need to parse the string and convert it to a number Let’s assign a new variable Integer new number And for each primitive data type I have listed here There is a wrapper class and we can call upon that wrapper class to parse The string into its associated primitive data type if we want to convert a string into an integer We would need to use the integer wrapper class and that’s capital. I then lowercase n te ger dot parse int Put whatever you want to parse within the parentheses and I’ll type in number I’m gonna change this to new number as well Now let’s rerun the program There we are 110 Let’s try the opposite where we convert a primitive data type to a string. So give me one moment to set up Will change string to end And we’ll change new number to be a string then so we’re switching them around All right, and to convert the primitive data type of Number that has 100 stored in it to a string type in integer Dot to string and then put in your value You know what now let’s try and add 10 There we are so now the number variable is treated as a string when I add a tenth on to this number it Concatenated the string and just tacked on ten to the end instead of increasing the value of Number by ten to equal 110 and that’s one of the benefits of wrapper classes Ladies and gentlemen now, we’ll conclude this episode of bro code. You did great by making it to the end Keep up the good work if this video helped you out You can help me out by smashing that like button and leave a comment behind subscribe if you’d like to become a fellow, bro

Leave a Reply

Your email address will not be published. Required fields are marked *