Chapter 6 Primitive types

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

Chapter 2: Data and Expressions

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Chapter 2: Data and Expressions

More Programming Constructs -- Introduction

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

Basics of Java Programming

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

CMPT 125: Lecture 3 Data and Expressions

Declaration and Memory

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Computer System and programming in C

Chapter 2: Data and Expressions

Getting started with Java

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 2: Using Data

Java Basic Datatypees

ECE 122 Engineering Problem Solving with Java

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

JAVA Programming Fundamentals

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

JAVA OPERATORS GENERAL

CIS 110: Introduction to Computer Programming

ESc101 : Fundamental of Computing

Operators in java Operator operands.

Java Notes. 10th ICSE. Saravanan Ganesh

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

Chapter 2 Elementary Programming

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

Logic Design: Part 2

4 Programming Fundamentals. Introduction to Programming 1 1

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Full file at

Zheng-Liang Lu Java Programming 45 / 79

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

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

Lecture 3. More About C

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

CS61B Lecture #14: Integers. Last modified: Wed Sep 27 15:44: CS61B: Lecture #14 1

Operators and Expressions

Primitive Data Types: Intro

LECTURE 3 C++ Basics Part 2

Language Fundamentals Summary

Chapter 3: Operators, Expressions and Type Conversion

Java Programming Fundamentals. Visit for more.

A variable is a name for a location in memory A variable must be declared

COMP519 Web Programming Lecture 11: JavaScript (Part 2) Handouts

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

4. Inputting data or messages to a function is called passing data to the function.

Chapter 2: Using Data

Chapter 2 Primitive Data Types and Operations. Objectives

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

COMP2611: Computer Organization. Data Representation

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

SECONDARY SCHOOL, L-IMRIEĦEL HALF YEARLY EXAMINATIONS 2016/2017

Exercise: Using Numbers

COMP2121: Microprocessors and Interfacing. Number Systems

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

A First Program - Greeting.cpp

QUIZ: What value is stored in a after this

Chapter. Let's explore some other fundamental programming concepts

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Visual C# Instructor s Manual Table of Contents

Program Elements -- Introduction

ARG! Language Reference Manual

Digital Logic. The Binary System is a way of writing numbers using only the digits 0 and 1. This is the method used by the (digital) computer.

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

Variables, Constants, and Data Types

CS112 Lecture: Primitive Types, Operators, Strings

CS61B Lecture #14: Integers

Operators. Java operators are classified into three categories:

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

Bits, Words, and Integers

Chapter 2 Primitive Data Types and Operations

Basic data types. Building blocks of computation

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.

UNIT- 3 Introduction to C++

CS Programming I: Primitives and Expressions

COMP 110 Introduction to Programming. What did we discuss?

Princeton University. Computer Science 217: Introduction to Programming Systems. Data Types in C

CS 106 Introduction to Computer Science I

Programming with Java

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Program Fundamentals

2: Basics of Java Programming

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 2

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

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

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

Full file at

6.096 Introduction to C++ January (IAP) 2009

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

Language Reference Manual simplicity

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Transcription:

Chapter 6 Primitive types Lesson page 6-1. Primitive types Question 1. There are an infinite number of integers, so it would be too ineffient to have a type integer that would contain all of them. Question 2. The five integral types are: byte, short, int, long, andchar. Question 3. The two floating-point types are float and double. Question 4. Type long is the integral type with the largest range. It is also the integral type that uses the most amount of memory. Question 5. Type double is the floating-point type with the largest range. It is also the floating-point type that uses the most amount of memory. Question 6. Type boolean has only two values: true and false. Question 7. Here s the filled-in table: type smallest value largest value boolean false/true false/true long (2 63 ) 2 63 1 int (2 31 ) 2 31 1 short (2 15 ) 2 15 1 byte (2 7 ) 2 7 1 Lesson page 6-2. The integral types Activity 6-2-1: Integral constants (literals) Question 1. A literal is a Java denotion for a value of a type. For example, 123 is a literal of type long. Activity 6-2-2: Operations on type int Question 2. The summary of operations on type int is in a footnote on the lesson page.

60 Chapter 6. Primitive types Symbol Name Example of operation unary operator: negation -45 unary operator: + addition +45 = 45 binary operator: + addition 4 + 4 = 8 binary operator: subtraction 6-4 = 2 binary operator: multiplication 3 * 2 = 6 binary operator: / division 5 / 2 = 2 binary operator: % remainder 5 % 2 = 1 Question 3. False. Java does not stop execution if integers get too large and cause overflow. Instead, the program probably produces incorrect results. Question 4. The expression -2147483648-1 evaluates to 2147483647. Question 5. We have: 5-(6/7) = 5-(0) = 5. Question 6. In Java, division 6/7 results in an integer, with the fraction part being deleted. In mathematics, division of two integers yields a rational number. Question 7. There are no operations on type byte or type short. Question 8. True. All values of one integral type are contained in the integral values of a wider type. Question 9. True. Java automatically promotes the narrower type byte to the wider type int. Question 10. The four integral types mentioned in this activity, from narrowest to widest, are: byte, short, int, long. Activity 6-2-4: Casting integer values Question 11. To cast a value is to convert it from one type to another. Question 12. The expression is: (byte)(32768-32765). Question 13. Here are the answers: byte b= 3.5; // can t assign a decimal to a byte short s= b; short j= 4; long l= (long)j; byte k= 440; // too large to store in a byte int i= k;

