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

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Java Programming Strings Chapter 7.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Constants and Data Types Constants Data Types Reading for this class: L&L,
CIS 234: Strings. What Is a String? series of characters enclosed in double quotes characters can include letters (lower case and upper case), digits,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Introduction to Computers and Programming Lecture 7:
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Working with the data type: char  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
Fundamental Programming Structures in Java: Strings.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--WuChapter Chapter 8 Characters and Strings.
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.
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
1 Lab Session-III CSIT-120 Spring 2001 Revising Previous session Data input and output While loop Exercise Limits and Bounds GOTO SLIDE 13 Lab session.
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 4: Strings & Arrays Sanjay Goel.
String Escape Sequences
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
CIS 234: Strings (click, scroll down)Strings Dr. Ralph D. Westfall April, 2010.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
Input & Output: Console
Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
String Manipulation Chapter 15 This chapter explains the String facilities. You have already seen some of the main methods of the String class.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Characters. Character Data char data type – Represents one character – char literals indicated with ' '
Vladimir Misic: Java1 Basic Java Syntax The java language will be described by working through its features: –Variable types and expressions.
Introduction to Java Java Translation Program Structure
Java Overview. Comments in a Java Program Comments can be single line comments like C++ Example: //This is a Java Comment Comments can be spread over.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
Coding Bat: Ends in ly Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Road map char data type Reading –Liang 5: Chapter 2: 2.7.4; 2.9; –Liang 6: Chapter 2: 2.7.4; 2.9 –Liang 7: Chapter 2: 2.7.4; 2.9.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
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.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Strings, StringBuilder, and Character
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Arrays in C.
Primitive Types Vs. Reference Types, Strings, Enumerations
Advanced Programming Behnam Hatami Fall 2017.
String Manipulation Chapter 7 Attaway MATLAB 4E.
Strings A string is a sequence of characters that is treated as a single value. Strings are objects. We have been using strings all along. Every time.
Can store many of the same kind of data together
Representation and Manipulation
Java Programming Language
String Processing 1 MIS 3406 Department of MIS Fox School of Business
Introduction to Computer Science
Visual Programming COMP-315
Presentation transcript:

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 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.

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’.

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

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.

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;

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’

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”

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.

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.

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.

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”?

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.

Strings Once you create a string and initialize it you can not change the size of the string. –You can replace individual characters. –You can assign a new string to the string variable. The original string is lost. –You can not add new characters or remove existing characters.

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.

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.

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.

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”

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.

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.

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.

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.

Example Program String program.

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.

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’; letA

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.

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.

Primitive vs. Complex Data Types String cityName; cityName = “Rochester”; cityName … R o c h e s t e r

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.

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.

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.

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.

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.

Example - Objects String name = “ted”; tester.myMethod(name); public void myMethod(String name1) { name1.toUpper(); }

Example – Primitive Data int num = 2; Tester.myMethod2(num); public void myMethod2(int num) { num += 5; }

Returning Objects from Methods When a method returns an object, a memory reference is really returned. –Not the actual data.

Returning Primitive Data from Methods When a method returns a primitive data type, then the actual value is returned.

Example - Objects String output; output = test.myMethod3(); public String myMethod() { String month = “April”; return month; }

Example – Primitive Data int output; output = test.myMethod3(); public int myMethod() { int year = 2000; return year; }