17-Mar-16 Characters and Strings. 2 Characters In Java, a char is a primitive type that can hold one single character A character can be: A letter or.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

Constants and Data Types Constants Data Types Reading for this class: L&L,
Types and Variables. Computer Programming 2 C++ in one page!
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Introduction to Computers and Programming Lecture 7:
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
ECE122 L2: Program Development February 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 2 Program Development.
CMT Programming Software Applications
Working with the data type: char  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
The Class String. String Constants and Variables  There is no primitive type for strings in Java.  There is a class called String that can be used to.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Characters and Strings. Characters In Java, a char is a primitive type that can hold one single character A character can be: –A letter or digit –A punctuation.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.3 The Class String.
1 Character Strings and Variables Character Strings Variables, Initialization, and Assignment Reading for this class: L&L,
String Escape Sequences
CIS 234: Character Codes Dr. Ralph D. Westfall April, 2011.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
L EC. 02: D ATA T YPES AND O PERATORS (1/2) Fall Java Programming.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
 2005 Pearson Education, Inc. All rights reserved Formatted Output.
System development with Java Lecture 2. Rina Errors A program can have three types of errors: Syntax and semantic errors – called.
C-Language Keywords(C99)
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Constants in C A Presentation On Department of Computer & Information Technology, M.S.P.V.L. Polytechnic College, Pavoorchatram.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
Program Statements Primitive Data Types and Strings.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
 Character set is a set of valid characters that a language can recognise.  A character represents any letter, digit or any other sign  Java uses the.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Primitive Data Types. Identifiers What word does it sound like?
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Review Spatial Filters – Smooth – Blur – Low Pass Filter – Sharpen – High Pass Filter – Edge detection – Erosion – Dilation Other Pixel Filters – Thresholding.
The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory.
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
Chapter 2 Variables.
Copyright © Curt Hill Regular Expressions Providing a Search Pattern.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
3 - 1 Text Processing In Java: Characters and Strings Reading:Downey: Chapter 7 Problem Set:Assignment #1 due Tuesday, Feburary 13 Wellesley College CS230.
Chapter 2 print / println String Literals Escape Characters Variables / data types.
17-Feb-16 String and StringBuilder Part I: String.
1 Data and Expressions Chapter 2 In PowerPoint, click on the speaker icon then the “play” button to hear audio narration.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Characters and Strings
CSCI 1100/1202 January 14, Abstraction An abstraction hides (or ignores) the right details at the right time An object is abstract in that we don't.
Chapter 2 1.What is the difference between print / println 2.What are String Literals 3.What are the Escape Characters for backslash, double quotataions,
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
28 Formatted Output.
Chapter 2 Variables.
ECE Application Programming
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Variables and Primative Types
Representing Information as bit patterns
Object Oriented Programming
Escape Sequences Some Java escape sequences: See Roses.java (page 68)
Chapter 2 Create a Chapter 2 Workspace Create a Project called Notes
String methods 26-Apr-19.
Instructor: Alexander Stoytchev
Literal data.
ASCII and Unicode.
Presentation transcript:

17-Mar-16 Characters and Strings

2 Characters In Java, a char is a primitive type that can hold one single character A character can be: A letter or digit A punctuation mark A space, tab, newline, or other whitespace A control character Control characters are holdovers from the days of teletypes—they are things like backspace, bell, end of transmission, etc.

3 char literals A char literal is written between single quotes (also known as apostrophes): 'a' 'A' '5' '?' ' ' Some characters cannot be typed directly and must be written as an “escape sequence”: Tab is '\t' Newline is '\n' Some characters must be escaped to prevent ambiguity: Single quote is '\'' (quote-backslash-quote-quote) Backslash is '\\'

4 Additional character literals \n newline \t tab \b backspace \r return \f form feed \\ backslash \' single quote \" double quote

