Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A string is a sequence of characters Strings are objects of the String.

Slides:



Advertisements
Similar presentations
Introduction to Programming Overview of Week 2 25 January Ping Brennan
Advertisements

 Base “primitive” types: TypeDescriptionSize intThe integer type, with range -2,147,483, ,147,483,647 4 bytes byteThe type describing a single.
Self Check 1.Which are the most commonly used number types in Java? 2.Suppose x is a double. When does the cast (long) x yield a different result from.
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 4  Fundamental Data Types 1 Chapter 4 Fundamental Data Types.
Datalogi A 3: 26/9. Java Concepts chapter 4 Fundamental Data Types int (long and short) double (and float) boolean char String.
Chapter 4 Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To recognize the limitations of the numeric types To.
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.
Introduction to Python
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
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.
Chapter 2 types, variables, methods Pages Horstmann.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Integer Division What is x? int x = 1729 / 100; Answer: 17.
Chapter 4: Fundamental Data Types. To understand integer and floating-point numbers To recognize the limitations of the numeric types To become aware.
Chapter 4 Numeric types & arithmetic Strings: reading & writing.
Chapter 4 Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To recognize the limitations of the numeric types To.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 4 – Fundamental Data Types.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Four: Fundamental Data Types.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 3 – Fundamental Data Types.
Chapter 4 Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To understand integer and floating-point numbers To recognize.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Primitive Types Java offers a number of primitive types eg.) int, short, long double, float char A variable which is declared as a primitive type stores.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 4 – Fundamental Data Types.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Four: Fundamental Data Types.
Fall 2006Slides adapted from Java Concepts companion slides1 Fundamental Data Types Advanced Programming ICOM 4015 Lecture 4 Reading: Java Concepts Chapter.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. if (amount
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 1 – Introduction ( ปรับปรุง )
Types CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (Chapter 4, Horstmann text)
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. int: integers, no fractional part: 1, -4, 0 double : floating-point.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Four: Fundamental Data Types.
Strings Carol Yarbrough AP Computer Science Instructor Alabama School of Fine Arts.
Chapter 2 Using Objects. Types A type defines a set of values and the operations that can be carried out on the values Examples: 13 has type int "Hello,
 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.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
String and Scanner CS 21a: Introduction to Computing I First Semester,
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Strings and ArrayLists April 23, 2012 ASFA AP CS.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 4 – Fundamental Data Types.
Strings See Chapter 2 u Review constants u Strings, concatenation and repetition 1.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Two: Fundamental Data Types Slides by Evan Gallagher.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Array length = maximum number of elements in array Usually, array is partially filled Need companion variable to keep track of current size Uniform naming.
Chapter 2 print / println String Literals Escape Characters Variables / data types.
1 Data and Expressions Chapter 2 In PowerPoint, click on the speaker icon then the “play” button to hear audio narration.
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 1.
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.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
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,
Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] : double[] data = new double[10]; When array.
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.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Introduction to Computer Science and Object-Oriented Programming
John Woodward A Simple Program – Hello world
Chapter 4 – Fundamental Data Types
String class.
Chapter Goals To understand integer and floating-point numbers
Section 3.2c Strings and Method Signatures
OUTPUT STATEMENTS GC 201.
Chapter 4 – Fundamental Data Types
Wednesday 09/23/13.
Lecture 10 Strings CSE /26/2018.
Nate Brunelle Today: Strings, Type Casting
Use a variable to store a value that you want to use at a later time
Chapter 5 – Decisions Big Java by Cay Horstmann
Each object belongs to a class
Parameter: an input to a method
Presentation transcript:

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A string is a sequence of characters Strings are objects of the String class A string literal is a sequence of characters enclosed in double quotation marks: "Hello, World!" String length is the number of characters in the String Example: "Harry".length() is 5 Empty string: "" 4.5 The String Class

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Use the + operator: String name = "Dave"; String message = "Hello, " + name; // message is "Hello, Dave" If one of the arguments of the + operator is a string, the other is converted to a string String a = "Agent”; int n = 7; String bond = a + n; // bond is "Agent7" Concatenation

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Useful to reduce the number of System.out.print instructions: System.out.print("The total is "); System.out.println(total); versus System.out.println("The total is " + total); Concatenation in Print Statements

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Convert to number: int n = Integer.parseInt(strI); double x = Double.parseDouble(strD); Convert to string: String str = "" + n; str = Integer.toString(n); str = Double.toString(x); Also methods for long and float; Long.toString() and Float.toString(). Converting between Strings and Numbers

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. String greeting = "Hello, World!"; String sub = greeting.substring(0, 5); // sub is "Hello" Supply start and “past the end” position First position is at 0 Substrings

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. String sub2 = greeting.substring(7, 12); // sub2 is "World“ Substring length is “past the end” - start Substrings

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Assuming the String variable s holds the value "Agent", what is the effect of the assignment s = s + s.length() ? Answer: s is set to the string Agent5 Self Check 4.14

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Assuming the String variable river holds the value "Mississippi", what is the value of river.substring(1, 2) ? of river.substring(2, river.length() - 3) ? Answer: The strings "i" and "ssissi" Self Check 4.15

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Reading Exception Reports

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Escape Character \ Suppose you want to print Hello, “World”! System.out.println(“Hello, “World”!”); Use escape character \ (backslash), System.out.println(“Hello, \“World\”!”); To print a backslash use two \\ in a row, System.out.println(“Located in C:\\Temp\\Secret.txt”); Another escape character is \n, which denotes a newline.

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Escape Character \ Escape sequences are useful for including international characters in strings Java uses the Unicode encoding scheme to denote international characters. Uses a 2-byte code which can encode over 65,000 characters How to print which has an accented letter ( ) System.out.printlin(“All the way to San Jose\u00E9!”) Chinese script is not alphabetic but ideographic- a character represents an idea or thing

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. German Keyboard

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Thai Alphabet

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chinese Ideographs