Strings and Object References

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
More methods and classes, 4 of 4 Math 130 Introduction to Programming Lecture # 18 Monday, October 8, 2007.
References and Objects Weiss ch. 2. Objects Are structured regions of memory –Reside in heap Each object is an instance of a type –e.g. String, File,
Classes and Objects in Java
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
10-Aug-15 Classes and Objects in Java. 2 Classes and Objects A Java program consists of one or more classes A class is an abstract description of objects.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CSC Programming I Lecture 8 September 9, 2002.
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Lecture 2: Classes and Objects, using Scanner and String.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
Questions? Suggestions?. References References Revisited What happens when we say: int x; double y; char c; ???
Object-Oriented Programming in C++
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Lecture 10: Object Oriented Programming Tami Meredith.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
OOP Basics Classes & Methods (c) IDMS/SQL News
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Chapter 9 A Second Look at Classes and Objects - 4.
Chapter 9 A Second Look at Classes and Objects - 2.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
JavaScript: Conditionals contd.
Sophomore Scholars Java
CSC 211 Java I for loops and arrays.
Topic: Classes and Objects
Classes and Objects Introduced
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
Static data members Constructors and Destructors
AKA the birth, life, and death of variables.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Classes and Objects in Java
Lecture 10: More on Methods and Scope
Computer Programming Methodology Introduction to Java
Programming Language Concepts (CIS 635)
Java Review: Reference Types
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Constructor Overloading
AP Java Unit 3 Strings & Arrays.
Java I.
7 Arrays.
Arrays.
CS 200 Loops Jim Williams, PhD.
Classes and Objects in Java
CS 180 Assignment 6 Arrays.
AKA the birth, life, and death of variables.
Assessment – Java Basics: Part 1
Building Java Programs
CLASSES, AND OBJECTS A FIRST LOOK
Overview of Java 6-Apr-19.
Data Structures & Algorithms
LCC 6310 Computation as an Expressive Medium
Classes and Objects in Java
Loops CGS3416 Spring 2019 Lecture 7.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
References Revisted (Ch 5)
Parameters, Overloading Methods, and Random Garbage
Classes and Objects Object Creation
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Strings and Object References Lecture # 14 Manish Sinha

Topics String Review The null value Garbage Collection Strings are immutable Sorting by Insertion

Review What two things does the following statement do? String zeta = new String("The last rose of summer"); Easy way to Construct Strings String zeta = "The last rose of summer" ; Question: Can a String be a parameter for a method?

String References as Parameters Example: Piece of a code String stringA = “Candle light"; String stringB = “Moon light"; if ( stringA.equals( stringB ) ) System.out.println("They are equal."); else System.out.println("They are different.");

String References as Parameters ….. Be CAREFUL: People often say "String" when they really mean "reference to a String" stringA.equals(stringB) What is usually said: The equals method of stringA is called with stringB Careful meaning: The equals method of the String referenced by stringA is called with a reference to stringB

Object Reference Object Reference is an information on how to find a particular object Object is a chunk of memory. A reference to object is a way to get to that memory Objects are created while a program is running An object reference does not contain the actual data

Questions Examine the following snippet of code. Answer the questions carefully: String a; Point b; What is the data type of the variable a? What is the data type of the variable b? How many objects are there (so far)?

Answers Q: What is the datatype of the variable a? A: a is a reference to a String object. Q: What is the data type of the variable b? A: b is a reference to a Point object. Q: How many objects are there (so far)? A: So far, there are no objects, just variables that can be used to keep track of objects once there are some.

The null value A reference variable holds information about the location of an object. It does not hold the object itself A special value called null is assigned to an object reference variable when it does not refer to an object The value null is a special value that means "no object." A reference variable is set to null when it is not referring to any object Though Question: Do you think that null can be assigned to reference variables of any type?

nullDemo1.java class nullDemo1 { public static void main (String[] arg) { String a = “Amitabh Bachchan"; String b = null; String c = ""; if ( a != null ) System.out.println( a ); if ( b != null ) System.out.println( b ); if ( c != null ) System.out.println( c ); }

Null Assigned to any Reference variable Question: In nullDemo1.java, What exactly is variable c initialized to? Answer: c is initialized to a reference to a String object containing no characters. This is most certainly a different value than null We need a universal value that means “nothing here”, the value null can be assigned to any reference variable A reference variable sometimes does and sometimes does not refer to an object. You need a way to say that a variable does not now refer to an object. You do this by assigning null to the variable

nullDemo1.java class nullDemo1 { public static void main (String[] arg) { String a = “Amitabh Bachchan"; // 1. an object is created // variable a refers to it String b = null; // 2. variable b refers to no object String c = ""; // 3. an object is created // containing no characters ----- variable c refers to it if ( a != null ) // 4. ( a != null ) is true, so System.out.println( a ); // 5. the println( a ) executes. if ( b != null ) // 6. ( b != null ) is false, so System.out.println( b ); // 7. the println( b ) is skipped. if ( c != null ) // 8. ( c != null ) is true, so System.out.println( c ); // 9. the println( c ) executes. // but it has no characters to print }

The Empty String A String object that contains no characters is still an object. Sometimes such an object is called an empty string. Examine the following code snippet: String alpha = "Dempster Dumpster"; alpha = null; . . . Where is an object constructed? In the first statement. What becomes of that object? It became garbage in the second statement.

Garbage The second statement assigns the value null to alpha. When this happens, the reference to the object is lost. Since there is no reference to the object elsewhere, it is now garbage. The line through the box in the second statement symbolizes the null value. The object still exists in memory. The memory occupied by the object will eventually be recycled by the garbage collector and will be made available for new objects. The first statement does two things: (1) a String object is created, containing the characters "Dempster Dumpster". Then, (2) a reference to that object is saved in the reference variable alpha

Garbage… String alpha = "Dempster Dumpster"; String beta = alpha; alpha = null; When was an object instantiated? In the first statement. What happened in the second statement?       The reference to the object was copied to beta. What becomes of beta object?       It remains "alive," because there is still a reference to it.

Not Garbage This time, the object does not become garbage. This is because in the second statement a reference to the object is saved in a second variable, beta. Now when, in the third statement, alpha is set to null, there is still a reference to the object. There can be many copies of the reference to an object. Only when there is no reference to an object anywhere does the object become garbage. Review: How can you determine what an object of a particular class can do?

String are immutable Strings are forever Confusion Alert: Distinguishing String Object and String Object reference Inspect the following code: String ring = "One ring to rule them all, " ; String find = "One ring to find them." ; ring = ring + find; Does the last sentence violate the rule that Strings are immutable?

Are the following statements True? String line = "The Sky was like a WaterDrop"; String a = line.toLowerCase(); String b = toLowerCase(line); String c = toLowerCase( "IN THE LIGHT OF MOON"); String d = “Neat, Clear, Beautiful".toLowerCase(); System.out.println("Dark,stormy...".toLowerCase());

Tedious Review Can an object reference variable exist without referring to an object? Yes, a reference variable can be declared without initialization: String myString; Also, a reference can be set to null. Can an object exist without an object reference variable referring to it? Yes

Assignment Study carefully InsertSortApp program Make the InsertSortApp program usable for file input

THE END Home: http://www.cse.iitb.ac.in/~manish Contact:manish[at]cse[dot]iitb[dot]ac[dot]in