As you already learned, “char” data type is used to store single characters like ‘a’, ‘$’ or ‘1’.
Let us take a look again at what the char data type can and cannot store.
As you learned, the char data type can only store a single character and this calls for a way to be able to store sentences or a set of characters.
String data type helps you do exactly this.
In the previous video, you learnt how to initialise and declare a string variable by using the keyword ‘String’. All the values of this data type should be enclosed within double quotes.
Also, you learnt about string concatenation, which is the combining of two or more strings to create one single string. Here, empty double quotes or spaces after quotes are used to ensure proper spacing in the final string. Eg. The concatenation of ‘Ankit’ and ‘Garg’ to print ‘Ankit Garg’ can done by writing “Ankit” + “ ” + “Garg”.
Let us now try to run the code you learned about.
You can download the following java file which will be very useful to you while watching the video. Right click on the java file and open it with Intellij. Please be careful with the name of the java file. The name of the java file should exactly be same as the class name inside it for the code to run. So make changes accordingly.
Now you know how to store numbers, characters as well as long sentences in Java. In the next segment, you will learn how to store multiple numbers, characters or sentences together in something called an ‘Array’.
Now let us take a look at another data type here in Java called Strings and Characters. So basically you would have learned that Characters is another data type which is used to store single characters in Java. By those single characters I mean alphabets or digits or any special characters as well. So basically for bearing a new character, I can say that care x equals two. Then I can assign a value such as A to it. Note that whenever you are assigning a particular value inside a care variable, you always need to enclose it within single quotes. So you cannot really use double quotes for this. And you must always be careful to use single quotes for declaring any care value. So now let us see whether this is a valid statement. Care y equals to five. So you will see that in this case, this five will not be treated as an integer but will be treated as the character five. So yes, this is a valid statement and also this is a valid statement. Now can you tell me whether this is a valid statement?
Well, we saw that a character data type can only contain a single character inside it. So statements like these are incorrect and the program will throw an error if you try to declare any statement like this. Similarly, if you declare something like this care c equals to say true then also it is going to be an invalid statement.
Characters can store single characters such as alphabets, digits, and special characters.
To assign a value to a character variable, it must be enclosed in single quotes, not double quotes.
The value assigned to a character variable will be treated as a character, even if it's a number.
A character data type can only contain a single character, so statements like "care c = 'hello'" are incorrect and will cause an error.
Assigning a Boolean value to a character variable is also invalid.