Other primitives and Strings
1. Other primitives and Strings
Now, let's continue with the other two primitive types!2. Primitives continued
We've covered all the six numerical primitives in the previous video, but numbers aren't the only data we can use.3. booleans
In programming, we often need to check if a condition is true, and for that, we need `true` and `false` values.4. booleans - how they work?
For example, if we need to check if an item is on sale, we can create a variable with a boolean value of true or false, representing whether the item is on sale or not.5. booleans - naming convention
Our variables should have clear, meaningful names, making them easy to read and understand. In the case of boolean variables, we typically start with “is”, such as isOnSale, to demonstrate that this variable represents a boolean.6. char
If we want to store a single character, such as A or $, we use the char primitive. Characters are wrapped in single quotes, and we can easily define them with an equal sign.7. char versus String
Now, remember that Strings are wrapped in double quotes. Why is that? It's because Strings are not primitive data types, they are actually a collection of characters. We use Strings to store text, as we've already seen in the previous chapter! Let's now look at Strings in more detail.8. Strings
Strings are a collection of characters, any characters, inside of double quotes. Even a single character, in double quotes, is a String. Remember, a single character in single quotes is a primitive char. Remember, Strings are not primitives, but their own type. This comes with an advantage: Java provides built-in functionality for manipulating Strings. Think of primitives like ingredients, a String in contrast is a prepared meal that comes with everything we need. We can use it without having to prepare it from scratch. We’ll explore some of these built-in conveniences in the next video.9. Storing Strings
To store text in a variable, we use the String type, followed by the variable name, an equals sign, and the text inside double quotes. For example, if we want to store a name, we can declare a variable called myName and assign it "Jim White". Strings always need to be inside double quotes. If we forget them, Java won't recognize them as text.10. Summary of primitives
Here is a list of all the primitive types.11. Summary of Strings
And a summary of what we’ve learned about Strings so far.12. Let's practice!
Are you ready to practice?Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.