Primitive and Reference Data Values

Slides:



Advertisements
Similar presentations
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.
Advertisements

©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 3 Console Output Keyboard Input Numerical.
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this.
Chapter 2: Java Fundamentals Operators. Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 2 Content Group of Operators Arithmetic Operators Assignment.
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.
1 Fundamental Data Types. 2 Outline  Primitive Data Types  Variable declaration  Numbers and Constants  Arithmetic Operators  Arithmetic Operator.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 3 Console Output Keyboard Input Numerical.
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.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Basic Elements of C++ Chapter 2.
Objectives You should be able to describe: Data Types
 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 part #4 Operator
Characters In Java single characters are represented using the data type char. Character constants are written as symbols enclosed in single quotes, for.
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.
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 3: Numerical Data Manipulating Numbers Variables.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 3 Console Output Keyboard Input Numerical.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
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,
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Chapter 3 Numerical Data. Objectives After you have read and studied this chapter, you should be able to Select proper types for numerical data. Write.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
M105 - Week 2 Chapter 3 Numerical Data 1 Prepared by: M105 Team - AOU - SAB.
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.
Numeric Data Types There are six numeric data types: byte, short, int, long, float, and double.
1.  Algorithm: 1. Read in the radius 2. Compute the area using the following formula: area = radius x radius x PI 3. Display the area 2.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Primitive Types Four integer types: Two floating-point types:
Chapter Topics The Basics of a C++ Program Data Types
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
ITEC113 Algorithms and Programming Techniques
Basic Elements of C++.
Object Oriented Programming
Chapter 7: Expressions and Assignment Statements
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Primitive and Reference Data Values
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
Basic Elements of C++ Chapter 2.
IDENTIFIERS CSC 111.
Type Conversion, Constants, and the String Object
Pseudocode and Flowcharts
Chapter 8 Characters In Java single characters are represented using the data type char. Character constants are written as symbols enclosed in single.
Chapter 2: Basic Elements of Java
Chapter 2 Variables.
Expressions and Assignment
Chapter 2: Java Fundamentals
Primitive Types and Expressions
Operator King Saud University
Boolean Expressions September 1, 2019 ICS102: The course.
Presentation transcript:

Primitive and Reference Data Values byte short int double long float boolean String Applet MessageBox HiLo InputBox etc. char primitive reference Data Type A constant data value is represented graphically by a lock icon to convey the fact that the value is locked and cannot be changed. primitive variables contain values Reference variables point at objects

Numeric Data Types There are six numeric data types: byte, short, int, long, float, and double. Sample variable declarations: int numberOfStudents; float weight, height; long secondsPerLightYear; double expectedRateOfReturn; In the same way that you can initialize variables at the time you declare them, you can declare and create an object at the same time. For example, the declaration MainWindow mainWindow = new MainWindow(); is equivalent to MainWindow mainWindow; mainWindow = new MainWindow();

Primitive Data Declaration and Assignments int firstNumber, secondNumber; firstNumber = 234; secondNumber = 87; firstNumber secondNumber A. Variables are allocated in memory. B. Values are assigned to variables. 234 87 A int firstNumber, secondNumber; int firstNumber, secondNumber; firstNumber = 234; secondNumber = 87; B firstNumber = 234; secondNumber = 87; Code State of Memory

Assigning Numeric Data int number; number = 237; number = 35; number A. The variable is allocated in memory. B. The value 237 is assigned to number. 237 C. The value 35 overwrites the previous value 237. 35 int number; number = 237; number = 35; A int number; B number = 237; C number = 35; See the note page on the next slide. Code State of Memory

Numeric Data Types At the time a variable is declared, it also can be initialized. For example, we may initialize the integer variables count and height to 10 and 34 as int count = 10, height = 34; Ping … out to reality … NumericVariables.java In the same way that you can initialize variables at the time you declare them, you can declare and create an object at the same time. For example, the declaration MainWindow mainWindow = new MainWindow(); is equivalent to MainWindow mainWindow; mainWindow = new MainWindow();

Data Type Precisions The six data types differ in the precision of values they can store in memory.

Numeric Literal Values Integer types can be Positive or negative Decimal, e.g., -5653 Octal, e.g., 05653 (but not 09876) Hexidecimal, e.g., 0x5A1 Long, e.g., 14084591234L Real types can be Float, e.g., 1.23f Double, e.g., -2.34 Scientific, e.g., 2.17e-27f or -456.2345e19

