Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.

Slides:



Advertisements
Similar presentations
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Advertisements

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.
Constants and Data Types Constants Data Types Reading for this class: L&L,
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.
18-May-15 Numbers. 2 Bits and bytes A bit is a single two-valued quantity: yes or no, true or false, on or off, high or low, good or bad One bit can distinguish.
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Constants Variables change, constants don't final = ; final double PI = ; … area = radius * radius * PI; see Liang, p. 32 for full code.
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.
Data types and variables
Fundamental data types Horstmann Chapter 4. Constants Variables change, constants don't final = ; final double PI = ; … areaOfCircle = radius *
String Escape Sequences
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
01- Intro-Java-part1 1 Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2008.
Objectives You should be able to describe: Data Types
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
VARIABLES AND TYPES CITS1001. Types in Java the eight primitive types the unlimited number of object types Values and References The Golden Rule Scope.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Java Data Types. Primitive Data Types Java has 8 primitive data types: – char: used to store a single character eg. G – boolean: used to store true or.
EXPRESSIONS AND ASSIGNMENT CITS1001. Scope of this lecture Assignment statements Expressions 2.
CPS120: Introduction to Computer Science
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Variables, Primitives, and Objects A Visual Learner’s Guide.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Copyright © – Curt Hill Types What they do.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
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.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
© A+ Computer Science - A reference variable stores the memory address of an object. Monster fred = new Monster(); Monster sally.
CPS120: Introduction to Computer Science Variables and Constants.
Basic Java Syntax Comments Basic data types Operators and assignment.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
1 Primitive Types n Four integer types:  byte  short  int (most common)  long n Two floating-point types:  float  double (most common) n One character.
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
1 2. Program Construction in Java. 01 Java basics.
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
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.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 2 Variables.
Chapter 2 Basic Computation
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Variables and Primative Types
Java package classes Java package classes.
Modified from Sharon Guffy
IDENTIFIERS CSC 111.
Introduction to Java, and DrJava part 1
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Chapter 2 Variables.
C++ Data Types Data Type
Introduction to Java, and DrJava
Introduction to Primitives
Introduction to Primitives
Primitive Types and Expressions
Introduction to Java, and DrJava part 1
Chapter 2 Variables.
Presentation transcript:

Primitive Data Types

int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2 32 ) and 2,147,483,647 ( ) (inclusive) Remember that dividing two ints will truncate the result, meaning you’ll lose anything after the decimal point (also known as rounding down)  E.g. 7/2 = 3 Stored in 32 bits

long Similar to an int, just has a larger range of storage Stores an integer value (whole number) between -9,223,372,036,854,775,808 (-2 63 ) and 9,223,372,036,854,775,807 ( ) (inclusive) Same rules with division as an int To create a long literal (a number that you physically type into the source code), type the number and put an L at the end  E.g is an int, 9128L is a long  This is important if your literal won’t fit in an int, otherwise you’ll get a compile error for having an int too large Stored in 64 bits

short Similar to an int and a long, just the other way around – has a smaller range of storage than an int. Stores an integer value (whole number) between -32,768 (-2 15 ) and 32,767 ( ) (inclusive) Same rules with division as an int Stored in 16 bits Used mostly to save memory in some cases We will not be using short very much, if at all, in this class. You should know that it exists and that it’s a 16-bit integer type, but unless you have a very good reason, it shouldn’t be used in programs for this class.

byte Similar to an short, just even more extreme Stores an integer value (whole number) between -128 (-2 7 ) and 127 ( ) (inclusive) Same rules with division as an int Stored in 8 bits Also used mostly to save memory in very special cases We will not be using byte very much, if at all, in this class. You should know that it exists and that it’s an 8-bit integer type, but unless you have a very good reason, it shouldn’t be used in programs for this class.

double I’ve briefly mentioned doubles in class, but here are some details on them Doubles are called a “floating point” type. Specifically, a double is a “double- precision floating-point value” Doubles store decimal values  E.g. 4.3, 8.27, -91.5, 8.0, 0.45, -7.2 They are not perfectly precise: doubles have extremely high max values, but their precision decreases as the value approaches the max value. For example, a double can’t store 0.1 exactly. This is a limitation in the way floating point values work – we will go over the specifics of how they work in more detail later in this class. Stored in 64 bits

double (cont.) Doubles allow you to perform addition, subtraction, multiplication, or division on decimal numbers Division works as you’d expect: 7.0 / 2.0 = 3.5 If either of the values in a division operation are doubles, the division will use double division (e.g. 7.0 / 2.0 = 3.5, 7 / 2.0 = 3.5, 7.0 / 2 = 3.5, 7 / 2 = 3) Because doubles aren’t perfectly precise, there are some things you can’t do with them  Never use doubles for currency or anything else that requires reliable precision and accuracy  Comparing doubles for equality does not always work the way one would expect

double Equality Doubles can be compared for less than or greater than in the same way as an int ( ), but equality doesn’t always work as expected Take, for instance, the code to the right: One may expect this to print out “True”, 0.1, and 0.1 In reality, it prints “False”, , 0.1 This is because 0.63 – 0.53 isn’t exactly 0.1 when dealing with doubles Type this code in yourself and run it!

double Equality (cont.) So how do we compare doubles for equality? We use something known as an epsilon! In programming, an epsilon is a value small enough such that we consider two doubles the same if they differ by no more than the epsilon The value of an epsilon depends on context: since doubles’ precision changes as the values get higher or lower, different epsilons may be needed for different contexts We’re going to use a function from the math library known as Math.abs  This function takes a double and returns the absolute value of that double  In other words, if the value is 0 or positive, it returns the value. If the value is negative, it will return -1 * value.  E.g. Math.abs(42) = 42, Math.abs(-10) = 10, Math.abs(0) = 0, Math.abs(-5.4) = 5.4, etc.

double Equality (cont.) So to compare two doubles for equality, we check to see if the absolute value of the difference of the doubles is less than our epsilon.  if(Math.abs(value1 – value2) < eps) This will be true if value1 is up to eps greater than value2, or if value2 is up to eps greater than value1. Since we’ve said that we consider value1 and value2 the same if they differ by less than eps, this means we consider them essentially equal. We can represent doubles in scientific notation using the letter “e”  E.g. 3.6e-3 = , 5e2 = 500, 1e3 = 1000, 1e-5 = As I mentioned earlier, epsilon values vary wildly based on context, but a good starting point for the types of numbers we’re likely to be working with is around 1e-12

float Floats are to doubles as shorts are to ints Floats are also known as “single-precision floating-point value” and are less precise than doubles They are stored using 32 bits We will not be using float very much, if at all, in this class. You should know that it exists and that it’s a 32-bit floating-point type, but unless you have a very good reason, it shouldn’t be used in programs for this class – double should almost always be preferred.

boolean A boolean stores exactly one of two possible values: true or false You’ve been using booleans for awhile even if you didn’t know it – all if statements operate on Boolean values We will discuss booleans in more depth after Test 1

char A char stores a single 16-bit Unicode character Examples: ‘a’, ‘0’, ‘Z’, ‘*’ Strings are built up of chars We will discuss chars in more depth after Test 1