Primitive Types Vs. Reference Types, Strings, Enumerations

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

ACM/JETT Workshop - August 4-5, Classes, Objects, Equality and Cloning.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Strings in Java 1. strings in java are handled by two classes String &
1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings.
Java Programming Strings Chapter 7.
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Constants and Data Types Constants Data Types Reading for this class: L&L,
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
Fundamental Programming Structures in Java: Strings.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
 Pearson Education, Inc. All rights reserved Strings, Characters and Regular Expressions.
JavaScript, Third Edition
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Introduction to Java Java Translation Program Structure
SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
Chapter 7: Characters, Strings, and the StringBuilder.
CHAPTER 9 Text Processing and More about Wrapper Classes Copyright © 2016 Pearson Education, Inc., Hoboken NJ.
Jaeki Song JAVA Lecture 07 String. Jaeki Song JAVA Outline String class String comparisons String conversions StringBuffer class StringTokenizer class.
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Java’s String Class.
String String Builder. System.String string is the alias for System.String A string is an object of class string in the System namespace representing.
1 Java Strings Dr. Randy M. Kaplan. 2 Strings – 1 Characters are a fundamental data type in Java It is common to assemble characters into units called.
Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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.
Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
The Methods and What You Need to Know for the AP Exam
© 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, Characters and Regular Expressions
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
JavaScript Objects.
Chapter 20 Generic Classes and Methods
String String Builder.
Advanced Programming Behnam Hatami Fall 2017.
String Objects & its Methods
Modern Programming Tools And Techniques-I Lecture 11: String Handling
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
Chapter 3 Introduction to Classes, Objects Methods and Strings
Part a: Fundamentals & Class String
Chapter 2: Java Fundamentals
Java – String Handling.
16 Strings.
Lecture 07 String Jaeki Song.
CS2011 Introduction to Programming I Strings
Object Oriented Programming in java
Strings in Java.
Dr. Sampath Jayarathna Cal Poly Pomona
Visual Programming COMP-315
Chapter 3 Introduction to Classes, Objects Methods and Strings
Switch, Strings, and ArrayLists in Java
In Java, strings are objects that belong to class java.lang.String .
Presentation transcript:

Primitive Types Vs. Reference Types, Strings, Enumerations Java How to Program, 10/e Late Objects Version © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

References & Reading The content is mainly selected (sometimes modified) from the original slides provided by the authors of the textbook Readings Chapter 6: Arrays and ArrayLists Chapter 14: Strings, Characters and Regular Expressions Introduction to Enumerations (CMSC 202, University of Maryland, Baltimore County) https://www.csee.umbc.edu/courses/undergraduate/202/spring13/le ctures/enums.pdf ©1992-2015 by Pearson Education, Inc. All Rights Reserved.

Outline 6.2 Primitive Types vs. Reference Types -.- Introduction to Objects 14.1  Introduction 14.2  Fundamentals of Characters and Strings 14.3  Class String 14.3.1 String Constructors 14.3.2 String Methods length, charAt and getChars 14.3.3 Comparing Strings 14.3.4 Locating Characters and Substrings in Strings ©1992-2015 by Pearson Education, Inc. All Rights Reserved.

Outline 14.3.5 Extracting Substrings from Strings 14.3.6 Concatenating Strings 14.3.7 Miscellaneous String Methods -.- Introduction to Enumerations (from CMSC202) ©1992-2015 by Pearson Education, Inc. All Rights Reserved.

6.2 Primitive Types vs. Reference Types Java’s types are divided into primitive types and reference types. Primitive types: boolean, byte, char, short, int, long, float and double. Appendix D lists the eight primitive types in Java. All nonprimitive types are reference types. ©1992-2015 by Pearson Education, Inc. All Rights Reserved.

6.2 Primitive Types vs. Reference Types (cont.) A primitive-type variable can hold exactly one value of its declared type at a time. Programs use variables of reference types (normally called references) to store the addresses of objects in the computer’s memory. Such a variable is said to refer to an object in the program. To call an object’s methods, you need a reference to the object. If an object’s method requires additional data to perform its task, then you’d pass arguments in the method call. Primitive-type variables do not refer to objects, so such variables cannot be used to invoke methods. ©1992-2015 by Pearson Education, Inc. All Rights Reserved.

-.- Introduction to Objects Class: is reference type String, System, Scanner, Math, SecureRandom Object: is a reference or object (variable of type class) String s; Scanner input; SecureRandom sr; Class Method: called using the class name Math.pow(x,y); Object Methods: called using the object input.nextInt(); Static fields: class field (belong to the class, common to all objects) Fields: object filed (specific to a particular object) © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

14.1  Introduction The foundation for string and character manipulation in Java is provided by three classes class String, class StringBuilder and class Character from the java.lang package. We’ll only study the usage of the class String © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

14.2 Fundamentals of Characters and Strings A program may contain character literals. An integer value represented as a character in single quotes. The value of a character literal is the integer value of the character in the Unicode character set. String literals (stored in memory as String objects) are written as a sequence of characters in double quotation marks. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

