Zheng-Liang Lu Java Programming 45 / 79

Similar documents
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.

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

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

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

Values in 2 s Complement

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

Chapter 2 Elementary Programming

Full file at

Chapter 2 ELEMENTARY PROGRAMMING

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

JAVA Programming Fundamentals

Chapter 3: Operators, Expressions and Type Conversion

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

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

CEN 414 Java Programming

More Programming Constructs -- Introduction

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

Getting started with Java

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 2 Primitive Data Types and Operations. Objectives

Computer System and programming in C

JAVA OPERATORS GENERAL

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Elementary Programming

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

CMPT 125: Lecture 3 Data and Expressions

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

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

DATA TYPES AND EXPRESSIONS

Visual C# Instructor s Manual Table of Contents

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

A Java program contains at least one class definition.

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

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

Datatypes, Variables, and Operations

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

Unit 3. Constants and Expressions

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Chapter 2 Primitive Data Types and Operations

Values and Variables 1 / 30

CIS133J. Working with Numbers in Java

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Chapter 2 Primitive Data Types and Operations

MODULE 02: BASIC COMPUTATION IN JAVA

Java Programming Language. 0 A history

Chapter 2: Data and Expressions

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

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

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Java Notes. 10th ICSE. Saravanan Ganesh

Chapter 2: Data and Expressions

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

COMP6700/2140 Data and Types

Work relative to other classes

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

Week 3 Lecture 2. Types Constants and Variables

Java Programming Fundamentals. Visit for more.

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Chapter 2. Elementary Programming

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

Language Reference Manual simplicity

ARG! Language Reference Manual

Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes

Programming with Java

Basic data types. Building blocks of computation

3. Java - Language Constructs I

CS-201 Introduction to Programming with Java

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

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

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Basics of Java Programming

UNIT 7A Data Representation: Numbers and Text. Digital Data

Chapter 2: Using Data

Introduction to Programming Using Java (98-388)

CIS 110: Introduction to Computer Programming

CS112 Lecture: Primitive Types, Operators, Strings

Declaration and Memory

Chapter 6 Primitive types

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

Chapter 02: Using Data

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

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

Chapter 2: Using Data

Operators and Expressions

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

Important Java terminology

Exercises Software Development I. 05 Conversions and Promotions; Lifetime, Scope, Shadowing. November 5th, 2014

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

Chapter 2: Data and Expressions

Java Identifiers, Data Types & Variables

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

Reserved Words and Identifiers

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

Eng. Mohammed S. Abdualal

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Transcription:

1 class Lecture2 { 2 3 "Elementray Programming" 4 5 } 6 7 / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch. 2 in HS 11 / Zheng-Liang Lu Java Programming 45 / 79

Example Given a radius 10, determine the area of the circle. Recall that a program has data and algorithms. How to store the data? Variable declaration, primitive date type, assignment operator How to compute the area? Arithmetic operators How to show the result? System.out.println() Zheng-Liang Lu Java Programming 46 / 79

Given a radius 10, determine the area of the circle. 1 public class ComputeArea { 2 public static void main (String[] args) { 3 int r = 10; 4 double area = r r 3.1415926; 5 System.out.println("The area for the circle of radius " 6 + r + " is " + area + "."); 7 } 8 } int and double are two of primitive data types. r and area are two variables. The string, which is double-quoted, can be concatenated by simply adding a plus (+) sign. 1 1 String class is very useful in Java because of its fruitful methods. Zheng-Liang Lu Java Programming 47 / 79

Variable as Container Zheng-Liang Lu Java Programming 48 / 79

Variable Declaration Variable declaration tells the compiler to allocate appropriate memory space for the variable based on its data type. Assign a memory address to hold the value Give an identifier (variable name) Refer 2 to the memory address when being fetched Determine the size, which is measured in bytes, to store the value depending on the date type For example, myvar is declared as integer type. 1... 2 int myvar; // int refers to integer type 3... 2 In compilation stage. Zheng-Liang Lu Java Programming 49 / 79

Variable in Memory 1... 2 int x; // x is declared as an int type variale 3... 0x000abc26 stands for a memory address in hexadecimal 3. x itself refers to 0x000abc26 in the program after compilation. 3 0-9, A-F. Zheng-Liang Lu Java Programming 50 / 79

