University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Data Types, Assignment, Casting, Constants Lecture.

Slides:



Advertisements
Similar presentations
CSci 1130 Intro to Programming in Java
Advertisements

Types and Arithmetic Operators
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
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.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Data Types, Assignment, Expressions, Constants Lecture 3, Thu.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Objects, Methods, Parameters, Input Lecture 5, Thu Jan
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Whitespace, Errors, Variables, Data Types, Assignment.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Constants, Objects, Strings Lecture 4, Tue Jan
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
String Escape Sequences
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
CSC Programming I Lecture 5 August 30, 2002.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
C++ Programming: Basic Elements of C++.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
1 2. Program Construction in Java. 01 Java basics.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
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.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Building Java Programs
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Variables and Primative Types
Escape Sequences What if we wanted to print the quote character?
Chapter 2 Edited by JJ Shepherd
Building Java Programs
Building Java Programs
Building Java Programs Chapter 2
Expressions and Assignment
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Introduction to Primitives
Introduction to Primitives
Building Java Programs
Primitive Types and Expressions
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Data Types, Assignment, Casting, Constants Lecture 5, Fri Jan borrowing from slides by Kurt Eiselt

2 Reading This Week n Chap 1: n Chap 2: , 2.5 n Chap 4: n reminder: weekly reading questions due now!

3 Recap: White Space n White space n Blanks between identifiers and other symbols n Tabs and newline characters are included n White space does not affect how program runs n Use white space to format programs we create so they’re easier for people to understand

4 Recap: Errors n Compile-time errors n syntax/structure n Run-time errors n Logical errors n semantics/meaning compile-time error editingtranslatingexecuting insight source object results code run-time error logical error

5 Recap: Variables n Variable: name for location in memory where data is stored n avoid having to remember numeric addresses n like variables in algebra class n Variable names begin with lower case letters n Java convention, not compiler/syntax requirement

6 Recap: Data Types n Java requires that we tell it what kind of data it is working with n For every variable, we have to declare a data type n Java language provides eight primitive data types n i.e. simple, fundamental n For more complicated things, can use data types n created by others provided to us through the Java libraries n that we invent n More soon - for now, let’s stay with the primitives n We want a, b, and c to be integers n Here’s how we do it...

7 Recap: Variables and Data Types //***************************************** // Test3.java Author: Kurt // // Our third use of variables! //***************************************** public class Test3 { public static void main (String[] args) { int a; //these int b; //are int c; //variable declarations b = 3; c = 5; a = b + c; System.out.println ("The answer is " + a); }

8 Recap: Floating Point Numbers n significant digits n 42= 4.2 * 10 = 4.2 * 10 1 n 4.2= 4.2 * 1 = 4.2 * 10 0 n = 4.2 * = 4.2 * 10 7 n = 4.2 * = 4.2 * n only need to remember n nonzero digits n where to put the decimal point n floats around when multiply/divide by 10

9 Data Type Sizes n fixed size, so finite capacity TypeSizeMinMax int 4 bytes-2,147,483,6482,147,483,647 double 8 bytes approx -1.7E308 (15 sig. digits) approx 1.7E308 (15 sig. digits) Address Data one integer

10 Variable Declaration Examples n person’s age in years n height of mountain to nearest meter n length of bacterium in centimeters n number of pets at home

11 Variable Declaration and Assignment n variable declaration is instruction to compiler n reserve block of main memory large enough to store data type specified in declaration n variable name is specified by identifier n syntax: n typeName variableName;

12 Assignment //***************************************** // Test3.java Author: Kurt // // Our third use of variables! //***************************************** public class Test3 { public static void main (String[] args) { int a; int b; int c; b = 3; // these c = 5; // are a = b + c; // assignment statements System.out.println ("The answer is " + a); }

