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

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

Basics of Java Programming

More Programming Constructs -- Introduction

Declaration and Memory

CS313D: ADVANCED PROGRAMMING LANGUAGE

Getting started with Java

Chapter 3: Operators, Expressions and Type Conversion

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

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Full file at

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

JAVA OPERATORS GENERAL

Ex: If you use a program to record sales, you will want to remember data:

Darrell Bethea May 25, 2011

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

LECTURE 3 C++ Basics Part 2

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

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

COMP Introduction to Programming Primitive Types, Strings and Console I/O

CMPT 125: Lecture 3 Data and Expressions

Operators. Lecture 3 COP 3014 Spring January 16, 2018

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

Operators in C. Staff Incharge: S.Sasirekha

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Full file at

COMP 110 Introduction to Programming. What did we discuss?

DATA TYPES AND EXPRESSIONS

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

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

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

Operators. Java operators are classified into three categories:

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Computer System and programming in C

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

CIS 110: Introduction to Computer Programming

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 2

Zheng-Liang Lu Java Programming 45 / 79

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Lecture 3 Tao Wang 1

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Chapter 2: Using Data

ECE 122 Engineering Problem Solving with Java

MODULE 02: BASIC COMPUTATION IN JAVA

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

Operators. Java Primer Operators-1 Scott MacKenzie = 2. (b) (a)

Reserved Words and Identifiers

Midterms Save the Dates!

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

Operators & Expressions

Data types Expressions Variables Assignment. COMP1400 Week 2

Program Fundamentals

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

Chapter 6 Primitive types

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

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Visual C# Instructor s Manual Table of Contents

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Chapter 2 ELEMENTARY PROGRAMMING

1.3b Type Conversion

JAVA Programming Fundamentals

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Programming with Java

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

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

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

Introduction to Programming EC-105. Lecture 2

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto

Chapter 2 Elementary Programming

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

AYBUKE BUYUKCAYLI KORAY OZUYAR MUSTAFA SOYLU. Week 21/02/ /02/2007 Lecture Notes: ASCII

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Arithmetic and IO. 25 August 2017

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

Values, Variables, Types & Arithmetic Expressions. Agenda

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

The Arithmetic Operators

Information Science 1

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Fundamentals of Programming CS-110. Lecture 2

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

Chapter 4: Basic C Operators

Lecture 1. Types, Expressions, & Variables

Basics of Java Programming variables, assignment, and input

Unit 3. Operators. School of Science and Technology INTRODUCTION

Prof. Navrati Saxena TA: Rochak Sachan

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Comparing Data. Comparing Floating Point Values. Comparing Float Values. CS257 Computer Science I Kevin Sahr, PhD

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Fundamentals of Programming

Work relative to other classes

Prepared by: Shraddha Modi

Building Java Programs

Object-Oriented Programming

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Transcription:

COMP 110-001 Primitive and Class Types Yi Hong May 14, 2015

Review What are the two major parts of an object? What is the relationship between class and object? Design a simple class for Student How to use a variable? 2

Today Primitive type Integer Boolean Float / Double Character Class type 3

Data Types Class type: Object with both data and methods Has the the same name as the class Name begins with uppercase letter (recommended) E.g.: Scanner, String, Student (user-defined) Primitive type: indecomposable values Name begins with lowercase letters E.g.: int, double, char, boolean, See Figure 2.1, p 52 for the full list 4

Primitive Types Integer (byte, short, int, long) 0, -5, 10, 30 Floating-point (float, double) 0.5, -10.0, 12.98 Single character (char) A, c, %, S Boolean (boolean) True or false 5

Integer byte: 1 byte, -2 7 to 2 7-1 short: 2 bytes, -2 15 to 2 15-1 int: 4 bytes, -2 31 to 2 31-1 long: 8 bytes, -2 63 to 2 63-1 Numerical operations on integers return integers bytes 01000101 01000101 01010101 11110000 11101001 00101010 10110101 11000101 01001101 main memory 6