5 Character encodings A character is represented as a pattern of bits The number of characters that can be represented depends on the number of bits used For a long time, ASCII (American Standard Code for Information Interchange) has been used ASCII is a seven-bit code (allows 128 characters) ASCII is barely enough for English Omits many useful characters: ¢     ½ ç “ ”

6 Unicode Unicode is a new standard for character encoding that is designed to replace ASCII “Unicode provides a unique number for every character, no matter what the platform, no matter what the program, no matter what the language.” Java uses a subset of Unicode to represent characters The Java subset uses two bytes for every character Java 1.5 expands this by allowing some three-byte characters Except for having these extra characters available, it seldom makes any difference to how you program

7 Unicode character literals The rest of the ASCII characters can be written as octal numbers from \0 to \377 Any Unicode character (in the Java subset) can be written as a hexadecimal number between \u0000 and \uFFFF Since there are over possible Unicode characters, the list occupies an entire book This makes it hard to look up characters Unicode “letters” in any alphabet can be used in identifiers

8 Glyphs and fonts A glyph is the printed representation of a character For example, the letter ‘A’ can be represented by any of the glyphs A A A A  A font is a collection of glyphs Unicode describes characters, not glyphs

9 Strings A String is a kind of object, and obeys all the rules for objects In addition, there is extra syntax for string literals and string concatenation A string is made up of zero or more characters The string containing zero characters is called the empty string

10 String literals A string literal consists of zero or more characters enclosed in double quotes "" "Hello" "This is a String literal." To put a double quote character inside a string, it must be backslashed: "\"Wait,\" he said, \"Don't go!\"" Inside a string, a single quote character does not need to be backslashed (but it can be)

11 String concatenation Strings can be concatenated (put together) with the + operator "Hello, " + name + "!" Anything “added” to a String is converted to a string and concatenated Concatenation is done left to right: "abc" gives "abc35" "abc" gives "8abc" 3 + (5 + "abc") gives "35abc"

12 Newlines The character '\n' represents a “newline” (actually, it’s an LF, the linefeed character) When “printing” to the screen, you can go to a new line by printing a newline character You can also go to a new line by using System.out.println with no argument or with one argument When writing to the internet, you should use "\r\n" instead of println because println is platform-specific On UNIX, println uses LF for a newline On Macintosh, println uses CR instead of LF for a newline On Windows, println uses CR - LF for a newline When you use the character constants, you are in control of what is actually output

13 System.out.print and println System.out.println can be called with no arguments (parameters), or with one argument System.out.print is called with one argument The argument may be any of the 8 primitive types The argument may be any object Java can print any object, but it doesn’t always do a good job Java does a good job printing Strings Java typically does a poor job printing types you define

14 Printing your objects In any class, you can define the following instance method: public String toString() {... } This method can return any string you choose If you have an instance x, you can get its string representation by calling x.toString() If you define your toString() method exactly as above, it will be used whenever your object is converted to a String This happens during concatenation: "My object is " + myObject toString() is also used by System.out.print and System.out.println

15 Constructing a String You can construct a string by writing it as a literal: "This is special syntax to construct a String." Since a string is an object, you could construct it with new : new String("This also constructs a String.") But using new for constructing a string is foolish, because you have to write the string as a literal to pass it in to the constructor You’re doing the same work twice!

16 String methods This is only a sampling of string methods All are called as: myString.method(params) length() -- the number of characters in the String charAt(index) -- the character at (integer) position index, where index is between 0 and length-1 equals( anotherString ) -- equality test (because == doesn’t do quite what you expect Hint: Use "expected".equals( actual ) rather than actual.equals("expected") to avoid NullPointerException s Don’t learn all 48 String methods unless you use them a lot—instead, learn to use the API!

17 Vocabulary escape sequence -- a code sequence for a character, beginning with a backslash ASCII -- an 7-bit standard for encoding characters Unicode -- a 16-bit standard for encoding characters glyph -- the printed representation of a character font -- a collection of glyphs empty string -- a string containing no characters concatenate -- to join strings together

18 The End