Java/C2/Strings/English

From Script | Spoken-Tutorial
Revision as of 15:42, 27 November 2012 by Chandrika (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Title of script: Strings in Java

Author: TalentSprint

Keywords: datatype, char, strings, adding strings, video tutorial


Visual Cue Description
Slide 1

Welcome

Welcome to the spoken tutorial on Strings in Java.
Slide 2

Learning Outcomes

In this tutorial, you will learn how to

create strings, add strings and perform basic string operations like converting to lower case and upper case.

Slide 3

Tools Used

For this tutorial we are using

Ubuntu 11.10,

Eclipse 3.7 and JDK 1.6

Slide 4

Prerequisites

For this tutorial, you should have knowledge of data types in Java.


If not, for relevant tutorial please visit our website as shown

Slide 5

Strings

String in Java, is a sequence of characters.


Before starting with Strings, we will first see the character data type.


Let us now switch to eclipse

Minimize Slides and open Eclipse

Eclipse should contain the following code

public class StringDemo{

public static void main(String[] args){

}

}

we have the skeleton required for the rest of the code.


We have created a class by the name StringDemo and added the main method.

Inside the main method, type

char star = '*';

Inside the main method, type char star equal to in single quotes star


This statement creates a variable with name star and of the type char.


It can store exactly one character.


Let us print a word using a few characters.

Type after the char...


char c1 = 'c';

char c2 = 'a';

char c3 = 'r';

So type,

char c1 equal to c

char c2 equal to a

char c3 equal to r

We have created three characters to make the word car.


Now let us use them to print the word.

Type after char3...


System.out.print(c1);

System.out.print(c2);

System.out.print(c3);


Save and Run.


Point to output

So type,


System.out.print(c1);

System.out.print(c2);

System.out.print(c3);


Please note that I’m using print instead of println so that all the characters are printed on the same line.


save and run the file.


As we can see, the output is as expected.


But this method only prints the word but does not create one.


To create a word, we use the String data type.


Let us try it out.

Remove everything inside the main function and type the following.


String greet = “Hello Learner”;

remove everything inside the main method and type


Type String greet equal to Hello Learner in quotes;


Note that the S in the data type String is in uppercase.


We have use double quotes as delimiters and not single quotes.


This statement creates a variable greet that is of the type String


Let us print the message.

Type the following

System.out.println(greet);

Save and run.


Point to output

So type,

System.out.println(greet);

Save and Run the file.

As we can see, the message has been stored in the variable and printed.


Strings can also be added in Java.


let us see how to do so.

Change “Hello Learner” to “Hello“ I'm removing the Learner from the message.

We'll store the name in a different variable.

Type after the greet... line

String name = “Java”;

String msg = greet + name;


change println(greet) to println(msg)


Save and Run.


Point to output

So type,

String name equal to “Java”;


Now we’ll add the strings to make a message


String msg equal to greet plus name;


change println(greet) to println(msg)


Let us run it.


We can see that the output shows the greeting and the name.

But there is no space separating them.

Before String greet line, type this

char SPACE = ' ';


Change greet + name to greet + SPACE + name


Save and Run.

Point to output

Let us create a space character;


char SPACE equal to ' ';


Note that I have used all uppercase letters in the variable name so that it is clear.


You can change it as you want.


Now add the space to the message.


greet plus SPACE plus name


Let us run the file.


Now the output is clear and as expected.


Let us look at a few basic string operations.

Change “hello” to “HeLLo”

Change “Java” to “jAvA”


Save and Run.


Point to output


I’m changing a few characters of “Hello” to upper case


and a few characters of “java” to uppercase.


Often, when users give input, we have values like this, in mixed case.


Let us run the file to see the output.


The output is not clean.


Let us use the String methods to clean the input.

Add the following code before System...


greet = greet.toLowerCase();

name = name.toUpperCase();


Save and Run.


Point to output

So type,

greet equal to greet.toLowerCase();


This statement converts each character of the string greet to lowercase


Type name equal to name.toUpperCase();


This statement converts each character of the string name to uppercase.


Run the file.


As we can see, the output is now clean after we have used the String methods.

This is how we create strings and perform string operations.


There are many more String methods and

We'll discuss them as we move on to complex topics.

Minimize the Eclipse window and switch to slides.


Slide 6

Summary

We have come to the end of this tutorial.

In this tutorial we have learnt

- how to create strings and add them

- few string operations like converting to lower case and upper case

Slide 7Assignment


For self assessment,

- Read about the concat method of Strings in Java. - Find out how is it different from adding strings.

Slide 8About the Spoken Tutorial Project
  • It summarises the Spoken Tutorial project
  • If you do not have good bandwidth, you can download and watch it


To know more about the Spoken Tutorial project, watch the video available at the following link.


It summarises the Spoken Tutorial Project.

If you do not have good bandwidth, you can download and watch it

Slide 9Spoken Tutorial WorkshopsThe Spoken Tutorial Project Team
  • Conducts workshops using spoken tutorials
  • Gives certificates for those who pass an online test


The Spoken Tutorial Project Team.

Conducts workshops using spoken tutorials.

Gives certificates for those who pass an online test. For more details, please write to contact AT spoken HYPHEN tutorial DOT org.

Slide 10Acknowledgement
  • Spoken Tutorial Project is a part of the Talk to a Teacher project
  • It is supported by the National Mission on Education through ICT, MHRD, Government of India
  • More information on this Mission is available at


Spoken Tutorial Project is a part of the Talk to a Teacher project and is supported by the National Mission on Education through ICT, MHRD, Government of India. More information on this Mission is available at spoken HYPHEN tutorial DOT org SLASH NMEICT HYPHEN Intro
Slide 11About the contributor
  • This tutorial has been contributed by TalentSprint
  • www.talentsprint.com
  • Thanks for joining


This tutorial has been contributed by TalentSprint. Thanks for joining.



Contributors and Content Editors

Ashwini, Chandrika, Sneha