L3 - CSE 114 Computer Science I Strings, I/O, and Methods...

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

View Full Document Right Arrow Icon
CSE 114 – Computer Science I Strings, I/O, and Methods Bonneville Salt Flats, Utah
Background image of page 1

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

View Full DocumentRight Arrow Icon
Java character data char • Type for a single character • Each char is a 2-byte number (Unicode) char symbol = '7'; System.out.println(symbol); System.out.println((int)symbol); • Unicode character set – „0‟ (48) … „9‟ (57) – „A‟ (65) … „Z‟ (90) – „a‟ (97) … „z‟ (122) Output? 7 55
Background image of page 2
String • A predefined class in Java API http://java.sun.com/javase/6/docs/api/java/lang/String.html – A string is a sequence of characters in double quotes • Used for text: <type> <variable_name> = text in double quotes String s1 = "Rudie Can't"; String s2 = s1 + " Fail"; //Concatenation System.out.println(s2); Output? Rudie Can’t Fail
Background image of page 3

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

View Full DocumentRight Arrow Icon
More about Strings Each character is stored at an index String sentence = "Charlie Don't Surf"; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 index C h a r l i e D o n ' t S u r f character The String class (from J2SE) has methods to process strings. System.out.println("charAt(6) is " + sentence.charAt(6)); System.out.println(sentence.toUpperCase()); System.out.println(sentence); System.out.println(sentence.substring(0,7) + sentence.substring(14)); charAt(6) is e CHARLIE DON'T SURF Charlie Don't Surf CharlieSurf Output to Console window by println methods
Background image of page 4
Applying method to a string object • String is a class and not a primitive type • Methods of this class can be applied to process strings in a variety of ways. • sentence is a reference variable, which points to an object storing the value or actual text. sentence.charAt(6) means apply the method charAt( ) to string sentence. 6 is a parameter to method charAt ( ).
Background image of page 5

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

View Full DocumentRight Arrow Icon
Strings are immutable! • There are no methods to change them once they have been created • So how can I change a String? 1. make a new one 2. assign the new one to the old variable String word = "Hello"; word.substring(0,4); System.out.println(word); word = word.substring(0, 4); System.out.println(word); Output? Hello Hell
Background image of page 6
• “+” used for making a new string by concatenating two strings. For example, String s = "If Music "; s = s + "Could Talk"; int mins = 4, secs = 36; String t = s + " (" + mins + ":" + secs + ")"; System.out.println(t); Output? If Music Could Talk (4:36)
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 34

L3 - CSE 114 Computer Science I Strings, I/O, and Methods...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online