Chapter 6. Primitive types 61 Activity 6-2-5: Review of integer types Lesson page 6-3. A minimalist view of floating-point Activity 6-3-1: Literals of type double Question 1. All the literals are of type double except the last, 4500, which is of type int. Activity 6-3-2: Values of type double and operations on them Question 2. In Java, write 108.225 10 5 as 108.225 * 10e5. Question 3. The underlined items are, in order: double, double, float, float, long, long, int, short, byte, char, int. Question 4. The rules are: (1) byte short int long float double (2) char int long float double. Lesson page 6-4. Remarks about floating point Question 1. The statement: System.out.println(100100100100100100.0 ); prints: 1.00100100100100096E17. Lesson page 6-5. Type char Activity 6-5-1: Literals of type char Question 1. True. A char literal begins and ends with a single quotemark. Question 2. True. A String literal begins and ends with a double quotemark. Question 3. An escape sequence is a literal that cannot be written as a single character. For example, \t denotes the tab character. All the Java escape sequences begin with the backslash (the \ ) character. Question 4. Here are the literals for various symbols:

62 Chapter 6. Primitive types Java literal Symbol it represents \\ backslash \ single quote \" double quote \n line feed, or newline \r carriage return \t tab \f form feed \b backspace Question 5. ASCII stands for American Standard Code for Information Interchange. Each ASCII character uses one byte of memory. Question 6. False. Java uses Unicode, not ASCII, to represent characters. Activity 6-5-2: char as an integral type Question 7. This works only for 0 i 9. Look at the footnote on the lesson page to figure out why (or try it and see what happens). char c= (char)(i + 0 ); Question 8. Because type int is wider than char, no casting is required: int i= c; Activity 6-5-3: A loop to sequence through characters Question 9. In ASCII and Unicode, a is larger than A. Question 10. char capc= ( A - a ) + c ; Activity 6-5-4: Execution of the loop Question 11. The statement c++; increments character c. Question 12. The expression is: A <= c && c <= Z ; Lesson page 6-6. Type boolean Activity 6-6-1: The literals and operations of type boolean Question 1. The literals true and false. Question 2. Not, negation, and complement. Question 3. Here s the truth table:

Chapter 6. Primitive types 63 b c b&&c b c!b b==c b!=c true true true true false true false true false false true false false true false true false true true false true false false false false true true false Question 4. Here are the precedences: 1. Unary operators: + - ++ --! 2. Binary arithmetic operators: * / % 3. Binary arithmetic operators: + - 4. Arithmatic relations: < > <= >= 5. Equality relations: ==!= 6. Logical and: && 7. Logical or: Question 5. We start with the formula and simplify it: (true false) 5 < 3 + 2 = true 5 < 5 = true Question 6. to ). Question 7. equal to ). Equivalence is the boolean operator == (also called equal Inequivalence is the boolean operator!= (also called not Activity 6-6-2: Short-circuit evaluation Question 8. The manner in which conjunctions && and disjunctions are evaluated. If the value of the operation can be determined from the first operand, the second is not evaluated. For example, in the boolean expression: 6 > 5 4 > 5 since the first operand, 6 > 5, is true, only the first would be evaluated. However, with the following example, even though the first operand is true, both would have to be evaluated. 6 > 5 && 6 > 5

64 Chapter 6. Primitive types Question 9. When the first operand is false. Question 10. When the first operand is true. Question 11. This expression can be evaluated; since the first operand is true, the second operand is not evaluated, and the result of evaluation is true. Question 12. This expression cannot be evaluated. Since x = 0, the first operand is true, the second operand is evaluated, and a divison by 0 occurs. Activity 6-6-3: Properties of boolean operators Question 13. The formula is: ( X && Y) == ( Y && X) Question 14. The formula is: (A (B C)) == ((A B) C) Question 15. The formula is: ( A && A) == A Question 16. B!B, and!(b &&!B). Question 17.!( B && C) == (!B!C), and!(b C) == (!B &&!C). Question 18. By DeMorgan s law (!(B C) == (!B &&!C) ) and double negation (!!B == B ), the formula is equivalent to: ( b && ( i < j)) == ( b && i > j) This is true iff b is false, so it reduces to!b. So it is cannot be said to be true or false without knowing the value of b. Activity 6-6-4: Exercises on type boolean Activity 6-6-5: The mark of a boolean tyro Question 19. A tyro is one (a person) familiar with the rudiments (basics) of a subject but lacking in practical experience. Question 20. Three marks of a boolean tyro are: 1. if (athome == true)... 2. if (!athome == false)... 3. if (athome atwork) { b == true; } else { b == false; } Question 21. Here we go:!((a!= true) && ( a == true)) == true =!(!a && a) == true =!(!a && a)

=!false = true Chapter 6. Primitive types 65

66 Chapter 6. Primitive types