13 Assignment Statements n Assignment statement assigns value to variable n sometimes say binds value to variable n Assignment statement is n identifier n followed by assignment operator (=) n followed by expression n followed by semicolon (;) n Note that = is no longer a test for equality! b = 3; c = 8; a = b + c; weekly_pay = pay_rate * hours_worked;

14 Assignment Statements n Java first computes value on right side n Then assigns value to variable given on left side x = 4 + 7; // what’s in x? n Old value will be overwritten if variable was assigned before x = 2 + 1; // what’s in x now?

15 Assignment Statements n Here’s an occasional point of confusion: a = 7; // what’s in a? b = a; // what’s in b? // what’s in a now???

16 Assignment Statements n Here’s an occasional point of confusion: n Find out! Experiments are easy to do in CS a = 7; // what’s in a? b = a; // what’s in b? // what’s in a now??? System.out.println(“a is “ + a + “b is “ +b);

17 Assignment Statements n Here’s an occasional point of confusion: n Variable values on left of = are clobbered n Variable values on right of = are unchanged n copy of value assigned to a also assigned to b n but that doesn’t change value assigned to a a = 7; // what’s in a? b = a; // what’s in b? // what’s in a now??? System.out.println(“a is “ + a + “b is “ +b);

18 Assignment Statements n Here’s an occasional point of confusion: n Memory locations a and b are distinct n copy of value assigned to a also assigned to b n changing a later does not affect previous copy n more later a = 7; // what’s in a? b = a; // what’s in b? // what’s in a now??? System.out.println(“a is “ + a + “b is “ +b); a = 8; System.out.println(“a is “ + a + “b is “ +b);

19 Variable Declaration and Assignment n variable declaration is instruction to compiler n reserve block of main memory large enough to store data type specified in declaration n variable name is specified by identifier n syntax: n typeName variableName; n typeName variableName = value; n can declare and assign in one step

20 Expressions n expression is combination of n one or more operators and operands n operator examples: +, *, /,... n operand examples: numbers, variables,... n usually performs a calculation n don’t have to be arithmetic but often are n examples * 5 (7 + 2) * 5

21 Operator Precedence n What does this expression evaluate to? * 5

22 Operator Precedence n What does this expression evaluate to? * 5 n Multiplication has higher operator precedence than addition (just like in algebra) precedenceoperatoroperation 1 higher+ -unary plus and minus 2* / %multiply, divide, remainder 3 lower+ -add, subtract

23 Operator Precedence n What does this expression evaluate to? * 5 n Multiplication has higher operator precedence than addition (just like in algebra) n Use parentheses to change precedence order or just clarify intent (7 + 2) * (2 * 5) precedenceoperatoroperation 1 higher+ -unary plus and minus 2* / %multiply, divide, remainder 3 lower+ -add, subtract

24 Converting Between Types n Which of these are legal? n int shoes = 2; n double socks = 1.75; n double socks = 1; n int shoes = 1.5;

25 Converting Between Types n Which of these are legal? n int shoes = 2; n double socks = 1.75; n double socks = 1; n int shoes = 1.5; n Integers are subset of reals n but reals are not subset of integers

26 Casting n Casting: convert from one type to another with information loss n Converting from real to integer n int shoes = (int) 1.5; n Truncation: fractional part thrown away n int shoes = (int) 1.75; n int shoes = (int) 1.25; n Rounding: must be done explicitly n shoes = Math.round(1.99);

27 Converting Between Types n What’s wrong? //***************************************** // Feet.java Author: Tamara // What type of things can be put on feet? //***************************************** public class Feet { public static void main (String[] args) { int shoes = 2; int socks = (int) 1.75; System.out.println("shoes = " + shoes + " socks = " + socks); int toes = Math.round(1.99); System.out.println("toes = " + toes); }

28 Data Type Sizes n is there more to life than 4-byte ints or 8-byte doubles? TypeSizeMinMax int 4 bytes-2,147,483,6482,147,483,647 double 8 bytes approx -1.7E308 (15 sig. digits) approx 1.7E308 (15 sig. digits)