Arithmetic Operators The following table summarizes the arithmetic operators available in Java. Operation In Java Example int x = 23; int y= 5; double z = 5.2; Addition + x + y 28 Subtraction - x - y 18 Multiplication * x * y 115 Integer division / x / y 4 Real division x / z 4.423 Modulo % x % y 3 Increment ++ x++ OR ++x x = 24 (but …) Decrement -- x-- OR --x x = 22 (but …)

Arithmetic Expressions Examples: sum = firstNumber + secondNumber; avg = (one + two + three) / 3.0; total++; Assignment operators, e.g., += /= etc. Tral-la-la, out to reality … NumericOperators.java You need to write the expression as (x + 3) * y if you want to multiply y to the sum of x and 3.

Arithmetic Expressions How does the expression x + 3 * y get evaluated? Answer: x is added to 3*y. We determine the order of evaluation by following the precedence rules. A higher precedence operator is evaluated before the lower one. If two operators are the same precedence, then they are evaluated left to right for most operators. You need to write the expression as (x + 3) * y if you want to multiply y to the sum of x and 3.

Precedence Rules -x(y + z) 4w How would be the result of the following expression? 4 * 3 / 2 + 5 Express the following formula in Java. -x(y + z) 4w

Example a * (b + -(c / d) / e) * (f - g % h) a * -b - (d + e * f) * (-g + h) a = 10; b = 5; c = 23; d = 4; e = 2; f = 5; g = 8; h = 3;

Example Program Flonk … out to reality … NumericPrecedence.java

Type Casting If x is a float and y is an int, what will be the data type of the following expression? x * y The answer is float. The above expression is called a mixed expression. The data types of the operands in mixed expressions are converted based on the promotion rules. The promotion rules ensure that the data type of the expression will be the same as the data type of an operand whose type has the highest precision. When an arithmetic expression consists of variables and constants of the same data type, then the result of the expression will be that data type also. When the data types of variables and constants in an arithmetic expression are of different data types, then a casting conversion will take place. A casting conversion, or type casting, is a process that converts a value of one data type to another data type. Two types of casting conversions in Java are implicit and explicit. An implicit conversion called numeric promotion is applied to the operands of an arithmetic operator.

Promotion Rules -x(y + z) 4w How would be the result of the following expression? 4 * 3 / 2 + 5 Express the following formula in Java. -x(y + z) 4w

Assignment Conversion If a lower precision value is assigned to a higher precision variable, type casting occurs Example double number; number = 25; is legal but int number; number = 23.45; is not

Explicit Type Casting Instead of relying on the promotion rules, we can make an explicit type cast by prefixing the operand with the data type using the following syntax: ( <data type> ) <expression> Example (float) x / 3 (int) (x / y * 3.0) Type case x to float and then divide it by 3. Type cast the result of the expression x / y * 3.0 to int.

Example Program Fuuuurtang … out to reality … NumericCasting.java

Variable and Constant Data Values Account SV129 Account A variable whose value can change over time. minimum balance 100.00 current balance 908.55 A constant whose value must remain fixed over time. account prefix 6427 opening balance 100.00 Constants are indicated by the final modifier Non-final public static data can give you warts Floorry … out to reality … MetricConversion.java A constant data value is represented graphically by a lock icon to convey the fact that the value is locked and cannot be changed.

Constants We can change the value of a variable. If we want the value to remain the same, we use a constant. final double PI = 3.14159; final int MONTH_IN_YEAR = 12; final short FARADAY_CONSTANT = 23060; These are constants, also called named constant. The reserved word final is used to declare constants. These are called literal constant. The data type of literal constant 3.45 is double and the data type of literal constant 398 is int. To designate other data types for literal constants, we suffix the literal constants with the designators F or f for float and L or l for long. For example, 2.45f 1453L

Use of Constants Very often class variables that are initialized May be private or public Local and instance variables may be declared final so they can be assigned a value only once Yabbadabba … out to reality … FinalVariables.java When an arithmetic expression consists of variables and constants of the same data type, then the result of the expression will be that data type also. When the data types of variables and constants in an arithmetic expression are of different data types, then a casting conversion will take place. A casting conversion, or type casting, is a process that converts a value of one data type to another data type. Two types of casting conversions in Java are implicit and explicit. An implicit conversion called numeric promotion is applied to the operands of an arithmetic operator.

