Constants and Variables  Memory cells used in program  Called constants and variables  Identifiers for constants and variables should be declared before.

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

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Types and Arithmetic Operators
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.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions.
Expressions ► An expression can be a single variable, or can include a series of variables. If an expression includes multiple variables they are combined.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 Chapter 3 Arithmetic Expressions. 2 Chapter 3 Topics l Overview of Java Data Types l Numeric Data Types l Declarations for Numeric Expressions l Simple.
1 Data types, operations, and expressions Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
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 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
What is a variable?  A variable holds data in memory so the program may use that data, or store results.  Variables have a data type. int, boolean, char,
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Chapter 2: Introducing Data Types and Operators.  Know Java’s primitive types  Use literals  Initialize variables  Know the scope rules of variables.
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
 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)
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
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.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Chapter 2: Using Data.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
Primitive Variables.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
Numeric Data Types There are six numeric data types: byte, short, int, long, float, and double. Sample variable declarations: int i, j, k; float numberOne,
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
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.
Rational Expressions relational operators logical operators order of precedence.
ISBN Chapter 7 Expressions and Assignments Statements.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
ITEC113 Algorithms and Programming Techniques
Chapter 7: Expressions and Assignment Statements
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Test Review Computer Science History
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Primitive and Reference Data Values
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
Explicit and Implicit Type Changes
Numerical Data Types.
Chapter 2: Basic Elements of Java
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Data Types and Expressions
Operator King Saud University
Presentation transcript:

Constants and Variables  Memory cells used in program  Called constants and variables  Identifiers for constants and variables should be declared before the constants or variables are used  Declaration is accomplished using declaration statements  Constants  Values do not change during execution of the program  Variables  Values can be changed as the program executes.  Memory cells used in program  Called constants and variables  Identifiers for constants and variables should be declared before the constants or variables are used  Declaration is accomplished using declaration statements  Constants  Values do not change during execution of the program  Variables  Values can be changed as the program executes.

Declarations of symbolic constants in Java  Value of a constant can not change during the execution of the program  Constant must be initialized when it is declared  Syntax of constant declaration  final TYPE identifier = value; OR  static final TYPE identifier = value;  Examples  final float PI= F;  static final double CONVERT=2.21;  Value of a constant can not change during the execution of the program  Constant must be initialized when it is declared  Syntax of constant declaration  final TYPE identifier = value; OR  static final TYPE identifier = value;  Examples  final float PI= F;  static final double CONVERT=2.21;

Declarations of variables in Java  A variable declaration assigns names for variables (memory cells used in program)  Syntax of variable declaration  TYPE var_identifier; or  TYPE var_identifier1=value, var_identifier2, …;  multiple variables of the same type may be declared on the same line  Variable may be initialized when it is declared  A variable declaration assigns names for variables (memory cells used in program)  Syntax of variable declaration  TYPE var_identifier; or  TYPE var_identifier1=value, var_identifier2, …;  multiple variables of the same type may be declared on the same line  Variable may be initialized when it is declared

Examples of Declaration of Variable Objects  int my_integer_value;  char middle_initial, first_initial;  byte counter;  double ScaleFactor;  float temperature;  Boolean TestResult  int my_integer_value;  char middle_initial, first_initial;  byte counter;  double ScaleFactor;  float temperature;  Boolean TestResult

InitializationInitialization  Memory locations associated with defined constants must be initialized when the constants are defined  Memory locations associated with variables may be initialized anywhere in the program  Initialization may occur in the declaration statement  Initialization may be done after declaration using an assignment statement  Memory locations associated with variables should have their values defined before they are used.  It is good programming practice to initialize variables at the start of your program  Memory locations associated with defined constants must be initialized when the constants are defined  Memory locations associated with variables may be initialized anywhere in the program  Initialization may occur in the declaration statement  Initialization may be done after declaration using an assignment statement  Memory locations associated with variables should have their values defined before they are used.  It is good programming practice to initialize variables at the start of your program

Examples: Initialization and Declaration  long my_integer_value = -12L;  char myinitial = ′a′;  byte counter = 31;  short AgeInYears = 22;  double ScaleFactor = ;  float temperature = 3.475F;  boolean done_flag=false;  long my_integer_value = -12L;  char myinitial = ′a′;  byte counter = 31;  short AgeInYears = 22;  double ScaleFactor = ;  float temperature = 3.475F;  boolean done_flag=false;