Values in 2 s Complement Java uses an encoding known as 2 s complement 4, which means that negative numbers are represented by inverting 5 all of the bits in a value, then adding 1 to the result. For example, 42 10 = 00101010 2. Then 42 10 = 11010110 2 can be derived from inverting 42 10 to 11010101 2 plus 1. 4 In 1 s complement, simply inverting all of the bits creates 11111111, which creates negative zero. The trouble is that negative zero is invalid in integer math. 5 Changing 1 s to 0 s and vice versa. Zheng-Liang Lu Java Programming 51 / 79

Identifiers Identifiers are the names that identify the elements such as variables, methods, and classes in a program. All identifiers must obey the following rules: a sequence of characters that consists of letters, digits, and underscores ( ) start with a letter or an underscore ( ) can be of any length cannot start with a digit cannot be a reserved word cannot be true, false, or null cannot contain +,,, and % Note that Java is case sensitive 6. 6 A and a are different. Zheng-Liang Lu Java Programming 52 / 79

Reserved Words 7 7 See Appendix A in YDL, p. 1253. Zheng-Liang Lu Java Programming 53 / 79

When To Declare Variables: Two Before Rules Every variable has a scope. The scope of a variable is the part of the program where the variable can be referenced. A variable must be declared before it can be assigned a value. A declared variable must be assigned a value before it can be used. 8 The detail of variable scope is introduced later. 8 In symbolic programming, such as Mathematica and Maple, a variable can be manipulated without assigning a value. Zheng-Liang Lu Java Programming 54 / 79

Data Types Java is a strongly typed programming language. Every variable has a type. Every expression has a type. Every type is strictly defined. All assignments are checked for type compatibility. 9 There are two categories of date types: primitive data types, referential data types. 9 By Java compiler. Zheng-Liang Lu Java Programming 55 / 79

Primitive Data Types 10 10 See Figure 3-4 in Sharan, p. 67. Zheng-Liang Lu Java Programming 56 / 79

Integers The most commonly used integer type is int. long is a signed 64-bit type and is useful for those occasions where an int type is not large enough to hold the desired value. Note that when byte and short values are used in an expression, they are promoted to int when the expression is evaluated. 11 11 Due to the first bullet. Zheng-Liang Lu Java Programming 57 / 79

Floating Points Floating-point numbers, also known as real numbers, are used when evaluating expressions that require fractional precision. All transcendental math functions, such as sin(), cos(), and sqrt(), return double values. Double precision is actually faster than single precision on some modern processors that have been optimized for high-speed mathematical calculations. Be aware that floating-point arithmetic can only approximate real arithmetic. (Why?) Zheng-Liang Lu Java Programming 58 / 79

