© 2006 Pearson Addison-Wesley. All rights reserved 5.1.1 Reference Types A variable of reference type refers to (is bound to) an object Data of reference.

Slides:



Advertisements
Similar presentations
© 2007 Lawrenceville Press Slide 1 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5;
Advertisements

Types and Arithmetic Operators
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.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
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.
1 Chapter 3 Arithmetic Expressions. 2 Chapter 3 Topics l Overview of Java Data Types l Numeric Data Types l Declarations for Numeric Expressions l Simple.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
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.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.2 Expressions and Assignment Statement.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Data Types, Expressions and Functions (part I)
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Chapter 4 Variables and Constants. Goals and Objectives 1. Understand simple data types (int, boolean, double). 2. Declare and initialize variables using.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Java Software Solutions Lewis and Loftus Chapter 5 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. More Programming Constructs.
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.
Java Structure import java_packages; class JavaClass { member variables declarations; void otherMethod( ) { } public static void main(String[]
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
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.
Primitive Variables.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
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.
© 2006 Pearson Addison-Wesley. All rights reserved Syntax if (some_condition) { then_clause } Notes some_condition must be a valid boolean expression.
Lecture 7.1 Selection - the if Instruction. © 2006 Pearson Addison-Wesley. All rights reserved An algorithm often needs to make choices… choose.
Numeric Data Types There are six numeric data types: byte, short, int, long, float, and double. Sample variable declarations: int i, j, k; float numberOne,
© 2005 Lawrenceville Press Slide 1 Chapter 4 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms:
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CMSC 202 Java Primer 1. July 24, 2007 Copyright © 2008 Pearson Addison-Wesley 2 A Sample Java Application.
Primitive Data Types int is a primitive data type A primitive data type is one that stores only a single piece of data. TypeStorageDescription int 4 bytes+ve.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Variables and Constants Chapter 4 Review. Declaring Variables A variable is a name for a value stored in memory. Variables and constants are used in programs.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
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.
ISBN Chapter 7 Expressions and Assignments Statements.
© 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 4 Assignment Statement
Chapter 3 Syntax, Errors, and Debugging
Java Primer 1: Types, Classes and Operators
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Object Oriented Programming
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Primitive and Reference Data Values
Chapter 3 Assignment Statement
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2: Basic Elements of Java
Expressions and Assignment
Primitive Types and Expressions
Presentation transcript:

© 2006 Pearson Addison-Wesley. All rights reserved Reference Types A variable of reference type refers to (is bound to) an object Data of reference type must be instantiated before it can be used. Examples: JFrame, Color, Rectangle, etc. _______________ Types A variable of primitive type has a value. Data of primitive type can not be instantiated. Example primitive types: int boolean char Primitive types generally permit a built-in Java notation for constants (sometimes called literals). Primitive types often support operators.

© 2006 Pearson Addison-Wesley. All rights reserved byte - - one byterange: -128 through 127 Integer numeric types short - - two bytesrange: through int - - four bytesrange: through long - - eight bytesrange: through float - - four bytesaccuracy: 7 decimal digits Real numeric types double - - eight bytesaccuracy: 15 decimal digits boolean - - one byte Logical type char - - two bytes Character type These are discussed later

© 2006 Pearson Addison-Wesley. All rights reserved  one or more consecutive decimal digits (0-9), optionally preceded by - or + Constant Syntax Prefix Operator (abridged)  examples: unary negationexample: -7 Infix Operators (abridged) + additionexample: subtractionexample: * multiplicationexample: 12 * 5 / integer division (i.e., quotient)example: 12 / 5 % remainderexample: Postfix Operators (abridged) ++ autoincrementexample: -- autodecrementexample: someIntVar--

© 2006 Pearson Addison-Wesley. All rights reserved  a number with an embedded decimal point; leading - or + is optional; E-format can be used for scientific notation. Constant Syntax Prefix Operator (abridged)  examples: E17 5.2E+3 - unary negationexample: -2.5 Infix Operators (abridged) + additionexample: subtractionexample: * multiplicationexample: 10.1 * 0.5 / real division example: 14.4 / 1.2

© 2006 Pearson Addison-Wesley. All rights reserved Question Answer (informally) How is the following expression evaluated? * 2 Evaluation Rules Is it (7+3) * 2 OR 7 + (3*2) ??? Java uses rules that are similar to those understood by mathematicians. 1. Operations inside parentheses are performed before those outside. 3. Within equal precedence groupings operations are performed left to right (with a few exceptions, as shown in the complete precedence table). Precedence Table (abridged) postfix - unary * / % multiplicative + - additive 2. Within the same parenthesis grouping operations of higher precedence are performed before those with lower precedence.

© 2006 Pearson Addison-Wesley. All rights reserved / * 5 % 3 + -(-6) * (1 + 1) * 3 * / % 2 (1 + 2) * (3 + 1) * 2 - (6 * (7 + (8 * 2))) ( ) / ( ) * 10.0

© 2006 Pearson Addison-Wesley. All rights reserved A variable of primitive type names a memory location that stores one value. The variable represents this value. private int headCount; private int footCount; Example Declaration Example Instruction Sequence headCount = * 2; headCount footCount = headCount * 2; footCount headCount = headCount + 1; headCount++;

© 2006 Pearson Addison-Wesley. All rights reserved When two expressions of different numeric types are combined with an operation, the narrower type is automatically converted to match the wider and the operation matches the wider type. Types from Narrow to Wider Examples byte 17 / 3 short int long double float 17.0 / / 3.0

© 2006 Pearson Addison-Wesley. All rights reserved It is acceptable to assign an expression of one type to a variable of wider type. Examples (assume each variable name indicates its type) someDouble = 25 / 2.0; someInt = someByte; someFloat = 13 / 3; Such assignments are considered to be safe. someByte = 35.2;

© 2006 Pearson Addison-Wesley. All rights reserved Sometimes it is necessary to convert unsafely by narrowing Example (The second instruction below results in a compile-time error.) someDouble = 25.0; someInt = someDouble; Casting Syntax ( TargetType ) Expression Semantics A cast can be applied to many expressions to convert them to roughly equivalent values of a different type (called TargetType above). Three acceptable casts  cast for converting to a wider type  cast to convert double to float (some potential loss of accuracy)  cast to convert float or double to int someDouble = someInt = truncation means fractional portion eliminated

© 2006 Pearson Addison-Wesley. All rights reserved Casts have a higher precedence than +, - (subtraction), *, /, and %. Example (The instruction below results in a compile-time error.) someInt = (int) 2 * someDouble; Solution: Use parentheses OR someInt = (int)(2 * someDouble); someInt = 2 * (int)someDouble; Do these produce the same results?

© 2006 Pearson Addison-Wesley. All rights reserved Java permits a variable to be initialized by merging the declaration and assignment. Variable Initialization Examples int depth = ; The initialization must consist of a single assignment that can be executed at the time of declaration. private Oval dot = ; A variable declared as final can be assigned a value just once. final Variables Examples final int inchesPerFt = 12; A final variable is a good way to create a program-declared constant. private final Oval smallDot = new Oval(0, 0, 1, 1); Initializing a final variable when it is declared is wise.

© 2006 Pearson Addison-Wesley. All rights reserved Java uses parameter passage by __________. Example Method private void moveOvalTo( Oval a, int j) { j = j + 1; a.setLocation(j, j); } private void moveOvalTo( Oval a, int j) { j = j + 1; a.setLocation(j, j); } Sample Call circle = new Oval(10, 10, 100, 100); someInt = 40; moveOvalTo( circle, someInt ); When the method is called... 1) Each actual argument expression is evaluated. 2) A copy of the value of each argument expression is assigned to its formal parameter. (Thereafter, the formal parameter behaves like a local variable.) Reference type parameters are passed the same way. The “value” of the parameter is an object binding (i.e. the address of the actual argument object).

© 2006 Pearson Addison-Wesley. All rights reserved Write a method for each of the following. This method has an int parameter, p, and a double parameter, r, where p represents an initial amount of money (in cents) and r is an annual interest rate (r == 5.2 means 5.2% interest). Your method must return the value of this investment after four years of compounded interest. Write method with the following signature. private void printScores( int q1, int q2, int q3, int e1, int e2, int f) Where q1, q2 and q3 are raw quiz scores (out of 10 points), e1 and e3 are raw exam scores (out of 100 points) and f is the raw final exam score (out of 150 points). This method should print the percentage for each score as well as a total percentage for the class based on 10% from quizes, 60% from exams and 30% from the final.

© 2006 Pearson Addison-Wesley. All rights reserved Write a method for each of the following. Write method with the following signature. private void printAreas( Rectangle r1, Rectangle r2) Your method should print the area of each rectangle, along with the average area. (The average should be accurate to one decimal point.) Write method with the following signature. private double trianglePerimeter( int x1, int y1, int x2, int y2, int x3, int y3) where (x1, y1) and the coordinates of one vertex of a triangle, and the other two vertices are (x2, y2) and (x3, y3). This method should return the length of the perimeter for the triangle formed by these three vertices.