When dealing with strings in Java, an important concept to understand is string concatenation. Now that's a really big word, but it just means we're joining character strings together end to end. If this is a string and this is a string, you can combine them by concatenating them to make an even longer string. To concatenate these strings together, we use the plus operator. This is the same addition symbol that we know from math. Just like you can add numbers together, you can concatenate strings together. Let's look at an example. Say I have three different strings, one string literal says I need, another string literal says 2 cups of coffee, and another string literal says on Monday. I can use the plus symbol to concatenate all these strings together. That forms a ginormous string that says I need2 cups of coffeeon Monday. Whenever I see something like this, I imagine the plus symbols are gone, and I imagine the quotes are gone, and I just imagine literally squishing all of these things together. And when I say squished, we're really squishing them together. There's even no extra space in between this string and this string. If you want to add a space here, you would have to explicitly add a space in this string literal at the end of it, or you add a space at the beginning of this string literal. Same with coffeeon Monday. I want a space here, so I'd have to either add it at the end of this string or the beginning of this string. I added a space here and a space here, so when I concatenate all of this together, I squish them together, and the sentence comes out correct like this. There's a space here and a space here. Adding spaces in the right place is a little bit tricky because you have the quotation marks everywhere and the plus symbols, and there's even spaces around the plus symbol. But these spaces around the plus symbol don't contribute to the overall display string. The space must be inside the double quotes. Here's an example of string concatenation in our app. I'm going to change the text so that it says "Amount due " + "$10". I'm concatenating this string literal with this string literal. When I run it on my device, and then I hit the order button, then I see Amount Due $10. You can also concatenate strings with integers like I have here. Before, the ten was in quotes so that was a string representation of the number ten. But here I just have 100 without quotes, so this is the integer value for 100. If I concatenate a string with an integer, then it immediately turns this whole thing into a string. If I hit the Order button, then I see $100 showing up on the screen. In a moment, I'll have you play around with string concatenation to try different values. You could get compile errors, so be careful of those. If I forget a closing quote, I could get an error. In a moment, I'll have you play around with string concatenation and try different values. According to Android code style guidelines, we should have a space before and after each operator. And this string concatenation operator counts as an operator. Now it's your turn to practice in your app. Experiment with combining different strings using the plus operator. You can also combine it with integer literal values as well. Once you feel comfortable with string concatenation, I want you to answer these questions.
Subscribe to:
Post Comments (Atom)
కరోనా కోవిడ్ -19 గురించి ఏ వికీపీడియా మీకు చెప్పలేము?
కరోనా కోవిడ్ -19 గురించి ఏ వికీపీడియా మీకు చెప్పలేము? మిమ్మల్ని మీరు రక్షించుకోండి Your మీ చేతులను తరచుగా కడగాలి Eyes మీ కళ్ళు, న...
-
India is not just a land of snake charmers and holy cows. It's also a land of amazing restaurants and dhabas that guarantee some lip...
No comments:
Post a Comment