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.

Slides:



Advertisements
Similar presentations
Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.
Advertisements

Strings Testing for equality with strings.
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
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.
Java Programming Strings Chapter 7.
AP Computer Science TOPICS TO DISCUSS.equals() == instanceof operator compareTo Interfaces Abstract Classes.
Java Strings in 10 minutes
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Programming 2 CS112- Lab 2 Java
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Fundamental Programming Structures in Java: Strings.
Lab session 3 and 4 Topics to be covered Escape sequences Escape sequences Variables /identifiers Variables /identifiers Constants Constants assignment.
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 4: Strings & Arrays Sanjay Goel.
The character data type char
String Class in Java java.lang Class String java.lang.Object java.lang.String java.lang.Object We do not have to import the String class since it comes.
Strings.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 6, page 1 Sun Certified Java 1.4 Programmer Chapter 6 Notes Gary Lance
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
From C++ to Java A whirlwind tour of Java for C++ programmers.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Characters. Character Data char data type – Represents one character – char literals indicated with ' '
Working with string In java Four classes are provided to work with String:1) String 2)String Buffer 3)String Tokenizer 4)String Builder Note: An object.
CS177 Week2: Recitation Primitive data types and Strings with code examples.
Some Standard Classes Goals The Object class The String class Wrapper classes The Math class Random Numbers.
The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory.
Characters and Strings. Characters  New primitive char  char letter; letter = ‘a’; char letter2 = ‘C’;  Because computers can only represent numbers,
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know.
String Processing Word processing term papers, writing memoirs, sending messages, responding to surveys, placing online orders and registering products.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Java String 1. String String is basically an object that represents sequence of char values. An array of characters works same as java string. For example:
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
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.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Data Types References:  Data Type:  In computer science and computer programming, a data type or simply type is a.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
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.
2 Arrays Array is a data structure that represents a collection of the same type of data. A "fixed length list".
1 reading: 3.3 Using objects. Objects So far, we have seen: methods, which represent behavior variables, which represent data (categorized by types) It.
 Type Called bool  Bool has only two possible values: True and False.
The Methods and What You Need to Know for the AP Exam
String and StringBuffer classes
Java Variables and Types
Strings, StringBuilder, and Character
Data Types, Identifiers, and Expressions
COMP Objects and References
Multiple variables can be created in one declaration
Programming in Java Text Books :
String Handling in JAVA
Primitive Types Vs. Reference Types, Strings, Enumerations
An Introduction to Java – Part I
String Objects & its Methods
Modern Programming Tools And Techniques-I Lecture 11: String Handling
An Introduction to Java – Part I, language basics
Exposure Java 2015 Pre-AP®CS Edition Chapter 12 Slides String Methods
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
The compareTo interface
Fundamentals 2.
CS2011 Introduction to Programming I Arrays (II)
Introduction to java Part I By Shenglan Zhang.
In Java, strings are objects that belong to class java.lang.String .
Presentation transcript:

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 length will be at least 2. middleTwo("string") → "ri" middleTwo("code") → "od" middleTwo("Practice") → "ct“ public String middleTwo(String str) { } string ri middle 6/2 = 3 Index 3 is i So we need 1 before middle and the middle Now write the substring method to get the middle two

Java is a pass by value ONLY When x is passed to the change() method, a copy of value of x (a reference) is passed. The method change() creates another object "cd" and it has a different reference. It is the variable x that changes its reference(to "cd"), not the reference itself. public class StringReference { public static void change(String x) x = "cd"; System.out.println(x); } public static void main(String[] args) String x = new String("ab"); change(x);

String is a Class: When an object is created it creates an Object Reference When an object is created a reference to that object is stored in memory. X would not store the actual value just a reference to it. public class Reference { public static void main(String[]args) Reference r = new Reference(); System.out.println(r); } Reference@83e35b memory location for object

References and Strings You can create Strings two different ways. Using the new keyword to create a String creates a different reference that creating a String without the new keyword. String s = new String(“mom”); String s2 = new String(“mom:”); String s3 = s2; String s4 = “mom”; String s5 = “mom:;

Storing String objects Java has 2 types of memory for Strings. 1st is the POOL Objects without the keyword new are stored in a Pool. Objects created this way that contain the same “String” information and also share the same reference. This saves space in memory. There is one reference and they all share it. String s = “Sun”; String s2 = “Sun”; s == s2 True (same reference ) s.equals(s2) True (same String “Sun”) Sun

Java has 2 types of memory for Strings. HEAP Objects created with the keyword new are stored in a Heap. Objects created this way that contain the same “String” information also share the same reference. However, they do not share the same reference. Each object created this way has its own reference. This saves space in memory. There is one reference and they all share it. String s = new String(“Sun”); String s2 = new String(“Sun”); s == s2 false (not same reference ) s.equals(s2) True (same String “Sun”) s reference Sun s2 reference

