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.

Slides:



Advertisements
Similar presentations
CSci 1130 Intro to Programming in Java
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,
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
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.
Chapter Day 4. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 4 Return Signed Contracts Questions from last Class?? Problem set 1 Posted.
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.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
String Escape Sequences
Expressions, Data Conversion, and Input
***** SWTJC STEM ***** Chapter 2-3 cg 29 Java Operators Recall Java’s programming components: Packages - Collection of classes (Programs) Classes - Collections.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
Chapter 2 Data and Expressions. © 2004 Pearson Addison-Wesley. All rights reserved2-2 Data and Expressions Let's explore some other fundamental programming.
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.
 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)
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.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Mathematical Calculations in Java Mrs. G. Chapman.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
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.
Mathematical Calculations in Java Mrs. C. Furman.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
COM S 207 Literal, Operator, and Expression Instructor: Ying Cai Department of Computer Science Iowa State University
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.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Doing math In java.
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.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
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.
1 2. Program Construction in Java. 01 Java basics.
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.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
© 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.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Object Oriented Programming
Data Conversion & Scanner Class
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Type Conversion, Constants, and the String Object
Escape Sequences What if we wanted to print the quote character?
Increment and Decrement
Lecture 3 Expressions Richard Gesick.
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
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Chapter 2: Objects and Primitive Data
Data Types and Expressions
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

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 be done with it, and how much memory needs to be put aside for it. When we create a variable in Java, we need to specify: the type of the value we want to put in there, and the name we will use for that variable. We have seen that when we enter the name of a Person object, we need to put it in quotes, but when we enter the age, we don't. This is because they are of different types. Draw a picture of a box representing a variable. Remind students that a variable is really a memory location that is put aside to hold a certain value. We give a name to that location and we never need to know where it actually is. We can put values in there and find out what values are in there whenever we like, by using the name of the variable. For an object, a variable is a place in memory where the state of the object is stored.

Variables A variable is a name for a location in memory A variable must be declared, specifying the variable's name and the type of information that will be held in it data type variable name int total; int count, temp, result; Multiple variables can be created in one declaration int count=1, temp=0, result; Variables can also be given initial values

final int MIN_HEIGHT = 69; Constants A constant is an identifier that is similar to a variable except that it holds one value for its entire existence The compiler will issue an error if you try to change a constant In Java, we use the final modifier to declare a constant final int MIN_HEIGHT = 69; Constants: give names to otherwise unclear literal values facilitate changes to the code prevent inadvertent errors

Numeric Primitive Data The difference between the various numeric primitive types is their size, and therefore the values they can store: 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

Type int (byte, short, & long) An int is a whole number (e.g. 21). You can do arithmetic with an int. int age = 21; addition + subtraction - multiplication * division / modulus % 21 age int age = 15; age + 3 2 * age - 4 age / 2 age % 10 Explain the last 3 lines here. The first one creates a variable of type int and calls it age. The next two are expressions that use whatever value is stored in that variable. Talk about the symbols used for arithmetic, and what modulus means. An int is not an object. It doesn't have attributes and behaviour. It just has a single value. It can be one of the attributes of an object. On the overhead, show that you cannot do arithmetic with a String that looks like it might be an int, e.g. "2" + 1. Don't go into too much detail about variables and types, as we will do more on that in week 3.

Type double (float) The type called double is used to store a real number , i.e. a number with a decimal point. It is stored in a different format from an int. You can do arithmetic on a double. double price = 3.95; 3.95 price * 0.1 might be working out a 10% discount, or the 10% GST. The final price of an item might be the last expression, i.e. the original price plus 10% of that. price addition + subtraction - multiplication * division / price * 0.10 price / 2 price + price * 0.1

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

Type char A char is a character, i.e. a bit pattern you can produce by pressing a key (or a combination of keys) on a keyboard. Examples are 'a' 'A' '3' '?' '!' char response = 'Y'; You cannot do arithmetic on a char. A String is a collection of chars. 'Y' response Explain the difference between "2", '2' and 2 briefly, and what you can do with each of these.

Type String A String is a collection of characters (e.g. "Sally"). String name = "Sally"; A String value is always written in double quotes. You can have an empty String, shown as "". A String has many methods including: change itself to upper/lower case tell you how long it is (how many characters) give you the character at a specified position the string concatenation operator + Sally name Talk about what a character is. This was not mentioned in week 1's lecture but should be covered in CSE1200. Point out the line that creates a String with an initial value of "Sally". String is actually a class in Java. A String object has attributes including the collection of characters in it, and certain behaviours in its public interface. There are more than what are listed here. Open the editor window in BlueJ and show the part of the code for the Person class that defines the attributes. Don't worry about the rest of it.

result = total + count / max - offset; Operator Precedence Operators can be combined into complex expressions result = total + count / max - offset; Operators have a well-defined precedence which determines the order in which they are evaluated Multiplication, division, and remainder are evaluated prior to addition, subtraction, and string concatenation Arithmetic operators with the same precedence are evaluated from left to right Parentheses can always be used to force the evaluation order

Operator Precedence What is the order of evaluation in the following expressions? a + b + c + d + e a + b * c - d / e 1 2 3 4 3 1 4 2 a / (b + c) - d % e 2 1 4 3 a / (b * (c + (d - e))) 4 3 2 1

Assignment Revisited The assignment operator has a lower precedence than the arithmetic operators First the expression on the right hand side of the = operator is evaluated answer = sum / 4 + MAX * lowest; 4 1 3 2 Then the result is stored in the variable on the left hand side

Assignment Revisited The right and left hand sides of an assignment statement can contain the same variable First, one is added to the original value of count count = count + 1; Then the result is stored back into count (overwriting the original value)

Data Conversions Sometimes it is convenient to convert data from one type to another For example, we may want to treat an integer as a floating point value during a computation Conversions must be handled carefully to avoid losing information Widening conversions are safest because they tend to go from a small data type to a larger one (such as a short to an int) Narrowing conversions can lose information because they tend to go from a large data type to a smaller one (such as an int to a short)

Data Conversions In Java, data conversions can occur in three ways: assignment conversion arithmetic promotion casting Assignment conversion occurs when a value of one type is assigned to a variable of another Only widening conversions can happen via assignment Arithmetic promotion happens automatically when operators in expressions convert their operands

result = (float) total / count; Data Conversions Casting is the most powerful, and dangerous, technique for conversion Both widening and narrowing conversions can be accomplished by explicitly casting a value To cast, the type is put in parentheses in front of the value being converted For example, if total and count are integers, but we want a floating point result when dividing them, we can cast total: result = (float) total / count;