DOC PREVIEW
FIU COP 2210 - Introduction to the String Class and Java Output

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Computer Programming I Instructor: Greg ShawCOP 2210 Introduction to the String Class and Java OutputStrings, Concatenation, and Escape SequencesI. TerminologyLiteral - a particular value of a given data type (formerly knownas a constant)Character - an uppercase or lowercase letter, digit, or specialcharacter (e.g., a space or a punctuation mark)String - a sequence of charactersString literal - any sequence of characters enclosed in doublequotesRule: a String literal can not include a carriage return/linefeed. I.e., you cannot press [Enter] in the middle of astring and close it on the next lineThe quotes are not part of the string. They are delimiters thattell Java where the string begins and ends.II. The print and println MethodsThe print and println methods each take a single argument and printit (see “Hello.java”)When these methods are called for the PrintStream object System.out,the argument is displayed in a console window (see “Hello.java”)The difference between print and println is this: after printing theargument, println “prints” one additional character - the newlinechar (i.e. a carriage return + line feed) - which moves the cursorto the start of the next line. The print method does not.ExamplesSystem.out.println(“Hi”) ;System.out.println(“Mom”) ;output: HiMom_ (note cursor at start of new line)System.out.print(“To”) ;System.out.print(“get”) ;System.out.println(“her”) ;output: Together_ (note cursor at start of new line)III. String Concatenation“Joining two strings together to form a single string”The concatenation operator is the plus sign (+)The plus sign is a context-dependent operator. When found betweentwo numeric types, it indicates addition. When found between astring and anything else, the “anything else” is automaticallyconverted to a string and concatenated.System.out.println("Here is a string that is too long for " + "one line of code, but not for one line" + " of output!") ;Here is a string that is too long for one line of code, but notfor one line of output!(note the spaces in the string literals)System.out.println("3 + 7 = " + 3 + 7) ;3 + 7 = 37(concatenates the ints (integers) 3 and 7 to the string)System.out.println("3 + 7 = " + (3 + 7) ) ;3 + 7 = 10 (adds first, then concatenates the sum)IV. Escape Sequences“Two characters treated as one”The first char must be the escape character, the backslash “\”The escape char tells Java to “treat the next char differentlythan normal”Some famous escape sequences:\n (newline) - a carriage return and line feed (starts a newline of output)\" (double quote) - to actually print quote marks\t (tab) - “quick and dirty” way to indent the output\\ (backslash) - to actually print a backslashSystem.out.print("Hi\nMom!") ; HiMom!_System.out.print("Hi\tMom!") ; Hi Mom!_System.out.print("\"Drive,\" he said.") ; "Drive," he said._System.out.println("\"\\\\\\\"") ;


View Full Document

FIU COP 2210 - Introduction to the String Class and Java Output

Download Introduction to the String Class and Java Output
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Introduction to the String Class and Java Output and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Introduction to the String Class and Java Output 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?