38
Characters • The data type char represents a single character in Java. – Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value in Java is really represented as an integer. • Each character has an associated integer value.

Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

Embed Size (px)

Citation preview

Page 1: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

Characters

• The data type char represents a single character in Java.– Character values are written as a symbol:

‘a’, ‘)’, ‘%’, ‘A’, etc.– A char value in Java is really represented

as an integer. • Each character has an associated integer

value.

Page 2: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

Characters– The integer value associated with the character is

based upon a code.• The ASCII code represents 256 characters including all

upper and lower case English letters, the numbers between 0 and 9, punctuation, and some unprintable characters.

• ASCII is a subset of the UNICODE character set.• The UNICODE character set contains 34,168 distinct

characters.– The major languages from the Americas, Europe, Middle

East, Africa, India, Asia, and Pacific are represented.

Page 3: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

Characters

• The value for a particular ASCII character is the sum of the row number and the column number.– What is the integer value of the character:

• ‘C’?

• ‘<‘?

• ‘c’?

– Remember that ‘C’ != ‘c’.

Page 4: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

Characters

• The printable ASCII characters include tabs, new lines, carriage return, single and double quote.– New line = ‘\n’– Tab = ‘\t’– Single quote = ‘\’’– Double quote = ‘\”’

Page 5: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

Non-printable Characters

• There are also characters contained in ASCII that are not printable.– Bell tone = bel– Characters 0 to 32 are non-printable

characters.

Page 6: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

Characters

• To define a character use the char data type.– char firstChar = ‘a’, secondChar = ‘A’;

• To convert an integer into a character you can type cast the integer.– char thirdCharacter = (char) 120;

Page 7: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

Characters

• You can print a char as an integer using type casting.– output.printLine( (int) ‘C’);

• Comparing characters is done based upon their integer representation.– True or false? ‘c’ < ‘C’– True or false? ‘1’ < ‘4’

Page 8: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

Strings• A string is composed of individual characters

that are treated as a single unit. – The individual characters ‘h’, ‘e’, ‘l’, ‘l’, and ‘o’ are

combined into the string “hello”.

• A string may contain letters, digits, and special characters such as +, -, etc.

• String Examples:– “My name is Matilda.”– “1 + 2 = 3”

Page 9: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

Strings

• The data type of a string is String.– The capital ‘S’ of String indicates that this

data type is not a primitive data type.– In fact, String is a complex data type.

• When an individual string is created, it is an object of class String.

Page 10: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

String Input

• Wu provides the method getString with the InputBox class to read strings as input.InputBox inputBox = new InputBox();String name = inputBox.getString(“Please enter you

last name: “);

• A string object may be created simply by assigning an actual string to a String object.

Page 11: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

String Constructors

• Java provides various string constructors.

• Assume String s1;– s1 = new String();

• This creates a string of length zero with no characters.

– s1 = new String(s);• This creates a string that is a copy of the

characters stored in String s that is passed to the constructor.

Page 12: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

Strings

• Each character of a String has an associated index. – The first letter of a string has an index of

zero (0), the second letter has an index of one (1), … the last letter has an index of (string length – 1).

– What is the string length of “hello”?– What is the index of the second ‘l’ in the

word “hello”?

Page 13: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

String Methods

• The length of a string can be found by: – stringName.length();

• The first element of a string is always zero.

• A character at a specific position can be found by:– stringName.charAt(3);

• Where 3 is an index into the string.

Page 14: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

Changing Case• To change the case of characters in a string:

– stringName.replace(‘l’, ‘L’);• This will replace all characters ‘l’ in the string with ‘L’. If

there are no ‘l’s in the string, the original string is returned.

– stringName.toUpperCase();• This will change all lower case letters to capital letters.

– stringName.toLowerCase();• This will change all capital letters to lower case letters.

Page 15: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

String Comparison

• Are to strings equal to one another?– stringName.equals(stringName2);

• The result is true if the two strings are the same and false if the two strings are different.

• Capital and Lower case letters are considered to be different.

– stringName == stringName2;• The result is true only if stringName and

stringName2 both refer to the same object in memory.

Page 16: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

String Comparison• You can ignore the case of letters during

comparison using: – stringName.equalsIgnoreCase(StringName2);

• That means that “hello” is equal to “HELLO”

• You can also compare strings using– stringName.compareTo(StringName2);

• This comparison returns 0 if the strings are equal, a negative number if stringName < stringName2, and a positive number if stringName > stringName2.

Page 17: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

String Comparison

