Cs2220: Engineering Software Class 3: Java Semantics Fall 2010 University of Virginia David Evans.

Slides:



Advertisements
Similar presentations
Classes And Objects II. Recall the LightSwitch Class class LightSwitch { boolean on = true; boolean isOn() { return on; } void switch() { on = !on; }
Advertisements

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Air Force Institute of Technology Electrical and Computer Engineering
David Evans CS201j: Engineering Software? University of Virginia Computer Science Lecture 2: Java Semantics, Validation.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Constants and Data Types Constants Data Types Reading for this class: L&L,
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
1 Java Basics. 2 Compiling A “compiler” is a program that translates from one language to another Typically from easy-to-read to fast-to-run e.g. from.
Shlomo Hershkop1 Introduction to java Class 1 Fall 2003 Shlomo Hershkop.
COMS W3156: Software Engineering, Fall 2001 Lecture #20: C, continued Janak J Parekh
CS102 Data Types in Java CS 102 Java’s Central Casting.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
BASIC JAVA. Hello World n // Hello world program public class MyFirstJavaProgram { public static void main(String args[]) { char c = 'H'; String s =
Fundamental Programming Structures in Java: Strings.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Strings, StringBuilder, StringBuffer. String Strings in java are immutable Once created they cannot be altered and hence any alterations will lead to.
Cs205: engineering software university of virginia fall 2006 Semantics and Specifying Procedures David Evans
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in The.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Objects, Classes and Syntax Dr. Andrew Wallace PhD BEng(hons) EurIng
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
Copyright © 2012 Accenture All Rights Reserved.Copyright © 2012 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part C Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
CS-1030 Dr. Mark L. Hornick 1 C++ Language Basic control statements and data types.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
Copyright Curt Hill Variables What are they? Why do we need them?
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
“Great leaders are never satisfied with current levels of performance. They are restlessly driven by possibilities and potential achievements.” – Donna.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
Object-Oriented Programming Fundamentals. Comp 241, Fall Example Colored points on the screen What data goes into making one? Coordinates Color.
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
Data Types References:  Data Type:  In computer science and computer programming, a data type or simply type is a.
Cs205: engineering software university of virginia fall 2006 Programming Exceptionally David Evans
100e Hosted by vhs © Don Link, Indian Creek School, 2004 Jeopardy.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
Object Oriented Programming Lecture 2: BallWorld.
Java: Base Types All information has a type or class designation
String and StringBuffer classes
Topics introduced today (these topics would be covered in more detail in later classes) – Primitive Data types Variables Methods “for” loop “if-else” statement.
Fundamentals 2.
Intro to ETEC Java.
Crash course in the Java Programming Language
Java: Base Types All information has a type or class designation
CS216: Program and Data Representation
CS180 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
CS230 Tutorial Week 3.
Programming Language Concepts (CIS 635)
Chapter 2.
Introduction to Programming in Java
Object Oriented Programming (OOP) LAB # 8
Chapter 7: Strings and Characters
March 29th Odds & Ends CS 239.
MSIS 655 Advanced Business Applications Programming
An Introduction to Java – Part I, language basics
Lecture 3: Abstraction by Specification CS201j: Engineering Software?
The Boolean (logical) data type boolean
Chapter 2: Java Fundamentals
In this class, we will cover:
Java’s Central Casting
Outline Creating Objects The String Class The Random and Math Classes
Lecture 2: Java Semantics, Validation CS201j: Engineering Software?
Presentation transcript:

cs2220: Engineering Software Class 3: Java Semantics Fall 2010 University of Virginia David Evans

Menu Java Semantics Stack and Heap

The Stack and Heap String s = new String (“hello”); Objects live on the heap new creates an object on the heap (Almost) equivalent to: String s = “hello”; s java.lang.String: “hello” Local variables live on the stack May point to Objects in heap

The Stack and Heap String s = new String (“hello”); String t = s; s java.lang.String: “hello”

The Stack and Heap String s = new String (“hello”); String t = s; s = new String (“goodbye”); s java.lang.String: “hello” t

Primitive Types Almost everything in Java is an Object The exceptions are primitive types: boolean, byte, char, double, float, int, long, short Primitive types have different semantics! Values of a primitive type are stored directly on the stack

Primitive Types String s = new String (“hello”); String t = s; s = new String (“goodbye”); int i = 2200; int j = i; s java.lang.String: “hello” t java.lang.String: “goodbye” i j

Can we see the difference between primitive types and objects? Does it matter? Does it matter if something is stored on the stack or the heap?

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 Preview: the equals method is defined in java.lang.Object, which is the ultimate superclass of all classes. Other classes override equals to mean different things.

Mutability When an Object is mutated, all references to the Object see the new value. StringBuffer sb = new StringBuffer (“hi”); StringBuffer tb = sb; tb.append (“gh”);

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 this object Note: StringBuilder is almost identical to StringBuffer.

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/must be true: a) s == t b) s1 == t1 c) s == s1 d) s.equals (t) e) sb == tb f) t.equals (tb)

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/must be true: a) s == t b) s1 == t1 c) s == s1 d) s.equals (t) e) sb == tb f) t.equals (tb)

Java Language Specification (Section : String Literals) Each string literal is a reference (§4.3) to an instance (§4.3.1, §12.5) of class String (§4.3.3). String objects have a constant value. String literals-or, more generally, strings that are the values of constant expressions (§15.28)-are "interned" so as to share unique instances, using the method String.intern.(§4.3)§4.3.1§12.5§4.3.3(§15.28)

Summary Java sacrificed simplicity and coherence for performance: primitive types are not Objects Cost: programmers have to think about stack, heap, semantics differences Benefit: saves memory, perhaps better performance, more like C/C++ Reading before next class: Chapters 3 and 9 PS2: Part I posted now; Part II posted later.