Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
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.
Numeric literals and named constants. Numeric literals Numeric literal: Example: A numeric literal is a constant value that appears in a Java program.
Expressions ► An expression can be a single variable, or can include a series of variables. If an expression includes multiple variables they are combined.
Constants and Variables  Memory cells used in program  Called constants and variables  Identifiers for constants and variables should be declared before.
1 9/17/07CS150 Introduction to Computer Science 1 Type Casting.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
CS150 Introduction to Computer Science 1
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
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.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
What is a variable?  A variable holds data in memory so the program may use that data, or store results.  Variables have a data type. int, boolean, char,
Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)
Expressions, Data Conversion, and Input
Floating point variables of different lengths. Trade-off: accuracy vs. memory space Recall that the computer can combine adjacent bytes in the RAM memory.
 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)
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
LESSON 6 – Arithmetic Operators
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 3A Integral Data (Concepts)
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory.
Programming with Visual C++: Concepts and Projects Chapter 3A: Integral Data (Concepts)
Integer numerical data types. The integer data types (multiple !) The integer data types use the binary number system as encoding method There are a number.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
1 Primitive Types n Four integer types:  byte  short  int (most common)  long n Two floating-point types:  float  double (most common) n One character.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
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.
1 Section 3.2b Arithmetic Operators Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
Chapter 2 Elementary Programming
Lecture 3: Operators, Expressions and Type Conversion
Chapter 4 – Fundamental Data Types
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
Operators and Expressions
Lecture 3 Expressions Richard Gesick.
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
Numerical Data Types.
Chapter 2: Basic Elements of Java
Arithmetic Expressions & Data Conversions
C Operators, Operands, Expressions & Statements
Data Types and Expressions
Chapter 2 Primitive Data Types and Operations
Operator King Saud University
Data Types and Expressions
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

Integer numerical data types

The integer data types The integer data types use the binary number system as encoding method There are a number of different integer types in Java

Integer operators Integer operators are arithmetic operators that manipulate (operate on) integer values A integer operator only operates on integer values The result of an integer operator is always an integer value

Priority and associativity of the integer arithmetic operators

The integer data types The computer can only perform operations on operands of the same date type: Correct incorrect

Automatic conversions in Integer arithmetic All values are converted to int type before an arithmetic operation (+, −, *, /, %) in performed. If one of the values in an arithmetic operation is long, then all values are converted to long type before the arithmetic operation in performed.

Safe Conversions Safe conversions: Unsafe conversions:

Automatic type conversion in the assignment operation The safe integer conversions are:

Summary of automatic conversion rules (1) Integers: When an arithmetic operation contains 2 integer values, convert any lower precision integer type to int type When an arithmetic operation contains a long value, convert any lower precision integer type to long type Floating point numbers (float and double): When an arithmetic operation contains 2 floating point values, convert any lower precision float type to double type

Summary of automatic conversion rules (2) When an arithmetic operation contains one floating point value and one integer value: convert the integer value to double convert the lower precision float type to double type

Summary of automatic conversion rules (3) If the range of values of type1 contains the range of values of type2, then Java will perform an automatic promotion from type2 ⇒ type1 for the assignment operation

Overflow

Reading integer input from the keyboard

Numeric literals (1) A numeric literal is a constant value that appears in a Java program. Every numerical literal (constant) has a data type

Numeric literals (2) Special tags that change the data type of a constant long tag: The tag L or l after an integer literal will change the data type of the literal to longExample: 12345L has the type long The float tag: The tag F or f after a decimal literal will change the data type of the literal to floatExample: f has the type float

Assigning integer constants to byte and short variables (1) Strictly speaking, you need to use casting operators:

Assigning integer constants to byte and short variables (2) If an integer literal (constant) is assigned to a byte typed or short typed variable, the literal (constant) is automatically converted into the appropriate type if the constant is within the range of the data type of the variable. Example: The range of the byte type is −

The assignment expressions (1) Result of var = expr is equal to the value of expr In addition to updating the variable. the assignment operator also returns a value

The assignment expressions (2) When there are multiple assignment operators in an expression, the expression is evaluate from right to left

Assignment statements with the same variable on the LHS and RHS The symbol "=" denotes an assignment operation: 1. The computer will first evaluate the RHS "x + 4.0": RHS = x (x contains 1.0) = = Then the result 5.0 is assigned to the receiving variable x: x = 5.0; Therefore, after executing the statement "x = x + 4.0", the variable x contains the value 5.0

Shorthand operators A shorthand operator is a shorter way to express something that is already available in the Java programming language

The ++ and -- operators (1) Pre-operation: the ++ and -- operator appear before the variable Post-operation: the ++ and -- operator appear after the variable Both operations (++var and var++) will increment the variable var by 1 The only difference between ++var and var++ is: the value that is returned by the expression.

The ++ and -- operators (2) The pre-operations ++a and --a will: Apply the increment/decrement operation before (pre) returning the value in the variable a The post-operations a++ and a-- will: Apply the increment/decrement operation after (pre) returning the value in the variable a

The ++ and -- operators (3)