University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Data Types, Assignment, Expressions, Constants Lecture 3, Thu.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
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 Programming Languages Identifiers, Variables Lecture 2, Tue Jan.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Data Types, Assignment, Casting, Constants Lecture.
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.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Whitespace, Errors, Variables, Data Types, Assignment.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Languages, Whitespace, Identifiers Lecture 3, Mon Jan.
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.
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.
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.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Conditionals II Lecture 11, Thu Feb
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
String Escape Sequences
CSC Programming I Lecture 5 August 30, 2002.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Copyright © – Curt Hill Types What they do.
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:
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
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
Type Conversion, Constants, and the String Object
Escape Sequences What if we wanted to print the quote character?
Building Java Programs Chapter 2
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
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Introduction to Primitives
Building Java Programs
In this class, we will cover:
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 Jan-Apr 2006 Tamara Munzner Data Types, Assignment, Expressions, Constants Lecture 3, Thu Jan based on slides by Kurt Eiselt

News Weekly Question 1 due today Labs and tutorials started this week Labs on Friday cancelled you’ve been reassigned elsewhere if you missed assigned lab this week, attend another session if possible

Reminder: Reading This Week Ch : Computer Anatomy from last time Ch 1.3 – 1.8: Programming Languages Ch , 2.5: Types/Variables, Assignment, Numbers Ch : Numbers, Constants

Reading for Next Week Rest of Chap , Rest of Chap

Objectives Understand how to declare and assign variables Understand when and how to use which data type Understand how to convert between data types Understand how to interpret expressions Understand when to use constants

Recap: Assembly and Machine Languages Hard to read, write, remember Many instructions required to do things Different languages for each computer type add what’s to what’s and put it unimportant details for us in this in this in this register register register assembler assembly language machine language add r1,r2,r6

Recap: High-Level Languages Program written in high-level language converted to machine language instructions by another program called a compiler (well, not always) High-level instruction: A = B + C becomes at least four machine language instructions! load B load C add them store in A compiler high-level language machine language

Recap: Sample Java Program Comments, whitespace ignored by compiler //******************************************************* // Oreo.java Author: Kurt Eiselt // // Demonstrating simple Java programming concepts while // revealing one of Kurt's many weaknesses //******************************************************* public class Oreo { //***************************************************** // demand Oreos //***************************************************** public static void main (String[] args) { System.out.println ("Feed me more Oreos!"); }

Recap: Identifiers Identifiers: start with letter [a-Z,$,_], then letters of digits [0-9] and not be reserved words case matters meaningful and descriptive, yet concise

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

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

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

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

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

Data Types: Int and Double int integer double real number (double-precision floating point)

Floating Point Numbers significant digits

Floating Point Numbers significant digits 42= 4.2 * 10 = 4.2 * = 4.2 * 1 = 4.2 * = 4.2 * = 4.2 * = 4.2 * = 4.2 * 10 -5

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

Data Type Sizes 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

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

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

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

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

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

Assignment Statements Here’s an occasional point of confusion: 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);

Assignment Statements Here’s an occasional point of confusion: Variable values on left of = are clobbered Variable values on right of = are unchanged copy of value assigned to a also assigned to b 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);

Assignment Statements Here’s an occasional point of confusion: Memory locations a and b are distinct copy of value assigned to a also assigned to b changing a later does not affect previous copy 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);

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

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

Operator Precedence What does this expression evaluate to? * 5

Operator Precedence What does this expression evaluate to? * 5 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

Operator Precedence What does this expression evaluate to? * 5 Multiplication has higher operator precedence than addition (just like in algebra) 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

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

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

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

Converting Between Types 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); }

Data Type Sizes doubles can store twice as much as ints 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)

Primitive Data Types: Numbers Primary primitives are int and double three other integer types one other real type 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)

Converting Between Types //***************************************** // 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); }

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

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

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

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

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

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

Questions?