false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Similar documents
false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87

IEEE Floating-Point Representation 1

Zheng-Liang Lu Java Programming 45 / 79

Java Programming. U Hou Lok. Java Aug., Department of Computer Science and Information Engineering, National Taiwan University

Logic is the anatomy of thought. John Locke ( ) This sentence is false.

Java Programming. Zheng-Liang Lu. Java 304 Fall Department of Computer Science & Information Engineering National Taiwan University

Week 3 Lecture 2. Types Constants and Variables

Values in 2 s Complement

AP Computer Science A

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Accelerating Information Technology Innovation

Chapter 3: Operators, Expressions and Type Conversion

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

Chapter 2 Elementary Programming

Full file at

Computer System and programming in C

CEN 414 Java Programming

JAVA Programming Fundamentals

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Getting started with Java

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Program Fundamentals

Full file at

COMP Primitive and Class Types. Yi Hong May 14, 2015

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Datatypes, Variables, and Operations

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

ARG! Language Reference Manual

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Basic data types. Building blocks of computation

PROGRAMMING FUNDAMENTALS

AP Computer Science A: Java Programming

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

CS2141 Software Development using C/C++ C++ Basics

Programming in C++ 6. Floating point data types

CS Programming In C

Programming with Java

Arithmetic Compound Assignment Operators

Important Java terminology

egrapher Language Reference Manual

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

COMP6700/2140 Data and Types

Inf2C - Computer Systems Lecture 2 Data Representation

Values, Variables, Types & Arithmetic Expressions. Agenda

1 class Lecture5 { 2 3 "Methods" / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 0 / Zheng-Liang Lu Java Programming 176 / 199

Visual C# Instructor s Manual Table of Contents

COMP2611: Computer Organization. Data Representation

Lecture Set 4: More About Methods and More About Operators

Computer Science II (20082) Week 1: Review and Inheritance

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

More about Binary 9/6/2016

Programming Lecture 3

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Annotation Annotation or block comments Provide high-level description and documentation of section of code More detail than simple comments

Chapter 6 Primitive types

Values and Variables 1 / 30

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

VARIABLES AND TYPES CITS1001

Chapter 2. Elementary Programming

Chapter 2 ELEMENTARY PROGRAMMING

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

printf( Please enter another number: ); scanf( %d, &num2);

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

Chapter 2: Using Data

Declaration and Memory

3. Java - Language Constructs I

Motivations. Chapter 2: Elementary Programming 8/24/18. Introducing Programming with an Example. Trace a Program Execution. Trace a Program Execution

Chapter 02: Using Data

Binary. Hexadecimal BINARY CODED DECIMAL

CS313D: ADVANCED PROGRAMMING LANGUAGE

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

Nested Loops. A loop can be nested inside another loop.

Basics of Java Programming

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

COMP2121: Microprocessors and Interfacing. Number Systems

1.3b Type Conversion

Bits, Words, and Integers

Chapter 2 Primitive Data Types and Operations. Objectives

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Introduction to Programming EC-105. Lecture 2

Chapter 2 Elementary Programming. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

COMP-202: Foundations of Programming. Lecture 5: More About Methods and Data Types Jackie Cheung, Winter 2016

CPSC 3740 Programming Languages University of Lethbridge. Data Types

Transcription:

1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4 5 } 6 7 // Keywords: 8 byte, short, int, long, char, float, double, boolean, true, false, import, new Zheng-Liang Lu Java Programming 44 / 78

Example Given the radius of a circle, say 10, determine the area. Recall that a program comprises data and algorithms. How to store the data? variables, data types How to compute the area? arithmetic operators How to show the result? System.out.println() Zheng-Liang Lu Java Programming 45 / 78

