BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

Types and Arithmetic Operators
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
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.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
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.
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.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
1 MATERI PENDUKUNG OPERATOR Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
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)
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
Building Java Programs Primitive Data and Definite Loops.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Java Programming, Second Edition Chapter Two Using Data Within a Program.
COMP Primitive and Class Types Yi Hong May 14, 2015.
COM S 207 Literal, Operator, and Expression Instructor: Ying Cai Department of Computer Science Iowa State University
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
CompSci Primitive Types  Primitive Types (base types)  Built-in data types; native to most hardware  Note: not objects (will use mostly first.
Doing math In java.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Topic 4 Expressions and variables Based on slides bu Marty Stepp and Stuart Reges from "Once a person has understood.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
1 2. Program Construction in Java. 01 Java basics.
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.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
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.
Chapter 2 Basic Computation
Building Java Programs
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Primitive Data, Variables, Loops (Maybe)
Type Conversion, Constants, and the String Object
Chapter 2 Basic Computation
Arithmetic Operator Operation Example + addition x + y
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Chapter 2 Programming Basics.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Primitive Types and Expressions
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Introduction to Java, and DrJava
Building Java Programs
Building Java Programs
Building Java Programs
Primitive Data Types and Operators
Building Java Programs
Presentation transcript:

BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS

22 OBJECTIVES Identify Java’s primitive data types and operators. Evaluate complex expressions using rules of precedence and mixed types.

33 DATA TYPES A name for a category of data values that are all related. Different data types support different operations and behave differently. You can multiply numbers, but can you multiply strings? Can you add them? Stored differently in computer memory as well. 27 = “hello” =

44 JAVA’S PRIMITIVE DATA TYPES NameDescriptionExample int integers (whole numbers) 42, -3, 18, 20493, 0 double real numbers 7.35, , 18.0 char single characters 'a', 'X', '3', '\n' boolean logical values true, false Variations of int: byte, short, long Variations of double: float

55 JAVA’S ARITHMETIC OPERATORS OperatorMeaningExampleResult + addition – subtraction 53 – 1835 * multiplication 3 * 824 / division 4.8 / % mod (remainder) 19 % 54 Most behave exactly like you would expect them to in math class, but we’ll learn about the exceptions soon.

66 EXPRESSIONS An expression is a simple value or a set of operations that produces a value. Simplest expressions are literals of the data types we know. E.g. 27, “Seattle... Yay!", -1.7, false More complex expressions can use operators and parentheses. E.g.: 5 * * (1.3 – (5.7 – 3)) System.out.println( 5 * 2 / );

77 FLOATING-POINT DIVISION When dividing double or float values, it’s just like math class, but with some rounding errors here and there… 5.0 / 2.0 = / 3.0 = (rounding error!) 4.0 / 5.0 = / 3.0 * 3.0 = 8.0

88 INTEGER DIVISION… WEIRD… When dividing integers, we keep the whole part, but discard the fractional part. 5 / 2 = 2 not / 3 = 3 not 3.333… 4 / 5 = 0 not / 3 * 3 = ?2 * 3 =? 6 not 8!

99 INTEGER MOD ( % ) The mod operator computes the remainder of a division 6 / 4 = 1, but it would have had a remainder 2 Therefore, 6 % 4 = 2 20 % 7 =15 % 4 = % 2 =13856 % 2 = 8374 % 10 =8374 % 100 = 36 % 6 =7 % 9 =

10 In your notebook… 1. Write four expressions using only % and / that will get me the four individual digits of a b c d *** Will your expressions work for other four-digit numbers? *** / 1000 % 10 / 100 % 10 / 10 % 10 THINK, PAIR, SHARE… = 2 = 8 = 3 = 7 ????? % 10 ?????

11 OPERATOR PRECEDENCE Usually, we evaluate expressions left-to-right, but certain operations take precedence over others and get evaluated first. Parentheses can always override precedence. Precedence table: DescriptionOperators unary operators +, - multiplicative operators *, /, % additive operators +, -

12 PRECEDENCE EXAMPLES * 5 = 12not 20 3 * * 2 = 13not / 2 * 3 – 4 = 9not * -4 = = = 7

13 MIXING TYPES When doing an operation on an int and a double, the int gets promoted to a double. 1 * = / 2.0 = * 1.0 / 2 = / 2 – 7 / 2 = ? 3.5 – 3 =? 0.5

14 CASTING You can explicitly convert a value from one data type to another by casting. (double) 99 = 99.0 (int) 2.5 = 2 ((double) 7) / 2 = 3.5 (int) 2.5 * 3.0 = 6.0 (int) (2.5 * 3.0) = 7

15 IN YOUR NOTEBOOK… 4.0 / 2 * 9 / * 9 / / 2 9.0

16 IN YOUR NOTEBOOK… 12 / 7 * 4.4 * 2 / 4 1 * 4.4 * 2 / * 2 / / 4 2.2

17 IN YOUR NOTEBOOK… 9 / / 3 – 3.0 / / 3 – 3.0 / – 3.0 / – –

18 IN YOUR NOTEBOOK… 53 / 5 / ( ) / / 2 53 / 5 / 2.0 / / 2 10 / 2.0 / / / / /