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.

Slides:



Advertisements
Similar presentations
CSci 1130 Intro to Programming in Java
Advertisements

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.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
ECE122 L2: Program Development February 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 2 Program Development.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Primitive Data Types There are exactly eight primitive data types in Java four of them represent integers: byte (class Byte), short (class Short), int.
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.
Chapter 2 Data Types, Declarations, and Displays
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
String Escape Sequences
Program Elements -- Introduction
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
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.
1 Program Elements -- Introduction zWe can now examine the core elements of programming zLecture focuses on: ydata types yvariable declaration and use.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Outline Questions / Review Predefined Objects Variables Primitive Data Arithmetic Expressions Interactive Programs Decision Making Assignments.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyCopyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 2: Objects and Primitive Data Presentation slides for Java Software Solutions for AP* Computer.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Chapter 2 Data and Expressions Part One. © 2004 Pearson Addison-Wesley. All rights reserved2-2/29 Data and Expressions Let's explore some other fundamental.
Chapter 2 Variables.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Chapter 2: Data and Expressions String and String Concatenation Escape Sequences Variables Primitive Date Types Expressions Interactive Programs.
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.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
2-1 Character Strings A string of characters can be represented as a string literal by putting double quotes around the text: Examples: "This is a string.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
Primitive Data Types 1 In PowerPoint, point at the speaker icon, then click the "Play" button.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Chapter 2: Objects and Primitive Data Lian Yu Department of Computer Science and Engineering Arizona State University Tempe, AZ
Data and Expressions. Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration.
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.
© 2006 Pearson Education Chapter 2: Objects and Primitive Data Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by.
© 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.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
ITEC113 Algorithms and Programming Techniques
Multiple variables can be created in one declaration
CprE 185: Intro to Problem Solving (using C)
Escape Sequences What if we wanted to print the quote character?
Data and Expressions Part One
Escape Sequences Some Java escape sequences: See Roses.java (page 68)
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.
Objects and Primitive Data
Chapter 2 Variables.
Chapter 2: Java Fundamentals
Chap 2. Identifiers, Keywords, and Types
Chapter 2: Objects and Primitive Data
Chapter 2 Variables.
Presentation transcript:

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 (primitive) objects, such as integers etc E.g., Java predefines classes for such basic types for example, one predefined class is Integer: To create an Integer (object): Integer wagePerHour = new Integer(10); System.out.println( wagePerHour );

Primitive Data Types Although in pure object-oriented languages (such as Smalltalk and Ruby), we can consider everything as objects and defines everything as class, for efficiency, some languages like C++ and Java treat common objects such as numbers, characters, and logical values specially, and introduces them into the language as primitive data types Note: this division between primitive types and objects is disliked by some programmers familiar with languages such as Smalltalk and Ruby where everything is an object

Primitive Data Types There are exactly eight primitive data types four of them represent integers: byte, short, int, long two of them represent floating point numbers float, double one of them represents characters: char and one of them represents boolean (logical) values: boolean

Numeric Primitive Data Types byte, short, int, long, float, and double are numeric data types The difference among the various numeric primitive types is their storage sizes and representations, and therefore the ranges and precision of the values they can store To understand this, we need to have a rough understanding of computer memory storage

Memory Primary storage area for programs and data Also called RAM RAM is divided into many cells; each cell can be identified by a numeric address Main Memory 10011010 Each memory cell has a set number of bits (usually 8 bits, or one byte); a bit can represent 2 values) 9278 9279 9280 9281 9282 9283 9284 9285 9286 - how many values can a byte represent? A computer can use more cells to store data, e.g., 2 bytes - how many values can 2 bytes represent?

Numeric Primitive Data “Objects” of different numeric data types occupy different number of cells Type byte short int long float double Storage 8 bits 16 bits 32 bits 64 bits Min Value -128 -32,768 -2,147,483,648 < -9 x 1018 +/- 3.4 x 1038 with 7 significant digits +/- 1.7 x 10308 with 15 significant digits Max Value 127 32,767 2,147,483,647 > 9 x 1018 IEEE 754 format

Characters A char is a single character from a character set A character set is an ordered list of characters; each character is given a unique number Character literals are represented in a program by delimiting with single quotes: 'a‘ 'X‘ '7' '$‘ ',' '\n'

Character Sets The ASCII character set is quite popular. It includes: uppercase letters lowercase letters punctuation digits special symbols control characters A, B, C, … a, b, c, … period, semi-colon, … 0, 1, 2, … &, |, \, … carriage return, tab, ...

Boolean A boolean value represents logical value: true or false The reserved words true and false are the only valid values for a boolean type A boolean can also be used to represent any two states, such as a light bulb being on or off

Multiple variables can be created in one declaration Variables: Revisited We already know that a variable must be declared, specifying the variable's name and the type of information that will be held in it As of now, think of a variable as a name for a location in memory cell (we will revisit the concept later) int total; int count, temp, result; Multiple variables can be created in one declaration data type variable name

Variables A variable can be given an initial value in the declaration int sum = 0; int base = 32, max = 149; String msg1 = new String( “Hello” ); String msg2 = “Hello” ; When a variable is referenced in a program, its current value is used

Change the Value of a Variable: Assignment Statement An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; The expression on the right is evaluated and the result is stored in the variable on the left The value that was in total is overwritten Remember: you can only assign a value to a variable that is consistent with the variable's declared type

Constants A “constant variable” is an identifier that is similar to a variable except that it holds one value for its entire existence Why constants: give names to otherwise unclear literal values facilitate changes to the code prevent inadvertent errors The compiler will issue an error if you try to assign value to a constant variable after its declaration

Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions (we will see logical expressions later) are essentially special methods applied to numerical data objects: compute numeric results and make use of the arithmetic operators: Addition + Subtraction - Multiplication * Division / Remainder %

Division and Remainder If both operands to the division operator (/) are integers, the result is an integer (the fractional part is discarded) 14 / 3 equals? 8 / 12 equals? The remainder operator (%) returns the remainder after dividing the second operand into the first 14 % 3 equals? 8 % 12 equals?