The Math Class The Math class in the java.lang package includes many common and useful mathematical functions such sin, cos, tan, square root, exponentiation, and others. The mathematical formula is expressed in Java as Math.abs( Math.sin( Math.PI / 4.0) * x ) Splodge … out to reality … MathFunctions.java

Relational Operators Gluuub … out to reality MakeBooleans.java Chapter 6 Relational Operators < //less than <= //less than or equal to == //equal to != //not equal to > //greater than >= //greater than or equal to testScore < 80 testScore * 2 >= 350 30 < w / (h * h) x + y != 2 * (a + b) 2 * Math.PI * radius <= 359.99 One very common error in writing programs is mixing up the assignment and equality operators. We frequently make a mistake of writing if (x = 5) ... when we actually wanted to say if (x == 5) ... Gluuub … out to reality MakeBooleans.java Intro to OOP w/Java--Wu

Boolean Expressions and Variables Chapter 6 Boolean Expressions and Variables A B A && B A || B !A false true The AND operation results in true only if both A and B are true. The OR operation results in true if either A or B is true. The NOT operation is true if A is false and false if A is true. Combining boolean operators with relational and arithmetic operators, we can come up with a long boolean expression such as (x + 150) == y || x < y && !(y < z && z < x) In mathematics, we specify the range of values for a variable as 80  x < 90 In Java, to test that the value for x is within the specified lower and upper bounds, we express it as 80 <= x && x < 90 You cannot specify it as 80 <= x < 90 which is a syntax error because the relational operators (<, <=, etc.) are binary operators whose operands must be numerical values. Notice that the result of the subexpression 80 <= x is a boolean value, which cannot be compared to the numerical value 90. Their data types are not compatible. Bbbblarp … out to reality CombineBooleans.java Intro to OOP w/Java--Wu

Short-Circuit Evaluation Chapter 6 Short-Circuit Evaluation Consider x > y || x > z This is evaluated left to right. If x > y is true, then there’s no need to evaluate x > z because the whole expression will be true whether x > z is true or not. To stop the evaluation once the result of the whole expression is known is called short-circuit evaluation. What would happen if the short-circuit evaluation is not done for the following expression? z == 0 || x / z > 20 Doodlingting … out to reality ShortCircuit.java Intro to OOP w/Java--Wu

Conditional Expressions Chapter 6 Conditional Expressions Boolean values are used to form conditional expressions, whose value is determined by a Boolean expression. Lattteeee … out to reality Conditional.java Intro to OOP w/Java--Wu

Chapter 8 Characters In Java single characters are represented using the data type char. Character constants are written as symbols enclosed in single quotes, for example, 'a', 'X', and '5'. To represent characters in computer, U. S. computer manufacturers devised several schemes for encoding characters as integers. One coding scheme widely used today is ASCII (American Standard Code for Information Interchange). Intro to OOP w/Java--Wu

Chapter 8 ASCII Table For example, character 'O' is 79 (row value 70 + col value 9 = 79). O 9 70 Intro to OOP w/Java--Wu

Character Translation Chapter 8 Character Translation Declaration and initialization char ch1, ch2 = ‘X’; Type conversion between int and char. System.out.println("ASCII of X is " + (int) 'X' ); System.out.println("Char 88 is " + (char)88 ); Erf … out to reality ASCIITranslate.java Intro to OOP w/Java--Wu

Character Comparison Spling … out to reality CharOrder.java Chapter 8 Character Comparison This comparison returns true because ASCII value of 'A' is 65 while that of 'c' is 99. ‘A’ < ‘c’ Spling … out to reality CharOrder.java Intro to OOP w/Java--Wu

Special Characters These are also known as “escape characters” Chapter 8 Special Characters These are also known as “escape characters” Written using a \ prefix \t \n \b \g \’ \\ Spling … out to reality EscapeCharacters.java Intro to OOP w/Java--Wu

The Character Class The Character class in the java.lang package includes methods for manipulating character values. Example, use isLetter to check if a character is a-z or A-Z Zzzznicka … out to reality … CharGames.java Zzzznicka … out to reality … CharGamesCompressed.java

Chapter 8 Unicode To accommodate the character symbols of non- English languages, the Unicode Consortium established the Unicode Worldwide Character Standard, commonly known as Unicode. Intro to OOP w/Java--Wu