1 public class ComputeArea { 2 public static void main(string[] args) { 3 // input 4 int r = 10; 5 // algorithm 6 double area = r r 3.14; 7 // output 8 System.out.println(area); 9 } 10 } The type int and double are two of primitive data types. We use two variables r and area. Zheng-Liang Lu Java Programming 46 / 78

Variable Box Zheng-Liang Lu Java Programming 47 / 78

Variable Declaration You give a name for the variable, say x. Additionally, you need to assign a type for the variable. For example, 1... 2 int x; // x is a variable declared an interger type. 3... Variable declaration tells the compiler to allocate appropriate memory space for the variable based on its data type. 1 It is worth to mention that, the date type determines the size, which is measured in bytes 2. 1 Actually, all declared variables are created at the compile time. 2 1 byte = 8 bits; bit = binary digit. Zheng-Liang Lu Java Programming 48 / 78

Naming Rules Identifiers are the names that identify the elements such as variables, methods, and classes in the program. The naming rule excludes the following situations: cannot start with a digit cannot be any reserved word 3 cannot include any blank between letters cannot contain +,,, / and % Note that Java is case sensitive 4. 3 See the next page. 4 The letter A and a are different. Zheng-Liang Lu Java Programming 49 / 78

Reserved Words 5 5 See Appendix A in YDL, p. 1253. Zheng-Liang Lu Java Programming 50 / 78

Variable as Alias of Memory Address The number 0x000abc26 stands for one memory address in hexadecimal (0-9, and a-f). 6 The variable x itself refers to 0x000abc26 in the program after compilation. 6 See https://en.wikipedia.org/wiki/hexadecimal. Zheng-Liang Lu Java Programming 51 / 78

Data Types Java is a strongly typed 7 programming language. Every variable has a type. Also, every (mathematical) expression has a type. There are two categories of data types: primitive data types, and reference data types. 7 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 78

Primitive Data Types 8 8 See Figure 3-4 in Sharan, p. 67. Zheng-Liang Lu Java Programming 53 / 78

Integers The most commonly used integer type is int. If the integer values are larger than its feasible range, then a overflow occurs. Zheng-Liang Lu Java Programming 54 / 78

Floating Points Floating points are used when evaluating expressions that require fractional precision. For example, sin(), cos(), and sqrt(). The performance for the double values is actually faster than that for float values on modern processors that have been optimized for high-speed mathematical calculations. Be aware that floating-point arithmetic can only approximate real arithmetic. 9 (Why?) 9 See https://en.wikipedia.org/wiki/numerical_error. Zheng-Liang Lu Java Programming 55 / 78

Example: 0.5-0.1-0.1-0.1-0.1-0.1 = 0? 1 public class FloatingPointsDemo { 2 public static void main(string args[]) { 3 System.out.println(0.5 0.1 0.1 0.1 0.1 0.1); 4 } 5 } The result is surprising. (Why?) You may try this decimal-binary converter. This issue is not only associated decimal numbers, but also big integers. 10 So double values are not reliable if the program runs for high-precision calculation. 10 Thanks to a lively discussion on June 26, 2016. Zheng-Liang Lu Java Programming 56 / 78

Example: Loss of Significance For example, 1... 2 System.out.println(3.14 + 1e20 1e20); // output? 3 System.out.println(3.14 + (1e20 1e20)); // output? 4... Can you explain why? Zheng-Liang Lu Java Programming 57 / 78

IEEE Floating-Point Representation 11 x = ( 1) s M 2 E The sign s determines whether the number is negative (s = 1) or positive (s = 0). The significand M is a fractional binary number that ranges either between 1 and 2 ɛ, or between 0 and 1 ɛ. The exponent E weights the value by a (possibly negative) power of 2. 11 William Kahan (1985); Aka IEEE754. Zheng-Liang Lu Java Programming 58 / 78

Illustration 12 That is why we call a double value. 12 See Figure 2-31 in Byrant, p. 104. Zheng-Liang Lu Java Programming 59 / 78

Assignments An assignment statement designates a value to the variable. 1 int x; // make a variable declaration 2... 3 x = 1; // assign 1 to x The equal sign (=) is used as the assignment operator. For example, is the expression x = x + 1 correct? Direction: from the right-hand side to the left-hand side To assign a value to a variable, you must place the variable name to the left of the assignment operator. 13 For example, 1 = x is wrong. 1 cannot be resolved to a memory space. 13 x can be a l-value and r-value, but 1 and other numbers can be only r-value but not l-value. See Value. Zheng-Liang Lu Java Programming 60 / 78

Two Before Rules Every variable has a scope. The scope of a variable is the range of the program where the variable can be referenced. 14 A variable must be declared before it can be assigned a value. In practice, do not declare the variable until you need it. A declared variable must be assigned a value before it can be used. 15 14 The detail of variable scope is introduced later. 15 In symbolic programming, such as Mathematica and Maple, a variable can be manipulated without assigning a value. For example, x + x returns 2x. Zheng-Liang Lu Java Programming 61 / 78

Arithmetic Operators 16 Note that the operator depends on the operands involved. 16 See Table 2-3 in YDL, p. 46. Zheng-Liang Lu Java Programming 62 / 78

Tricky Pitfalls Can you explain this result? 1... 2 double x = 1 / 2; 3 System.out.println(x); // output? 4... Revisit 0.5 0.1 0.1 0.1 0.1 0.1 0.1 = 0. 17 1... 2 System.out.println(1 / 2 1 / 10 1 / 10 1 / 10 1 / 10 1 / 10); // output 0; however, this is not the real solution to the original problem. 3... 17 Thanks to a lively discussion on on June 7, 2016. Zheng-Liang Lu Java Programming 63 / 78

Type Conversion and Compatibility If a type is compatible to another, then the compiler will perform the conversion implicitly. For example, the integer 1 is compatible to a double value 1.0. However, there is no automatic conversion from double to int. (Why?) To do so, you must use a cast, which performs an explicit conversion for compilation. Similarly, a long value is not compatible to int. Zheng-Liang Lu Java Programming 64 / 78

Casting 1... 2 int x = 1; 3 double y = x; // compatible; implicit conversion 4 x = y; // incompatible; need an explicit conversion by casting 5 x = (int) y; // succeed!! 6... Note that the Java compiler does only type-checking but no real execution before compilation. In other words, the values of x and y are unknown until they are really executed. Zheng-Liang Lu Java Programming 65 / 78

Type Conversion and Compatibility (concluded) small-size types large-size types small-size types large-size types (need a cast) simple types complicated types simple types complicated types (need a cast) Zheng-Liang Lu Java Programming 66 / 78

Characters A character stored by the machine is represented by a sequence of 0 s and 1 s. For example, ASCII code. (See the next page.) The char type is a 16-bit unsigned primitive data type. 18 18 Java uses Unicode to represent characters. Unicode defines a fully international character set that can represent all of the characters found in all human languages. Zheng-Liang Lu Java Programming 67 / 78

ASCII (7-bit version) Zheng-Liang Lu Java Programming 68 / 78

Example Characters can also be used as an integer type on which you can perform arithmetic operations. 19 For example, 1... 2 // A single quoted value is the char type. 3 char x = a ; 4 System.out.println(x + 1); // output 98!! 5 System.out.println((char)(x + 1)); // output b 6... Notice that a double-quoted string is a String object, which can have more char values. 19 Widely used in information security! See https://en.wikipedia.org/wiki/cryptography. Zheng-Liang Lu Java Programming 69 / 78

Boolean Values The program is supposed to do decision making by itself, for example, Google Driverless Car. 20 To do this, Java has the boolean-type flow controls (selections and iterations). Only two possible values, true and false. Note that a boolean value cannot be cast into a value of another type, nor can a value of another type be cast into a boolean value. 20 See https://www.google.com/selfdrivingcar/. Zheng-Liang Lu Java Programming 70 / 78

Rational Operators 21 These operators take two operands. Rational expressions return a boolean value. Note that the equality comparison operator is double equality sign (==), not single equality sign (=). 21 See Table 3-1 in YDL, p. 82. Zheng-Liang Lu Java Programming 71 / 78

Example 1... 2 int x = 2; 3 boolean a = x > 1; 4 boolean b = x < 1; 5 boolean c = x == 1; 6 boolean d = x!= 1; 7 boolean e = 1 < x < 3; // sorry? 8... Be aware that e is logically correct but syntactically wrong. Usually, the boolean expression consists of a combination of rational expressions. For example, 1 < x < 3 should be (1 < x)&&(x < 3), where && refers to the AND operator. Zheng-Liang Lu Java Programming 72 / 78

Logical Operators 22 22 See Table 3-2 in YDL, p. 102. Zheng-Liang Lu Java Programming 73 / 78

Truth Table Let X and Y be two Boolean variables. Then the truth table for logical operators is as follows: X Y!X X&&Y X Y X Y T T F T T F T F F F T T F T T F T T F F T F F F Note that the instructions of computers, such as arithmetic operations, are implemented by logic gates. 23 23 See any textbook for digital circuit design. Zheng-Liang Lu Java Programming 74 / 78

Logic is the anatomy of thought. John Locke (1632 1704) This sentence is false. I know that I know nothing. anonymous Plato (In Apology, Plato relates that Socrates accounts for his seeming wiser than any other person because he does not imagine that he knows what he does not know.) Zheng-Liang Lu Java Programming 75 / 78

Arithmetic Compound Assignment Operators Note that these shorthand operators are not available in languages such as Matlab, R, and Python. Zheng-Liang Lu Java Programming 76 / 78

Example 1... 2 int x = 1; 3 System.out.println(x); // output 1 4 x = x + 1; 5 System.out.println(x); // output 2 6 x += 2; 7 System.out.println(x); // output 4 8 x++; // equivalent to x += 1 and x = x + 1 9 System.out.println(x); // output 5 10... Zheng-Liang Lu Java Programming 77 / 78

The compound assignment operators are also useful for char values. 24 For example, 1... 2 char s = a ; 3 System.out.println(s); // output a 4 s += 1; 5 System.out.println(s); // output b 6 s++; 7 System.out.println(s); // output c 8... 24 Contribution by Mr. Edward Wang (Java265) on May 1, 2016. Zheng-Liang Lu Java Programming 78 / 78