introduction to programming with java, for beginners string object primitive types and references...

16
Introduction to Programming with Java, for Beginners String Object Primitive Types and References The Stack and the Heap

Post on 19-Dec-2015

229 views

Category:

Documents


0 download

TRANSCRIPT

Introduction to Programmingwith Java, for Beginners

String ObjectPrimitive Types and ReferencesThe Stack and the Heap

2

Strings A sequence of characters A String is a built-in Java object type Java provides this type because it’s used so frequently Examples of String creation:

> String s1 = new String("hello");> String s2 = "hello"; // commonly used shortcut> s2 + " you!""hello you!"> s2 = "The result is " + 100;> System.out.println(s2);"The result is 100"

3

String Concatenation + usually means “add,” but if either operand (thing

involved in the operation) is a String, then + means concatenation

If you concatenate anything with a String, that thing is first turned into a String

+ as the concatenation operator is an exception to the rule: Primitives have operations, Objects have methods

A String is immutable (more on this on slide 16) Once you create it, there are no methods to change it But you can easily make new Strings:

myName = "Dave";myName = "Dr. " + myName;

4

String Object Methods

Listed in the Java API Part of the java language: java.lang Many useful Methods

length() toUpperCase() charAt(int index) substring(int beginIndex, int endIndex) …

5

System.out.println(String) Method that which part of the print program in class

System (java.lang.System) System is stateless class like Math class

Has static variable called “out” of reference type PrintStream Class PrintStream contains println(String x)

Input takes in string literal or variable If something else the gets converted to string automatically

System.out.println(“hello, world”);System.out.print(“x is “ + x);System.out.println(“, answer is “ + foo());

6

Memory: Stack and Heap

When we run a standalone Java programs, memory is allocated for variables and objects

Understanding how this memory is managed helps us understand how Java works

The JVM uses two kinds of memory: stack and heap

The stack is used to store variables of primitive type: When created in the DrJava interactions pane During method calls

The heap is used to store objects

7

How the Stack Grows

DrJava Interactions Stack

> int x;

> x = 5;

> double min = 0.5;

> boolean done = false;

8

Reference Types

Examples of reference variables:String name;Counter c1;

In Java, no variable can ever hold an object One variable can only contain one thing Object consists of multiple of data/state and hence

stored on heap

A variable can only hold a reference to an object a reference to an object is the address of the

memory location where the object is stored on the heap

9

Value of a Reference Variable The value of are reference variable is either null or a

“heap address” Null means currently not pointing at any location

Example:> Counter c1;> c1null> c1 = new Counter();> c1Counter@e05ad6

e05ad6 is location in memory where c1 resides e05ad6 hexadecimal (base 16) number

We don’t have to (and can’t) deal with these hex numbers directly

10

How the Heap Grows

DrJava Interactions Stack and Heap> int x = 99;

> Counter c1;

> c1

null

> c1 = new Counter();

> c1

Counter@2f996f

> c1.incrementCount();

> Counter c2 = new Counter();

> c2

Counter@4a0ac5

11

Freshman DormRoom Example

12

DormRoom Code and UML

> DormRoom room = new DormRoom(208, "Hill");

> room.getLocation()

"208 Hill"

public class DormRoom{ private int num; private String bldgName; public DormRoom(int n, String b){ num = n; bldgName = b; } public String getLocation(){ return num + " " + bldgName; }}

13

A DormRoom on the Heap

> DormRoom room = new DormRoom(208, "Hill");

> room.getLocation()

"208 Hill"

14

Freshman Code and UML> DormRoom room = new DormRoom(208, "Hill");

> Freshman f = new Freshman("jo", room);

> f.getName()

"jo"

> f.getRoom().getLocation()

"208 Hill"

public class Freshman{ private String name; private DormRoom room; public Freshman(String n, DormRoom r){ name = n; room = r; } public String getName(){ return name;} public DormRoom getRoom(){ return room;}}

15

A Freshman on the Heap

> DormRoom room = new DormRoom(208, "Hill");

> Freshman f = new Freshman("jo", room);

> f.getName()

"jo"

> f.getRoom().getLocation()

"208 Hill"

16

String Immutability String s1 = new String("Hello"); String s2 = new String("There"); System.out.println(s1); s1 = s2; System.out.println(s1);

If Strings cannot be changed then s1 should still print out Hello But it prints “There”

The immutability really refers to what the String reference is pointing to When s2 is assigned to s1, the String containing "Hello" is no

longer referenced and s1 now points to the same string as s2 The fact that the "Hello" string has not actually been modified is

fairly theoretical as you can no longer "get at it".