Signed Conversions Signed binary to decimal, e.g., 10111101 2 1 0111101 Original value 0 1000010 Ones complement +1 Add 1 0 1000011 Result: 67 The sign is 1, a negative number, so 10111101 2 = -67 10 Signed decimal to binary, e.g., -102 102/2 = 51 rem. 0 51/2 = 25 rem. 1 25/2 = 12 rem. 1 12/2 = 6 rem. 0 6/2 = 3 rem. 0 3/2 = 1 rem. 1 1/2 = 0 rem. 1 0 1100110 : +102 1 1 0 0 1 1 0 1 0011010 Two s complement (ones complement and add 71)

Floating-point Has a fractional part E.g.: 5.0 float: 4 bytes, single-precision, smaller range, lower precision double: 8 bytes, double-precision, larger range, higher precision If you cannot decide between the types float and double, use double 8

Single Character (Unicode) Char: 2 bytes, 0 to 2 16-1 Single quotes enclose a character E.g.: a, A Uppercase letters and lowercase letters are different characters 9

Boolean boolean: 1 bit, true or false Boolean operators && (and), (or),! (negation) && true false true true false false false false true false true true true false true false! true false! false true 10

Assignment Compatibilities Usually, we need to put values of a certain type into variables of the same type However, in some cases, the value will automatically be converted when types are different A value can be assigned to a variable whose type allows more precision byte à short à int à long à float à double int age; age = 10; double length; length = age; 11

Type Casting Changes the data type of a value from its normal type to some other type E.g: double distance = 9.0; int points = distance; int points = (int)distance; Syntax: (Type_Name) Expression Note that the value is truncated, not rounded Note: in the example, the variable distance is not changed, the assignment statement affects only the value stored in points 12

Examples of Type Casting 3 / 2 = 1 Integer division truncates the results (double)3 / (double)2 = 1.5 Try it yourself System.out.println(3/2); System.out.println((float)3 / (float)2); What happens if you cast a double into int? E.g.: what s the output of the following statement? System.out.println((int)1.5); 13

Try It Yourself Run code in Eclipse See TypeCasting.java on the course website for more details 14

Arithmetic Operators Unary operators + : Unary plus operator; indicates positive value - : Unary minus operator; negates an expression ++ : Increment operator; increments a value by 1 -- : Decrement operator; decrements a value by 1! : Logical complement operator; inverts the value of a boolean Binary arithmetic operators *, /, %, +, - E.g.: rate * rate + delta 1 / (time + 3*mass) (a 7) / (t + 9 * v) 15

% Operator Remainder operator, or modulus operator The % operator gets the remainder after division An example An integer n is even if n%2=0, odd if n%2=1 Floating-point numbers Java allows to use % with floating-point operands f % d = f d * q (q is the integer portion of f/d, and the sign of q is the same as the sign of f/d) E.g.: -6.5 % 2.0 = -0.5, 6.5 % -2.0 = 0.5 16

Specialized Assignment Operators Combine an arithmetic operator with the simple assignment operator (=) as a shorthand notation E.g.: amount += 5; <--> amount = amount + 5; amount *= 25; <--> amount = amount * 25; 17

Parentheses and Precedence (I) Expressions inside parentheses Tell the computer which operations to perform first, second, and so forth E.g.: (cost + tax) * discount cost + (tax * discount) 18

Parentheses and Precedence (II) Precedence rules Highest Precedence First: the unary operators +, -,!, ++, and -- Second: the binary arithmetic operators *, /, % Third: the binary arithmetic operators + and Lowest Precedence Boolean operators:! à && à E.g.:!true && (false true) true 19

Errors in a Program Syntax error: grammatical mistake in your program Run-time error: an error that is detected during program execution Logic error: a mistake in a program caused by the underlying algorithm 20

Self-Test Questions How do you swap the values of two variables, e.g., Integer, or Floating-point? 21

Next Class Lab 0 & 1 Bring your laptop and textbook To-do before the class Review the slides of lecture 2 on creating objects and accessing objects methods 22