Lecture 2: Topics Bits and Bytes Primitive Types Casting Strings Boolean expressions.

Slides:



Advertisements
Similar presentations
Today’s lecture Review of Chapter 1 Go over homework exercises for chapter 1.
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
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.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
Constants Variables change, constants don't final = ; final double PI = ; … area = radius * radius * PI; see Liang, p. 32 for full code.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
22-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
Primitive Data Types There are exactly eight primitive data types in Java four of them represent integers: byte (class Byte), short (class Short), int.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Data types and variables
PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ04A - Scalar and composite data Programming Language.
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.
Floating Point Numbers
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
Computer Science 210 Computer Organization Floating Point Representation.
Objectives You should be able to describe: Data Types
Simple Data Type Representation and conversion of numbers
 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)
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
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.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
CISC105 – General Computer Science Class 9 – 07/03/2006.
Java Overview. Comments in a Java Program Comments can be single line comments like C++ Example: //This is a Java Comment Comments can be spread over.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Data Type. Syntax Rules Recap keywords breakdoubleifsizeofvoid caseelseintstatic..... Identifiers not#me123th scanfprintf _idso_am_igedd007 Constant ‘a’‘+’
Programming with Visual C++: Concepts and Projects Chapter 3A: Integral Data (Concepts)
Copyright © – Curt Hill Types What they do.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
COMP Primitive and Class Types Yi Hong May 14, 2015.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
“Great leaders are never satisfied with current levels of performance. They are restlessly driven by possibilities and potential achievements.” – Donna.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Programming in java Lecture-3 (a). Java Data Type TypeDescription byte8 bit signed integer short16 but signed integer int32 bit signed integer long64.
1.2 Primitive Data Types and Variables
Chapter One Lesson Three DATA TYPES ©
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
Object Oriented Programming Lecture 2: BallWorld.
1 Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
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.
1 float Data Type Data type that can hold numbers with decimal values – e.g. 3.14, 98.6 Floats can be used to represent many values: –Money (but see warning.
Computer Science 210 Computer Organization
Chapter 2 Basic Computation
How Computers Store Variables
Lecture 3 Java Operators.
Data Representation Binary Numbers Binary Addition
Chapter 4 – Fundamental Data Types
Multiple variables can be created in one declaration
Type Conversion, Constants, and the String Object
Data Structures Mohammed Thajeel To the second year students
Chapter 2.
Computer Science 210 Computer Organization
Introduction to Java, and DrJava part 1
Primitive Types and Expressions
Presentation transcript:

Lecture 2: Topics Bits and Bytes Primitive Types Casting Strings Boolean expressions

Getting Help Don’t hesitate to drop by my office or send When you send , use the subject header “honors 101” (if you don’t, and you use an outside address such as yahoo or hotmail, I won’t get your )

Web page error The mailing list link was wrong! If you added your name to the Section 3 mailing list, please delete it and join the Section 1 list instead The link is now fixed but if your web page is cached you may not see the corrected link: may have to force reload

Bits and Bytes A bit is either 0 or 1, “on” or “off” A byte is ___ bits A kilobyte is ___ bytes A megabyte is ___ kilobytes A gigabyte is ___ megabytes A ______ is _____ gigabytes

Primitive Types boolean: only 2 values: true and false In principle, needs only a bit, but uses a byte because of the way addressing is organized char: 256 different possible character values. Each char variable uses 1 byte int: for signed whole numbers Each int variable uses a 32-bit word byte, short, long: 8, 16, 64-bit variants float and double: for real numbers (not necessarily whole numbers) Each float (“floating point”) variable uses a 32-bit word divided into 3 fields: sign, significand and exponent double (“double floating point”): 64-bit variant

int: possible values Positive integers from 1 to ________, represented by (0bbb…bbb) 2 (31 bits are available for bbb…bbb) Negative integers from -1 to _______ _________ When System.out.println is used to print the value of an int, it automatically calls a routine to convert the value from binary to decimal See Homework 1

float: possible values + (1.bbb….bbb) 2 x 2 E where E ranges from -126 to +127 and there are 23 bits available in the “significand” for bbb…bbb - (1.bbb….bbb) 2 x 2 E where E ranges from -126 to +127 and there are 23 bits available in the “significand” for bbb…bbb Thus floats can store very large and very small numbers, but are limited to 23 “significant bits”: corresponds to about 7 decimal digits 0 (and -0 !) Infinity and -Infinity NaN (Not a Number) The standard that defines this is called the IEEE Floating Point Standard (1985)

double: possible values + (1.bbb….bbb) 2 x 2 E where E ranges from to and there are 52 bits available in the “significand” for bbb…bbb - (1.bbb….bbb) 2 x 2 E where E ranges from to and there are 52 bits available in the “significand” for bbb…bbb Thus doubles have an even bigger range AND have 52 significant bits: more than int. Corresponds to about 16 decimal digits ______ When System.out.println is used to print the value of a float or double, it calls a conversion routine to convert the value from binary to decimal

More on float and double System.out.println(1 – 0.9) does not give exactly 0.1, because the computations are not exact: the answer is “rounded” to the closest “double” value to the exact answer, which is not exactly 0.9 Notice that “doubles” are displayed to about 16 decimal digits by default, while “floats” are displayed to about 7 digits The Math class has constants such as Math.PI and (static) methods such as Math.sin(), Math.cos(), Math.exp()

The parallel resistance formula The resistance of a circuit with two resistors connected in parallel is 1/(1/R1 + 1/R2) What happens if R1 = 0?

Mixing types in expressions What is value of “1 / 2” ? ____ What is value of “1.0/2” ? ____ Type of the result is the “widest” type in the expression If the type of the identifier on the left-hand side of an assignment statement has a narrower type than the expression on the right, it must be explicitly “cast” to the narrower type

Casting Explicit “casting” is needed to convert a wider type to a narrower type Type width order: byte, short, int, long, float, double (widest) Example: float f = (float) Math.PI Example: int i = (int) Math.PI Does float f = require casting? Is the value of f the same as the value on the right-hand side? What about double f = ?

Converting char to int What happens: char x = ‘5’; int i = x; System.out.println(i); If want the integral value corresponding to character x: int i = x – ‘0’; Why is casting not needed?

Strings A string is not a primitive data type It is an object Details later For now, need to know about some methods of the String class –String s; // s is a reference to a String object –s.length() is a call to a method that returns the length of the string that s references –s.charAt(int j) is a call to a method that returns the character at position j of the string that s references (first character is position 0, the last character is position s.length() – 1

Boolean expressions Comparison (relational) operators: >, >=, <, <=, ==, != Boolean (logical) operators: !, ^, &&, ||, &, | Is this OK? public static boolean isLeapYear(int year){ return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; } Note: &&, || are “short-circuit” operators