POOL HEAP String s1 = "Sun"; String s3 = “Sun”; When created this way, Java looks in the pool to see if there is already a String with the same name. If so points to it and has the same reference as other objects with that content. When created WITH THE WORD NEW objects go on the heap. Each object is brand new and has its own reference. It not shared. String s1 = "Sun"; String s3 = “Sun”; String s4 = new String(“Sun“); String s5 = new String (“Sun”); There is only one reference created. All objects share the same reference to the object “Sun”. These are created as new objects and do not have the same reference. Every object created is unique and has its own reference. s1 It is safe for immutable objects to copy references rather than the contents. Cloning (creating copies of objects) is not required. This may save time when Strings are stored in lists, etc. String reference s4 String reference “Sun" "Sun" s5 String reference s3 POOL HEAP

Alias You can assign a String to another String; however and it will contain the same reference. String s = new String(“Sun”); s2 = s; Now there is only one object. s and s2 are aliases of each other. They now share the same reference.

String s1 = "Hello"; // String literal String s3 = s1; // same reference String s4 = new String("Hello"); // String object String s5 = new String("Hello"); // String object

Why does this matter? String comparison: There are three ways to compare String objects: By equals() method (inherited from Object) By == operator By compareTo() method

By equals() method: Equals() method compares the original content of the String. Does it have the same content. public boolean equals(Object another) public boolean equalsIgnoreCase(String another)

Equality using equals(Object o) String n = "Computer"; String s = "Computer"; String t = new String("Computer"); String u = new String("Computer"); String v = new String (“computer”); boolean b = n.equals(s); true boolean b = n.equals(t); true boolean b = n.equals(u); true boolean b = n.equalsIgnoreCase(v); true

By == operator String n = "Computer"; String s = "Computer"; String t = new String("Computer"); String u = new String("Computer"); n t String reference “Computer" String reference u s String reference HEAP “Computer" POOL

== equality String n = "Computer"; String s = "Computer"; String t = new String("Computer"); String u = new String("Computer"); String v = u; n == s n == t t == u v==u

compareTo method parameters int compareTo(Object o) // Object o is the object to be compared or int compareTo(String anotherString) // String to be compared What is returned if the two strings are equal to each other returns 0 . if argument is > than String return value < 0 (negative number) if the argument is < than the string return > 0 (positive number) .

Lexicographic order: Lexicographic order is a generalization of alphabetical order. In this ordering, numbers come before letters and capital letters come before lower case letters. Lexicographic comparison is like alphabetizing the Strings. numbers uppercase lower case http://ss64.com/ascii.html

Ascii table Notice the Ascii table assigns a decimal value for each character, symbol, uppercase and lowercase letter. This is used in the compareTo method.

Lexicographical Upper case characters are regarded as less than lower case characters. "APPLE".compareTo("apple") returns a negative integer. -32 Argument is greater than String A ascii code of 65 < a ascii code of 97 “apple”.compareTo(“APPLE”) returns positive 32 argument less than String a ascii code of 97 > A ascii code of 65 “apple”.compareTo(“apple”) returns 0 equal to each other RULES FOR compareTo if the two strings are equal to each other returns 0 . if argument is > than String return value < 0 (negative number) if the argument is < than the string return > 0 (positive number

-32 32 5 “ABC to “abc argument is > so negative -32 Return Value : if the two strings are equal to each other returns 0 . if argument is greater than String return value < 0 (negative number) if the argument is less than the string return > 0 (positive number) String str1 = "Abc"; String str2 = "abc"; String str3 = "year"; String str4 = "table"; String str5 = "abc"; System.out.println(str1.compareTo(str2)); System.out.println(str2.compareTo(str1)); System.out.println(str3.compareTo(str4)); System.out.println(str5.compareTo(str2)); “ABC to “abc argument is > so negative -32 “abc to “Abc” argument is < so positive 32 “year” to “”table” argument is < so positive 5 “abc” to “abc” equal returns 0 -32 32 5

public class Compare { public static void main(String[]args) String a = "APPLE"; String a2 = "apple"; String a3 = "Apple"; String a4 = "banana"; String a5 = "APPLE"; String word1 = "a"; String word2 = "2"; System.out.println("apple and APPLE =" + a2.compareTo(a)); System.out.println(" APPLE and apple =" + a.compareTo(a2)); System.out.println("APPLE and Apple =" + a.compareTo(a3)); System.out.println("APPLE and APPLE = " + a.compareTo(a5)); System.out.println("a and 2 =" + word1.compareTo(word2)); System.out.println("banana and APPLE =" + a4.compareTo(a)); System.out.println("APPLE and banana =" + a.compareTo(a4)); } Will each print a positive number negative number or 0