14.3 Class String Class String is used to represent strings in Java. The next several subsections cover many of class String’s capabilities. We’ll create strings using different versions of the String constructor A constructor is an object method called when declaring an object © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

14.3.1 String Constructors Create an empty string represented as "" and has a length of 0. String s1 = new String(); Constructor that takes a String object copies the argument into the new String. String str = "Hello"; String s = new String("Hello"); String s2 = new String(s2); Constructor that takes a char array creates a String containing a copy of the characters in the array. Constructor that takes a char array and two integers creates a String containing the specified portion of the array. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

14.3.2 String Methods length, charAt and getChars String method length determines the number of characters in a string. String method charAt returns the character at a specific position in the String. String method getChars copies the characters of a String into a character array. The first argument is the starting index in the String from which characters are to be copied. The second argument is the index that is one past the last character to be copied from the String. The third argument is the character array into which the characters are to be copied. The last argument is the starting index where the copied characters are placed in the target character array. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

14.3.3 Comparing Strings Strings are compared using the numeric codes of the characters in the strings. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

14.3.3 Comparing Strings (cont.) Method equals tests any two objects for equality The method returns true if the contents of the objects are equal, and false otherwise. Uses a lexicographical comparison. When primitive-type values are compared with ==, the result is true if both values are identical. When references are compared with ==, the result is true if both references refer to the same object in memory. Java treats all string literal objects with the same contents as one String object to which there can be many references. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

14.3.3 Comparing Strings (cont.) String method equalsIgnoreCase ignores whether the letters in each String are uppercase or lowercase when performing the comparison. Method compareTo is declared in the Comparable interface and implemented in the String class. Returns 0 if the Strings are equal, a negative number if the String that invokes compareTo is less than the String that is passed as an argument and a positive number if the String that invokes compareTo is greater than the String that is passed as an argument. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

14.3.3 Comparing Strings (cont.) Method regionMatches compares portions of two Strings for equality. The first argument to this version of the method is the starting index in the String that invokes the method. The second argument is a comparison String. The third argument is the starting index in the comparison String. The last argument is the number of characters to compare. Five-argument version of method regionMatches: When the first argument is true, the method ignores the case of the characters being compared. The remaining arguments are identical to those described for the four-argument regionMatches method. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

14.3.3 Comparing Strings (cont.) String methods startsWith and endsWith determine whether strings start with or end with a particular set of characters © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

14.3.4 Locating Characters and Substrings in Strings © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

14.3.4 Locating Characters and Substrings in Strings (cont.) Method indexOf locates the first occurrence of a character in a String. If the method finds the character, it returns the character’s index in the String—otherwise, it returns –1. A second version of indexOf takes two integer arguments—the character and the starting index at which the search of the String should begin. Method lastIndexOf locates the last occurrence of a character in a String. The method searches from the end of the String toward the beginning. If it finds the character, it returns the character’s index in the String—otherwise, it returns –1. A second version of lastIndexOf takes two integer arguments—the integer representation of the character and the index from which to begin searching backward. There are also versions of these methods that search for substrings in Strings. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

14.3.5 Extracting Substrings from Strings Class String provides two substring methods to enable a new String object to be created by copying part of an existing String object. Each method returns a new String object. The version that takes one integer argument specifies the starting index in the original String from which characters are to be copied. The version that takes two integer arguments receives the starting index from which to copy characters in the original String and the index one beyond the last character to copy. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

14.3.6 Concatenating Strings String method concat concatenates two String objects (similar to using the + operator) and returns a new String object containing the characters from both original Strings. The original Strings to which s1 and s2 refer are not modified. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

14.3.7 Miscellaneous String Methods © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

-.- Introduction to Enumerations Enumerated values are used to represent a set of named values. Historically in Java (and other languages), these were often stored as constants. For example, in Java . . . public static final int NORTH = 0; public static final int EAST = 1; public static final int SOUTH = 2; public static final int WEST = 3; © 1992-2015 by Pearson Education, Inc. All Rights Reserved.

-.- Introduction to Enumerations (cont.) There are several issues with this approach. Acceptable values are not obvious No type safety No name-spacing Not printable Eums address these issues (Java 5). Declared using the enum keyword instead of class In its simplest form, it contains a comma-separated list of names representing each of the possible options. public enum Direction { NORTH, EAST, SOUTH, WEST } © 1992-2015 by Pearson Education, Inc. All Rights Reserved.

-.- Introduction to Enumerations (cont.) Additional Benefits of enums. Storage of additional information enums are objects Retrieval of all enumerated values of a type: Direction[ ] directions = Direction.values(); Comparison of enumerated values if(suit == Suit.CLUBS) { // do something } © 1992-2015 by Pearson Education, Inc. All Rights Reserved.

-.- Introduction to Enumerations (cont.) Enums can also be used with the switch control structure. Direction dir = /* ... */; switch (dir) { case EAST: case WEST: // do something break; case NORTH: case SOUTH: // do something else default: // yet another thing } © 1992-2015 by Pearson Education, Inc. All Rights Reserved.