{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture06 - COSC 1046 08F J Rajnovich Lecture#6 Monday...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture #06 Page 1 of 10 Sep 22, 2008 COSC 1046 --- 08F --- J. Rajnovich Lecture #6: Monday, September 22, 2008 Topic: Java Strings and Keyboard Input Assigned Readings: Gaddis, Chapter 2.9-2.13 Quiz #1 Wednesday Quiz #1 will be held in the next class, Wednesday, September 24. You are responsible for all material covered in the lectures and assigned readings from the beginning of the course up to and including today. I have posted last year’s Quiz #1 on the course web site so you can see what a quiz for this course looks like. In Wednesday’s class I will lecture for the first 30 minutes and then we will do the quiz. The String Class (Ch. 2.9) In addition to the primitive data types, Gaddis introduces the class String , a non- primitive data type (aka a reference data type) predefined in the Java library java.lang . unlike primitive data types, classes like String are defined to be accompanied by a set of associated methods. methods will be a major topic for us in Chapter 5. Table 2-15 on page 72 displays four of the methods belonging to the String class which we need to know about early in the course. just memorize them for now (for example, for use on Test #1). Declaring String Objects As is the case with primitive data variable, String data variables must be declared before they can be used in a Java program. String name1, name2 ; Each variable can now be assigned a String value. name1 = “Algoma” ; name2 = “University” ; recall that Java is a strongly typed language .
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Lecture #06 Page 2 of 10 Sep 22, 2008 this means that you cannot assign primitive data values to a String variable, or vice versa. Of course, it is possible to declare and initialize a variable in one statement (just as we have seen with primitive data types). String name3 = “Algoma University” ; The following assignment is legitimate: name3 = “2008” ; since “2008” is a string literal, whereas the next one is not: name3 = 2008 ; since 2008 is not a string literal or variable. We can use the string concatenation operator ‘ + ’ to glue string values together. String name4 = name1 + name2 ; This would give name4 the string value AlgomaUniversity”, which is probably not quite what we intended. Here is how we should have initialized it: name4 = name1 + “ ” + name2 ; Be sure you understand the difference between the single quotes (used only for char data) and double quotes (used only for string data). the string concatenation operator ‘ + ’ is strongly typed. it only works on String data (not char data), so the extra space character between the strings above has to be double quoted, not single quoted. It is, of course, true that any string object contains a sequence of zero or more characters (we can have empty strings), but do not confuse chars and Strings in Java. char is a primitive data type String is a reference data type Each has its own operators and rules.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}