CSci 1130 Intro to Programming in Java

Slides:



Advertisements
Similar presentations
© 2007 Lawrenceville Press Slide 1 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5;
Advertisements

Lecture 5 Types, Expressions and Simple I/O COMP1681 / SE15 Introduction to Programming.
© Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.
1 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1 Operators and Expressions.
COMP 14 Introduction to Programming
Introduction to Programming G51PRG University of Nottingham Revision 1
DATA TYPES, VARIABLES, ARITHMETIC. Variables A variable is a “named container” that holds a value. A name for a spot in the computer’s memory This value.
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.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
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.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
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
Objectives You should be able to describe: Data Types
1 Course Lectures Available on line:
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)
Outline Questions / Review Predefined Objects Variables Primitive Data Arithmetic Expressions Interactive Programs Decision Making Assignments.
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.
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:
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)
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.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Chapter 2: Data and Expressions String and String Concatenation Escape Sequences Variables Primitive Date Types Expressions Interactive Programs.
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.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
© 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.
Data and Expressions. Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
© 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.
Fundamentals 2.
Chapter 2 Variables.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
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?
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.
Fundamentals 2.
Chapter 2: Java Fundamentals
Chapter 2: Java Fundamentals
Chap 2. Identifiers, Keywords, and Types
Chapter 2: Objects and Primitive Data
Chapter 2 Variables.
Presentation transcript:

CSci 1130 Intro to Programming in Java Variables and Calculations

Primitive Data Types A data type is defined by a set of values and the operators you can perform on them Each value stored in memory is associated with a particular data type The Java language has several predefined types, called primitive data types The following reserved words represent eight different primitive types: byte, short, int, long, float, double, boolean, char

Integers There are four separate integer primitive data types They differ by the amount of memory used to store them Type byte short int long Storage 8 bits 16 bits 32 bits 64 bits Min Value -128 -32,768 -2,147,483,648 < -9 x 1018 Max Value 127 32,767 2,147,483,647 > 9 x 1018

Floating Point There are two floating point types: The float type stores 7 significant digits The double type stores 15 significant digits Approximate Min Value -3.4 x 1038 -1.7 x 10308 Approximate Max Value 3.4 x 1038 1.7 x 10308 Type float double Storage 32 bits 64 bits

Characters A char value stores a single character from the Unicode character set A character set is an ordered list of characters The Unicode character set uses sixteen bits per character, allowing for 65,536 unique characters It is an international character set, containing symbols and characters from many world languages

Characters The ASCII character set is still the basis for many other programming languages ASCII is a subset of Unicode, 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, ...

Boolean A boolean value represents a true or false condition They 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

Wrappers For each primitive data type there is a corresponding wrapper class. For example: Wrapper classes are useful in situations where you need an object instead of a primitive type They also contain some useful methods Primitive Type int double char boolean Wrapper Class Integer Double Character Boolean

Variables A variable is an identifier that represents a location in memory that holds a particular type of data Variables must be declared before they can be used The syntax of a variable declaration is: data-type variable-name; For example: int total;

Variables Multiple variables can be declared on the same line: int total, count, sum; Variables can be initialized (given an initial value) in the declaration: int total = 0, count = 20; float price = 57.25f; double tuition = 145.50;

Assignment Statements An assignment statement takes the following form: variable-name = expression; The expression is evaluated and the result is stored in the variable, overwriting the value currently stored in the variable The expression can be a single value or a more complicated calculation

Constants A constant is similar to a variable except that they keep the same value throughout their existence They are specified using the reserved word final in the declaration For example: final double PI = 3.14159; final int STUDENTS = 25;

Constants When appropriate, constants are better than variables because: they prevent inadvertent errors because their value cannot change They are better than literal values because: they make code more readable by giving meaning to a value they facilitate change because the value is only specified in one place

Escape Sequences An escape sequence is a special sequence of characters preceded by a backslash (\) They indicate some special purpose, such as: Escape Sequence \t \n \" \' \\ Meaning tab new line double quote single quote backslash

Expressions An expression is a combination of operators and operands The arithmetic operators include addition (+), subtraction (-), multiplication (*), and division (/) Operands can be literal values, variables, or other sources of data The programmer determines what is done with the result of an expression (stored, printed, etc.)

Division If the operands of the / operator are both integers, the result is an integer (the fractional part is truncated) If one or more operands to the / operator are floating point values, the result is a floating point value The remainder operator (%) returns the integer remainder after dividing the first operand by the second The operands to the % operator must be integers

Division Expression 17 / 5 17.0 / 5 17 / 5.0 9 / 12 9.0 / 12.0 6 % 2 14 % 5 -14 % 5 Result 3 3.4 0.75 4 -4

Operator Precedence The order in which operands are evaluated in an expression is determined by a well-defined precedence hierarchy Operators at the same level of precedence are evaluated according to their associativity (right to left or left to right) Parentheses can be used to force precedence

Operator Precedence Multiplication, division, and remainder have a higher precedence than addition and subtraction Both groups associate left to right Expression: Order of evaluation: Result: 5 + 12 / 5 - 10 % 3 6 3 1 4 2

Operator Precedence Expression 2 + 3 * 4 / 2 3 * 13 + 2 (3 * 13) + 2 3 * (13 + 2) 4 * (11 - 6) * (-8 + 10) (5 * (4 - 1)) / 2 Result 8 41 45 40 7