• To compare portions of two strings:– stringName.regionMatches(0, StringName2,

0, 5);• The first parameter 0 is the starting index in

stringName, the third parameter is the starting index in stringName2, and the last argument is the number of characters to compare.

• This method returns true only if the members compared are equal.

– “ello” == “ello” but “ello” != “Ello”

Page 18: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

Locating Characters and Substrings

• indexOf can be used to find characters in strings.– stringName.indexOf((int) ‘a’);

• This returns the index of the first ‘a’ in the string if it is found. If it is not found the result is -1.

– stringName.indexOf((int) ‘a’, 2);• This is similar to the first except the second

parameter specifies which index of the string the search should begin.

Page 19: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

Locating Characters and Substrings

• lastIndexOf can be used to find characters in strings.– stringName.lastIndexOf((int) ‘a’);

• This returns the index of the last ‘a’ in the string if it is found. If it is not found the result is -1.

– stringName.lastIndexOf((int) ‘a’, 2);• This is similar to the first except the second

parameter specifies the highest index of the string where search should begin.

Page 20: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

Extracting Substrings

• Methods to get substrings out of strings are:– stringName.substring(10);

• This returns the string that begins at index 10.

– stringName.substring(10, 15);• This returns the string that begins at index 10

and ends at one index before 15.

Page 21: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

Concatenating Strings

• We have already used string concatenation with:– “this is a string” + stringName

• To concatenate two string variables:– stringName3 =

stringName.concat(stringName2);• This returns the second string added to the end

of the first string.

Page 22: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

Example Program

• String program.

Page 23: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

Other String Methods

• Using the Javadoc documentation you can learn about the many other String methods.– Methods for comparing regions of strings.– Converting variables of other data types to

strings.

Page 24: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

Primitive vs. Complex Data Types

• When you define a primitive data type (int, char, double, bool) the memory location is allocated.– The number of bytes is always the same to

store a value.– char let = ‘A’;

let A

Page 25: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

Primitive vs. Complex Data Types• A complex data type is a data type defined by

a class. – String is an example of a complex data type.– Complex data types usually begin with a capital

letter.– The amount of storage required for a Complex

data type varies depending upon how large the actual values are.

– Complex data types are also called reference data types.

Page 26: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

Primitive vs. Complex Data Types

• When we define a String a memory location is allocated to hold a reference to the actual location of the information.– The reference is the location of the first

item in memory.– The information is stored sequentially

beginning at the reference location.

Page 27: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

Primitive vs. Complex Data Types

String cityName;

cityName = “Rochester”;

cityName 2044 1012…

2044

2048R oc h

e s

t e

r

2052

2056

2060

Page 28: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

Primitive vs. Complex Data Types

• If we define another string and assign it equal to cityName then they will both point to the same location in memory.string cityName2 = cityName;– Now cityName and cityName2 both

point to memory location 1012.

Page 29: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

Passing Objects to methods

• Some of the String methods we have talked about require a String as a parameter to the method.– For example, *.equalsTo(String);– The method definition requires a String

object to be passed to the method equalsTo.

Page 30: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

Passing Objects to methods

– When we pass a String to a method we are passing it using call-by-reference.

• This means that we do not pass the actual string, we are passing the memory location that holds the reference (address) to the actual string.

Page 31: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

Passing Objects to Methods

– A problem associated with call-by-reference is that the original object may be modified.

– All objects (both Java defined and user defined) are passed using call-by-reference.

Page 32: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

Passing Primitive Data to Methods

• If a program passes a variable that has a primitive data type to a method, the actual value is passed using call-by-value.– The advantage is that the original value can

not be modified by the method.– The disadvantage is that a copy of the

original value is made, this requires more memory.

Page 33: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

Example - Objects

String name = “ted”;

tester.myMethod(name);

public void myMethod(String name1) {

name1.toUpper();

}

Page 34: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

Example – Primitive Data

int num = 2;

Tester.myMethod2(num);

public void myMethod2(int num) {

num += 5;

}

Page 35: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

Returning Objects from Methods

• When a method returns an object, a memory reference is really returned. – Not the actual data.

Page 36: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

Returning Primitive Data from Methods

• When a method returns a primitive data type, then the actual value is returned.

Page 37: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

Example - Objects

String output;

output = test.myMethod3();

public String myMethod() {

String month = “April”;

return month;

}

Page 38: Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value

Example – Primitive Data

int output;

output = test.myMethod3();

public int myMethod() {

int year = 2000;

return year;

}