Example: 0.5-0.1-0.1-0.1-0.1-0.1 = 0? 1 public class displaytime { 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? Decimal-binary converter Zheng-Liang Lu Java Programming 59 / 79

IEEE Floating-Point Representation 12 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. 12 William Kahan (1985) Zheng-Liang Lu Java Programming 60 / 79

Illustration 13 13 See Figure 2-31 in Byrant, p. 104. Zheng-Liang Lu Java Programming 61 / 79

Assginment Statement An assignment statement designates a value for a variable, by the following syntax: 1... 2 myvar = expression; 3... The equal sign (=) is used as the assignment operator. For example, is the expression x = x + 1 correct? From the right-hand side to the left-hand side An expression represents a computation involving values, variables, and operators that evaluates to a value. To assign a value to a variable, you must place the variable name to the left of the assignment operator. 14 For example, 1 = x is wrong. 14 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 62 / 79

Variable Initialization Initialization once declaration 1... 2 int x = 1, y = 2; // Equivalent to int x, y; x = 1; y = 2; 3... Dynamical initialization 1... 2 double x, y = 3.0; 3 x = y; 4... y is assigned in the compilation stage; x is assigned in the runtime stage. Zheng-Liang Lu Java Programming 63 / 79

Numeric Operators 15 Note that when both operands of a division are integers, the result of the division is an integer and the fractional part is truncated. So, the operator depends on the operands. 15 See Table 2-3 in YDL, p. 46. Zheng-Liang Lu Java Programming 64 / 79

Example 1... 2 int a, b; 3 double c,d; 4 a = c = b = d = 3 + 7 / 2.; 5... a =? b =? c =? d =? Zheng-Liang Lu Java Programming 65 / 79

Type Conversion If the two types are compatible, then Java will perform the conversion automatically. However, not all types are compatible, and thus, not all type conversions are implicitly allowed. The two types are compatible. The destination type is larger than the source type. For example, there is no automatic conversion defined from double to byte. (Why?) To do so, you must use a cast, which performs an explicit conversion between incompatible types. Zheng-Liang Lu Java Programming 66 / 79

Casting 1 public class test { 2 public static void main (String[] args) { 3 double x = 1.234; 4 int y; 5 y = (int) x; // (target type) 6 System.out.println("y = " + y); 7 } 8 } Target-type specifies the desired type to convert the specified value to. In this example, a different type of conversion will occur when a floating-point value is assigned to an integer type, as known as, truncation. Zheng-Liang Lu Java Programming 67 / 79

Example 1 public class promote { 2 public static void main(string[] args) { 3 byte b = 42; 4 char c = a ; // single quote for a letter, a > 97 5 short s = 1024; 6 int i = 50000; 7 float f = 5.67; 8 double d =.1234; 9 double result = (f b) + (i / c) (d s); 10 System.out.println((f b) + " + " + (i / c) + " " + 11 (d s) + " = " + result); 12 } 13 } (i/c) =? How to correct? Zheng-Liang Lu Java Programming 68 / 79

Type Promotion (Concluded) Type promotion rules are as follows: All byte, short, and char values are promoted to int. If one operand is a long, the whole expression is promoted to long. Similarly, if one is float, then promoted to float. Also, if any of the operands are double, then promoted to double. Zheng-Liang Lu Java Programming 69 / 79

Characters A character is stored in a computer as a sequence of 0s and 1s. The char data type is a 16-bit unsigned Java primitive data type. 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 70 / 79

ASCII (7-bit version) Zheng-Liang Lu Java Programming 71 / 79

Example Characters can also be used as an integer type on which you can perform arithmetic operations. For example, 1... 2 char x = a ; Cannot be "a" 3 System.out.println(x + 1); 4 System.out.println((char)(x + 1)); 5 System.out.println(x + "1"); 6 System.out.println("This is " + x + 1); 7 System.out.println("This is " + (x + 1)); 8... x =? Note that the plus (+) operator is overloaded. Zheng-Liang Lu Java Programming 72 / 79

Character Escape Sequences 16 16 See Table 3-1 in HS, p. 44. Zheng-Liang Lu Java Programming 73 / 79

Boolean Java has a primitive type, called boolean, for logical values. It can have only one of two possible values, true or false. boolean is also the type required by the conditional expressions that govern the control statements such as if and for. 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. Zheng-Liang Lu Java Programming 74 / 79

Rational Operators 17 They are all binary operators. Rational expressions return Boolean values. Note that the equality comparison operator is two equal signs (==), not a single equal sign (=), which is the assignment operator. 17 See Table 3-1 in YDL, p. 82. Zheng-Liang Lu Java Programming 75 / 79

Example 1... 2 a = 4 > 3; 3 b = 4 < 3; 4 c = 4 == 3; 5 d = 4!= 3; 6 e = 4 > 3 > 2; 7... a =? b =? c =? d =? e =? Be aware that e is logically correct but syntaxically wrong. Zheng-Liang Lu Java Programming 76 / 79

Logical Operators 18 Sometimes, whether a statement is executed is determined by a combination of several conditions. For example, 4 > 3 > 2 should be (4 > 3)&&(3 > 2) in the program, where && refers to the and operator. 18 See Table 3-2 in YDL, p. 102. Zheng-Liang Lu Java Programming 77 / 79

Arithmetic Compound Assignment Operators += ( =) is called the addition (subtraction) assignment operator. The increment (++) and decrement ( ) operators are for incrementing and decrementing a variable by 1. Zheng-Liang Lu Java Programming 78 / 79

Example 1... 2 int a = 1; 3 System.out.println(a+=1); // a = a + 1 4 System.out.println(a++); // a = a + 1 5 System.out.println(++a); // a = a + 1 6 System.out.println( a); // a = a 1 7 System.out.println(a ); // a = a 1 8... What are the results? Zheng-Liang Lu Java Programming 79 / 79