29 Primitive Data Types: Numbers n Primary primitives are int and double n three other integer types n one other real type n range of choices for storage capacity TypeSizeMinMax byte 1 byte short 2 bytes-32,76832,767 int 4 bytes-2,147,483,6482,147,483,647 long 8 bytes -9,223,372,036,854,775,8089,223,372,036,854,775,807 float 4 bytes approx -3.4E38 (7 sig.digits)approx 3.4E38 (7 sig.digits) double 8 bytes approx -1.7E308 (15 sig. digits) approx 1.7E308 (15 sig. digits)

30 Using Long Integers //***************************************** // Feet2.java Author: Tamara // What type of things can be put on feet? //***************************************** public class Feet2 { public static void main (String[] args) { int shoes = 2; int socks = (int) 1.75; System.out.println("shoes = " + shoes + " socks = " + socks); long toes = Math.round(1.99); System.out.println("toes = " + toes); }

31 Or Cast To Int //***************************************** // Feet3.java Author: Tamara // What type of things can be put on feet? //***************************************** public class Feet3 { public static void main (String[] args) { int shoes = 2; int socks = (int) 1.75; System.out.println("shoes = " + shoes + " socks = " + socks); int toes = (int) Math.round(1.99); System.out.println("toes = " + toes); }

32 Primitive Data Types: Non-numeric n Character type n named char n Java uses the Unicode character set so each char occupies 2 bytes of memory. n Boolean type n named boolean n variables of type boolean have only two valid values n true and false n often represents whether particular condition is true n more generally represents any data that has two states n yes/no, on/off

33 What Changes, What Doesn’t? //***************************************** // Vroom.java Author: Tamara // Playing with constants //***************************************** public class Vroom { public static void main (String[] args) { double lightYears, milesAway; lightYears = 4.35; // to Alpha Centauri milesAway = lightYears * *60*60*24*365; System.out.println("lightYears: " + lightYears + " milesAway " + milesAway); lightYears = 68; // to Aldebaran milesAway = lightYears * *60*60*24*365; System.out.println("lightYears: " + lightYears + " milesAway " + milesAway); }

34 Constants n Things that do not vary n unlike variables n will never change n Syntax: n final typeName variableName; n final typeName variableName = value; n Constant names in all upper case n Java convention, not compiler/syntax requirement

35 Programming With Constants public static void main (String[] args) { double lightYears, milesAway; final int LIGHTSPEED = ; final int SECONDS_PER_YEAR = 60*60*24*365; lightYears = 4.35; // to Alpha Centauri milesAway = lightYears * LIGHTSPEED * SECONDS_PER_YEAR; System.out.println("lightYears: " + lightYears + " miles " + milesAway); lightYears = 68; // to Aldebaran milesAway = lightYears * LIGHTSPEED * SECONDS_PER_YEAR; System.out.println("lightYears: " + lightYears + " miles " + milesAway); }

36 Avoiding Magic Numbers n magic numbers: numeric constants directly in code n almost always bad idea! n hard to understand code n hard to make changes n typos possible n use constants instead

37 Programming With Constants public static void main (String[] args) { double lightYears, milesAway; final int LIGHTSPEED = ; final int SECONDS_PER_YEAR = 60*60*24*365; final double ALPHACENT_DIST = 4.35; // to AlphaCentauri final double ALDEBARAN_DIST = 68; // to Aldebaran lightYears = ALPHACENT_DIST; milesAway = lightYears * LIGHTSPEED * SECONDS_PER_YEAR; System.out.println("lightYears: " + lightYears + " miles " + milesAway); lightYears = ALDEBARAN_DIST; milesAway = lightYears * LIGHTSPEED * SECONDS_PER_YEAR; System.out.println("lightYears: " + lightYears + " miles " + milesAway); }

38 Reading Next Week n Rest of Chap 2 n 2.3-4, n Rest of Chap 4 n