Data Types and Expressions  Data Type  A set of values plus a set of operations on those values  A crucial concept on modern programming  Data Type of a variable determines how its memory cells are interpreted  In Java every variable or constant object has a type  Only values of that type can be directly assigned to the variable.  Operators combine variables of the same type  Methods for implicit and explicit conversions between types exist for specific conversions  Data Type  A set of values plus a set of operations on those values  A crucial concept on modern programming  Data Type of a variable determines how its memory cells are interpreted  In Java every variable or constant object has a type  Only values of that type can be directly assigned to the variable.  Operators combine variables of the same type  Methods for implicit and explicit conversions between types exist for specific conversions

ExpressionsExpressions  An expression can be a single variable, or can include a series of variables. If an expression includes multiple variables they are combined using operators  Arithmetic expressions manipulate numeric variables following the rules of algebra and have numeric values.  Integral expressions manipulate integer values  Floating point or decimal expressions manipulate floating point numbers  Relational expressions compare numerical values and have logical (boolean) values  Logical expressions combine boolean variables or expressions and yield boolean values  An expression can be a single variable, or can include a series of variables. If an expression includes multiple variables they are combined using operators  Arithmetic expressions manipulate numeric variables following the rules of algebra and have numeric values.  Integral expressions manipulate integer values  Floating point or decimal expressions manipulate floating point numbers  Relational expressions compare numerical values and have logical (boolean) values  Logical expressions combine boolean variables or expressions and yield boolean values

ExpressionsExpressions  An expression is a series of variables combined using unary and/or binary operations  An algebraic expression has a numeric value, a relational or logical expression has a boolean value  Example  X*Y is an algebraic expression  If the variables X and Y have different types then they must be converted to a common type before the expression X*Y is evaluated  The type of the value of the entire expression is the same as the common type the variables are converted to  An expression is a series of variables combined using unary and/or binary operations  An algebraic expression has a numeric value, a relational or logical expression has a boolean value  Example  X*Y is an algebraic expression  If the variables X and Y have different types then they must be converted to a common type before the expression X*Y is evaluated  The type of the value of the entire expression is the same as the common type the variables are converted to

Assignment Statements  Basic statement using the assignment operator to set the value of a variable to the value of an evaluated expression  A way to save the results of evaluating an expression  Form: resultvariable = expression;  Examples:  X = X + X * Y;  root = -3;  X += Y;  X -= 5;  The types of the resultvariable and expression must be the same.  Basic statement using the assignment operator to set the value of a variable to the value of an evaluated expression  A way to save the results of evaluating an expression  Form: resultvariable = expression;  Examples:  X = X + X * Y;  root = -3;  X += Y;  X -= 5;  The types of the resultvariable and expression must be the same.

Assignment operators  A = B assign value of expression B to variable A, store result in A  A += B add the value of expression B to variable A, store result in A  A -= B subtract the value of expression B from variable A, store result in A  A *= B multiply the value of expression B by variable A, store result in A  A /= B multiply the value of expression B by variable A, store result in A  A = B assign value of expression B to variable A, store result in A  A += B add the value of expression B to variable A, store result in A  A -= B subtract the value of expression B from variable A, store result in A  A *= B multiply the value of expression B by variable A, store result in A  A /= B multiply the value of expression B by variable A, store result in A

Rules for implicit conversion  The value of the expression in an assignment statement may be converted to the type of the resultvariable  The value of one of the operands of a binary operation may be converted before the operation is performed  Some conversions are done implicitly. These conversions are the widening conversions that always have valid results  byte to int  float to double  short to int  int to long  int to float  The value of the expression in an assignment statement may be converted to the type of the resultvariable  The value of one of the operands of a binary operation may be converted before the operation is performed  Some conversions are done implicitly. These conversions are the widening conversions that always have valid results  byte to int  float to double  short to int  int to long  int to float

Explicit conversion: The cast operation  In Java you can explicitly convert the type of a variable or expression within a larger expression using a cast operator  The value of the variable or expression is not changed  The value used in the larger expression is converted to the requested type  Sample expressions including casts  (int)(Floatone+Floattwo)  (float)Integerone + Float1 + Float2  (double)(int1)+double(short2) * double2  (float)(int1)/int2  In Java you can explicitly convert the type of a variable or expression within a larger expression using a cast operator  The value of the variable or expression is not changed  The value used in the larger expression is converted to the requested type  Sample expressions including casts  (int)(Floatone+Floattwo)  (float)Integerone + Float1 + Float2  (double)(int1)+double(short2) * double2  (float)(int1)/int2