Introduction to Programming with Java, for Beginners

Slides:



Advertisements
Similar presentations
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Advertisements

Introduction to Programming with Java, for Beginners “Has a” Relationship.
Strings and Arrays The objectives of this chapter are:  To discuss the String class and some of its methods  To discuss the creation and use of Arrays.
ECE122 L4: Creating Objects February 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
10-Jun-15 Using Objects. 2 Overview In this presentation we will discuss: Classes and objects Methods for objects Printing results.
Shlomo Hershkop1 Introduction to java Class 1 Fall 2003 Shlomo Hershkop.
Using Objects. Overview In this presentation we will discuss: –Classes and objects –Methods for objects –Printing results.
J.43 ARRAYS  A Java array is an Object that holds an ordered collection of elements.  Components of an array can be primitive types or may reference.
Implications of Substitution Fall 2005 OOPD John Anthony.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
28-Jun-15 Using Objects. 2 Overview In this presentation we will discuss: Classes and objects Methods for objects Printing results.
29-Jun-15 Using Objects. 2 Classes and objects The type of an object is the class that describes that object If we say int count, the type of count is.
References, Aliases, Garbage Collection and Packages Packages and Importing Classes Reading for this Lecture: L&L, Familiarize yourself with.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Lecture 2: Classes and Objects, using Scanner and String.
Java 2 More Java Then Starbucks ;-). Important Terms Primitive Data – Basic, built-in values (characters & numbers) Data Type – Used to talk about values.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
From C++ to Java A whirlwind tour of Java for C++ programmers.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
Interacting Classes Lecture #6 CMPSCI 121, Spring 2005 Introduction to Problem Solving with Computers Prof. McCallum No quiz today. It will again be in.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
Chapter 2 Using Objects. Types A type defines a set of values and the operations that can be carried out on the values Examples: 13 has type int "Hello,
Some Standard Classes Goals The Object class The String class Wrapper classes The Math class Random Numbers.
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 2 - Using Objects.
Chapter 7: Characters, Strings, and the StringBuilder.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Copyright Curt Hill Variables What are they? Why do we need them?
CS 11 java track: lecture 2 This week: more on object-oriented programming (OOP) objects vs. primitive types creating new objects with new calling methods.
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.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
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.
17-Feb-16 String and StringBuilder Part I: String.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
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.
Strings A string is a sequence of characters that is treated as a single value. Strings are objects. We have been using strings all along. For example,
Introduction to Programming using Java Day 3 and 4 Java Language Basics Review The “For” loop Subroutines The “String” class.
String and StringBuffer classes
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Information and Computer Sciences University of Hawaii, Manoa
Chapter 2 Basic Computation
String class.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
String Handling in JAVA
Java Programming: From Problem Analysis to Program Design, 4e
Introduction to Programming in Java
String and StringBuilder
Using Objects 21-Nov-18.
String and StringBuilder
Chapter 2: Basic Elements of Java
Strings A string is a sequence of characters that is treated as a single value. Strings are objects. We have been using strings all along. Every time.
Object Oriented Programming in java
String and StringBuilder
String methods 26-Apr-19.
Outline Creating Objects The String Class The Random and Math Classes
String Class.
SPL – PS2 C++ Memory Handling.
Presentation transcript:

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

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"

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;

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) …

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());

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

How the Stack Grows DrJava Interactions Stack > int x; > x = 5; > double min = 0.5; > boolean done = false;

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

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; > c1 null > c1 = new Counter(); Counter@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

How the Heap Grows DrJava Interactions Stack and Heap > int x = 99; > Counter c1; > c1 null > c1 = new Counter(); Counter@2f996f > c1.incrementCount(); > Counter c2 = new Counter(); > c2 Counter@4a0ac5

Freshman DormRoom Example

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;

A DormRoom on the Heap > DormRoom room = new DormRoom(208, "Hill"); > room.getLocation() "208 Hill"

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;}

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"

String Immutability String s1 = new String("Hello"); String s2 = new String("There"); System.out.println(s1); s1 = s2; 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".