Multiple variables can be created in one declaration

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,
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.
1 Variables b A variable is a name for a location in memory b A variable must be declared, specifying the variable's name and the type of information that.
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.
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
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.
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.
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.
© 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)
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.
Mathematical Calculations in Java Mrs. C. Furman.
Chapter 2: Data and Expressions String and String Concatenation Escape Sequences Variables Primitive Date Types Expressions Interactive Programs.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
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.
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.
© 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.
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.
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.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
© 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)
CSC 1051 – Data Structures and Algorithms I
Data Conversion & Scanner Class
Type Conversion, Constants, and the String Object
CprE 185: Intro to Problem Solving (using C)
Escape Sequences What if we wanted to print the quote character?
Increment and Decrement
Lecture 3 Expressions Richard Gesick.
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
Arithmetic Expressions & Data Conversions
Chapter 2 Variables.
Data Types and Expressions
Chap 2. Identifiers, Keywords, and Types
Chapter 2: Objects and Primitive Data
Data Types and Expressions
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

Multiple variables can be created in one declaration A variable must be declared by specifying the variable's name and the type of information that it will hold Data Types: int, double, char data type variable name int total; int count, temp, result; Multiple variables can be created in one declaration

2.3 – Variables A variable can be given an initial value in the declaration A variable can be given an initial value or a different value anywhere in the program When a variable is used in a program, its current value is used int sum = 0; int base = 32, max = 149;

public class Variables { public static void main (String[] args) { int apple = 13; System.out.println (“The number of apples is " + apple); apple = 25; } What happens if you put an int before apple = 25 ? What happens if you don’t give an initial value?

2.3 - Assignment Operator (=)/Statement An assignment statement changes the value of a variable The assignment operator is the = sign int total; 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 just like in the Variables program.

2.3 - Constants A constant is an identifier that is similar to a variable except that it holds one value while the program is active The compiler will issue an error if you try to change the value of a constant during execution In Java, we use final before the data type to declare a constant final int dozen = 12;

2.4 - Primitive Data There are exactly eight primitive data types in Java int : represents integers double : represents floating point numbers char : represents characters boolean : represents characters (true or false)

2.4 - Numeric Primitive Data The difference between the numeric primitive types is their size and the values they can store. The int type stores only integer numbers while double includes a decimal place. Type int double Storage 32 bits 64 bits Min Value -2,147,483,648 +/- 1.7 x 10308 with 15 significant digits Max Value 2,147,483,647

2.4 - Boolean A boolean value represents a true or false condition The reserved words true and false are the only valid values for a boolean type boolean done = false;

2.4 - Characters A char variable stores a single character from the Unicode character set A character set is an ordered list of characters, and each character corresponds to a unique number Character literals are delimited by single quotes: 'a' 'X' '7' '$' ',' '\n‘ The Unicode character set has 65,536 unique characters It is an international character set, containing symbols and characters from many world languages

2.4 - Characters The ASCII character set is older and smaller than Unicode, but is still quite popular The ASCII characters are a subset of the Unicode character set, including: 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, ...

2.5 - Arithmetic Expressions What is an arithmetic expression? An expression is a combination of one or more operands and their operators Arithmetic expressions compute numeric results and make use of the arithmetic operators: Addition + Subtraction - Multiplication * Division / Remainder % If either or both operands associated with an arithmetic operator are floating point, the result is a floating point

2.5 - Division and Remainder If both operands to the division operator (/) are integers, the result is an integer (the remainder is discarded) 14 / 3 equals? 4 8 / 12 equals? The remainder operator (%) returns the remainder after dividing 14 % 3 equals? 2 8 % 12 equals? 8

result = total + count / max - offset; 2.5 - Operator Precedence Operators can be combined into complex expressions result = total + count / max - offset; How does PEMDAS work? 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 be used to force the evaluation order

2.5 - 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

2.5 - Assignment Operator 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

2.5 - Assignment Operator (=) 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)

2.5 - Data Conversions Sometimes we might want to convert data from one type to another Widening conversions are safest because they usually do not lose information (int to double) Example: The integer 5 as a double is _____ Narrowing conversions can lose information (double to int) Example: The double 6.7 as an integer is _____

2.5 - Data Conversions In Java, data conversions can occur in three ways: Assignment conversion occurs when a value of one type is assigned to a variable of another (only for widening conversions) Arithmetic promotion happens automatically when operators in expressions convert their operands Casting is when the type is put in parentheses in front of the value being converted

2.5 - Data Conversions Assignment int total = 2; Arithmetic int total; double result = total; Arithmetic int total; double count; result1 = total / count; result2 = count / total; Casting result = (double) total / count;

Assignment Textbook Assignment M.C. (2, 4 – 6) T/F (4 - 7) S.A. (6, 7)