Lecture 2 Java Semantics Validation CS201j Engineering Software University of Virginia Computer Science David Evans http www cs virginia edu evans Menu Objects in Java Heap and Stack Validation Testing and Analysis 2 September 2003 CS 201J Fall 2003 2 Java Semantics 2 September 2003 CS 201J Fall 2003 3 The Stack and Heap s String s new String hello java lang String hello String is a type in the Java API for representing sequences of characters Objects live on the Heap new creates an Object on the Heap Local variables live on the Stack Point to objects on the Heap 2 September 2003 CS 201J Fall 2003 4 String s new String hello String t s s t 2 September 2003 java lang String hello CS 201J Fall 2003 5 String s new String hello String t s s t java lang String hello java lang String goodbye s new String goodbye 2 September 2003 CS 201J Fall 2003 6 Primitive Types Not everything in Java is an Object Some types are primitive types boolean byte char double float int long short Values of primitive types are stored directly on the stack 2 September 2003 CS 201J Fall 2003 7 String s new String hello s java lang String t String t s int i 201 int j i hello i 201 j 201 2 September 2003 How can we see the difference between primitive types and objects CS 201J Fall 2003 8 Equality x y Object Types same objects Primitive Types same value x equals y Object Types method that compares values of objects Primitive Types doesn t exist 2 September 2003 CS 201J Fall 2003 9 Mutability If an object is mutated all references to the object see the new value StringBuffer sb new hi StringBuffer tb sb tb append gh sb tb 2 September 2003 java lang StringBuffer high hi CS 201J Fall 2003 10 Immutable Mutable Types Types can be mutable or immutable Objects of an immutable type never change value after they are created String is immutable StringBuffer is mutable String concat creates a new String object StringBuffer append mutates the old object 2 September 2003 CS 201J Fall 2003 11 Java Semantics Question public class Strings public static void test String args String s new String hello String t new String hello StringBuffer sb new StringBuffer he StringBuffer tb sb String s1 hello String t1 hello sb append llo tb append goodbye s concat goodbye t s concat goodbye What are the values of s t sb and tb now Which of these are true a s t b s1 t1 c s s1 d s equals t e sb tb f t equals tb 2 September 2003 CS 201J Fall 2003 12 Java Semantics Question public class Strings public static void test String s new String hello String t new String hello StringBuffer sb new StringBuffer he StringBuffer tb sb String s1 hello String t1 hello java lang String s t hello java lang String hello sb append llo tb append goodbye s concat goodbye t s concat goodbye sb java lang StringBuffer tb he String spec is not enough to determine if s t s1 and t1 are the same objects This is what Sun s JDK 1 4 does Other implementations could correctly do different things s1 2 September 2003 t1 java lang String hello Note added Feb 2005 Nora Sovarel noticed that this isn t actually true The JLS section on String literals specifies CS 201J Fall 2003 the behavior as shown 13 http java sun com docs books jls second edition html lexical doc html 101083 Java Semantics Question public class Strings public static void test String s new String hello String t new String hello StringBuffer sb new StringBuffer he StringBuffer tb sb String s1 hello String t1 hello sb append llo tb append goodbye s concat goodbye t s concat goodbye java lang String s t java lang String hello sb java lang StringBuffer tb hello he s1 t1 2 September 2003 hello CS 201J Fall 2003 java lang String hello 14 Java Semantics Question public class Strings public static void test String s new String hello String t new String hello StringBuffer sb new StringBuffer he StringBuffer tb sb String s1 hello String t1 hello sb append llo tb append goodbye s concat goodbye t s concat goodbye java lang String s t sb tb s1 t1 2 September 2003 CS 201J Fall 2003 hello java lang String hello java lang StringBuffer hello he goodbye java lang String hello 15 Java Semantics Question public class Strings public static void test String s new String hello String t new String hello StringBuffer sb new StringBuffer he StringBuffer tb sb String s1 hello String t1 hello sb append llo tb append goodbye s concat goodbye t s concat goodbye java lang String hello goodbye 2 September 2003 java lang String s t sb tb s1 t1 CS 201J Fall 2003 hello java lang String hello java lang StringBuffer hello he goodbye java lang String hello 16 java lang String public class Strings public static void test String s new String hello String t new String hello StringBuffer sb new StringBuffer he StringBuffer tb sb String s1 hello String t1 hello sb append llo tb append goodbye s concat goodbye t s concat goodbye java lang String hello goodbye 2 September 2003 hello goodbye java lang String s t sb tb s1 t1 CS 201J Fall 2003 hello java lang String hello java lang StringBuffer hello he goodbye java lang String hello 17 java lang String After test returns public class Strings public static void test String s new String hello String t new String hello StringBuffer sb new StringBuffer he StringBuffer tb sb String s1 hello String t1 hello sb append llo tb append goodbye s concat goodbye t s concat goodbye java lang String hello goodbye 2 September 2003 hello goodbye java lang String s t sb tb s1 t1 CS 201J Fall 2003 hello java lang String hello java lang StringBuffer hello he goodbye java lang String hello 18 Validation 2 September 2003 CS 201J Fall 2003 19 Dictionary Definition val i date 1 To declare or make legally valid 2 To mark with an indication of official sanction 3 To establish the soundness of corroborate Can we do any of these with software 2 September 2003 CS 201J Fall 2003 20 Java s License READ THE TERMS OF THIS AGREEMENT AND ANY PROVIDED SUPPLEMENTAL LICENSE TERMS COLLECTIVELY AGREEMENT CAREFULLY BEFORE OPENING THE SOFTWARE MEDIA PACKAGE BY OPENING THE SOFTWARE MEDIA PACKAGE YOU AGREE TO THE TERMS OF THIS AGREEMENT IF YOU ARE ACCESSING THE SOFTWARE ELECTRONICALLY INDICATE YOUR ACCEPTANCE OF THESE TERMS BY SELECTING THE ACCEPT BUTTON AT THE END OF THIS AGREEMENT IF YOU DO NOT AGREE TO ALL THESE TERMS PROMPTLY RETURN THE UNUSED SOFTWARE TO YOUR PLACE OF PURCHASE FOR A REFUND OR IF THE SOFTWARE IS ACCESSED ELECTRONICALLY SELECT THE DECLINE BUTTON AT 2 September 2003 CS 201J Fall 2003 21 THE END OF THIS AGREEMENT Java s License 5
View Full Document
Unlocking...