1 00:00:01 --> 00:00:04 Welcome to the spoken tutorial on Strings in Java. 2 00:00:05 --> 00:00:07 In this tutorial, you will learn how to: 3 00:00:08 --> 00:00:17 create strings, add strings and perform basic string operations like converting to lower case and upper case. 4 00:00:18 --> 00:00:25 For this tutorial, we are using Ubuntu 11.10, JDK 1.6 and Eclipse 3.7 5 00:00:26 --> 00:00:31 To follow this tutorial, you must have the knowledge of data types in Java. 6 00:00:32 --> 00:00:39 If not, for relevant tutorials, please visit our website as shown. 7 00:00:40 --> 00:00:43 String in Java is a sequence of characters. 8 00:00:44 --> 00:00:49 Before starting with Strings, we will first see the character data type. 9 00:00:50 --> 00:00:54 Let us now switch to eclipse. 10 00:00:55 --> 00:00:59 We have the 'Eclipse IDE' and the skeleton required for the rest of the code. 11 00:01:00 --> 00:01:06 We have created a class StringDemo and added the main method. 12 00:01:07 --> 00:01:18 Inside the main method, type: char star equal to in single quotes asterisk. 13 00:01:19 --> 00:01:24 This statement creates a variable with the name star and of the type char. 14 00:01:25 --> 00:01:27 It can store exactly one character. 15 00:01:28 --> 00:01:32 Let us print the word using a few characters. 16 00:01:33 --> 00:01:35 Remove the char line and type: 17 00:01:36 --> 00:01:42 char c1 equal to in single quotes 'c'; 18 00:01:43 --> 00:01:48 char c2 equal to in single quotes 'a'; 19 00:01:49 --> 00:01:54 char c3 equal to in single quotes 'r'; 20 00:01:55 --> 00:01:58 We have created three characters to make the word car. 21 00:01:59 --> 00:02:01 Let us use them to print the word. 22 00:02:02 --> 00:02:03 Type: 23 00:02:04 --> 00:02:11 System.out.print(c1); 24 00:02:12 --> 00:02:21 System.out.print(c2); 25 00:02:22 --> 00:02:30 System.out.print(c3); 26 00:02:31 --> 00:02:38 Please note that I’m using print instead of println so that all the characters are printed on the same line. 27 00:02:39 --> 00:02:42 Save the file and run it. 28 00:02:43 --> 00:02:45 As we can see, the output is as expected. 29 00:02:46 --> 00:02:49 But this method only prints the word but does not create one. 30 00:02:50 --> 00:02:53 To create a word, we use the String data type. 31 00:02:54 --> 00:02:56 Let us try it out. 32 00:02:57 --> 00:03:02 Remove everything inside the main method and type: 33 00:03:03 --> 00:03:15 String greet equal to "Hello Learner"; 34 00:03:16 --> 00:03:18 Note that 'S' in the word String is in uppercase. 35 00:03:19 --> 00:03:24 And we are using double quotes instead of single quotes as delimiters. 36 00:03:25 --> 00:03:30 This statement creates a variable greet that is of the type String. 37 00:03:31 --> 00:03:32 Now Let us print the message. 38 00:03:33 --> 00:03:43 System.out.println(greet); 39 00:03:44 --> 00:03:50 Save the file and run it. 40 00:03:51 --> 00:03:56 As we can see, the message has been stored in the variable and it has been printed. 41 00:03:57 --> 00:03:59 Strings can also be added in Java. 42 00:04:00 --> 00:04:03 Let us see how to do so. 43 00:04:04 --> 00:04:07 I'm removing the Learner from the message. 44 00:04:08 --> 00:04:13 We'll store the name in a different variable. 45 00:04:14 --> 00:04:21 String name equal to “Java”; 46 00:04:22 --> 00:04:27 Now, we’ll add the strings to make a message. 47 00:04:28 --> 00:04:41 String msg equal to greet plus name; 48 00:04:42 --> 00:04:55 change the 'greet' in the print statement (println(greet)) to 'message' (println(msg)) save the file and run it. 49 00:04:56 --> 00:04:59 We can see that the output shows the greeting and the name. 50 00:05:00 --> 00:05:01 But there is no 'space' separating them. 51 00:05:02 --> 00:05:07 So, let us create a space character. 52 00:05:08 --> 00:05:16 char SPACE equal to in single quotes ' '(space); 53 00:05:17 --> 00:05:22 Note that I have used all uppercase letters in the variable name so that it is clear. 54 00:05:23 --> 00:05:25 You can change it as you want. 55 00:05:26 --> 00:05:28 Now, let us add the SPACE to the message. 56 00:05:29 --> 00:05:35 greet plus SPACE plus name; 57 00:05:36 --> 00:05:39 save the file and run it. 58 00:05:40 --> 00:05:44 Now we can see the output is clear and as expected. 59 00:05:45 --> 00:05:49 Let us look at a few string operations. 60 00:05:50 --> 00:06:04 I’m changing a few characters of the word “Hello” to upper case and of the word “java” to uppercase. 61 00:06:05 --> 00:06:10 Often, when users give input, we have values like this, in mixed case. 62 00:06:11 --> 00:06:17 So, Let us run the file and see the output. 63 00:06:18 --> 00:06:21 As we can see, the output is not clean. 64 00:06:22 --> 00:06:26 So let us use the String methods to clean the output. 65 00:06:27 --> 00:06:40 Type: greet equal to greet.toLowerCase(); 66 00:06:41 --> 00:06:46 This statement converts each character of the string greet to lowercase. 67 00:06:47 --> 00:06:57 name equal to name.toUpperCase(); 68 00:06:58 --> 00:07:02 This statement converts each character of the string name to uppercase. 69 00:07:03 --> 00:07:07 Save the file and Run it. 70 00:07:08 --> 00:07:12 As we can see, the output is now clean after we have used the String methods. 71 00:07:13 --> 00:07:17 This is how we create strings and perform string operations. 72 00:07:18 --> 00:07:18 There are more String methods and 73 00:07:19 --> 00:07:25 we'll discuss them as we move on to complex topics. 74 00:07:26 --> 00:07:28 This brings us to the end of this tutorial. 75 00:07:29 --> 00:07:30 In this tutorial, we have learnt: 76 00:07:31 --> 00:07:32 how to create strings, add strings 77 00:07:33 --> 00:07:38 and perform string operations like converting to lower case and upper case. 78 00:07:39 --> 00:07:40 As an assignment for this tutorial: 79 00:07:41 --> 00:07:49 Read about the concat method of Strings in Java. Find out how is it different from adding strings. 80 00:07:50 --> 00:07:54 To know more about the Spoken Tutorial project, watch the video available at the following link. 81 00:07:55 --> 00:07:57 It summarizes the Spoken Tutorial project. 82 00:07:58 --> 00:08:02 If you do not have good bandwidth, you can download and watch it. 83 00:08:03 --> 00:08:04 The Spoken Tutorial Project Team: 84 00:08:05 --> 00:08:06 Conducts workshops using Spoken Tutorials. 85 00:08:07 --> 00:08:16 Gives certificates to those who pass an online test. For more details, please write to contact AT spoken HYPHEN tutorial DOT org. 86 00:08:17 --> 00:08:20 Spoken Tutorial Project is a part of the 'Talk to a Teacher' project. 87 00:08:21 --> 00:08:27 It is supported by the National Mission on Education through ICT, MHRD, Government of India. 88 00:08:28 --> 00:08:32 More information on this mission is available at the following link: spoken HYPHEN tutorial DOT org SLASH NMEICT HYPHEN Intro. 89 00:08:33 --> 00:08:38 This tutorial has been contributed by TalentSprint. Thanks for joining.