Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
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.
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.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Constants Variables change, constants don't final = ; final double PI = ; … area = radius * radius * PI; see Liang, p. 32 for full code.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
22-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
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.
Fundamental data types Horstmann Chapter 4. Constants Variables change, constants don't final = ; final double PI = ; … areaOfCircle = radius *
01- Intro-Java-part1 1 Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2008.
Objectives You should be able to describe: Data Types
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Java Data Types. Primitive Data Types Java has 8 primitive data types: – char: used to store a single character eg. G – boolean: used to store true or.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Mathematical Calculations in Java Mrs. G. Chapman.
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.
Primitive Variables.
Chapter 2 Variables.
Working With Objects Tonga Institute of Higher Education.
Mathematical Calculations in Java Mrs. C. Furman.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
COMP Primitive and Class Types Yi Hong May 14, 2015.
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.
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
1 2. Program Construction in Java. 01 Java basics.
Georgia Institute of Technology Introduction to Java, and DrJava part 1 Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
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.
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.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Chapter 2 Variables.
Chapter 2 Basic Computation
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Computer Science 3 Hobart College
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2 Basic Computation
IDENTIFIERS CSC 111.
Introduction to Java, and DrJava part 1
Chapter 2 Variables.
Building Java Programs
Introduction to Java, and DrJava
Introduction to Primitives
Introduction to Primitives
Primitive Types and Expressions
Unit 3: Variables in Java
Introduction to Java, and DrJava
Introduction to Java, and DrJava part 1
Chapter 2 Variables.
Building Java Programs
Presentation transcript:

Data Tonga Institute of Higher Education

Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember data: A loaf of bread was sold to Sione Latu on 14/02/04 for T$1.00.  Customer Name: Sione Latu  Date Sold: 02/14/04  Item Sold: Loaf of Bread  Item Cost: T$1.00 Variable – Place in the computer’s memory where information is stored.

Value Types vs. Reference Types A variable stores a value or object. Value Types only store values  int only stores:  boolean only stores: True False Reference Types only store references to an object  Object - A self-contained entity that contains data and procedures to manipulate the data.  A reference is a location in memory  The location is the starting point of an object

Primitive Data Types TypeSize (bits)Value Range byte8-128 to 127 short16-32,768 to int32-2,147,483,648 to 2,147,483,647 long x to 9.2 x float x to 3.4 x double x to 1.8 x char160 to 65,535 boolean1True or False Primitives - The basic types supported by the programming language. Use lowercase for primitive data types in code

Primitive Data Type Selection Practice What is a good data type for:  Someone’s age?  A customer’s identification number for a video rental store in Nuku’alofa?  A very large number with decimals?  The price of an item? How to choose the right primitive variable type.  Byte, short, int and long store whole numbers.  Float and double store fractions. Double is more accurate, so if accuracy is important, use double.  If you pick something that is too big, you’re wasting memory space. Wasting memory space slows things down.  If you pick something that is too small, then you’re going to crash.  If you need a character, use char.  If you need a true/false value, use boolean.

Using Primitive Variables 2 Steps to using variables 1. Declare the variable 2. Initialize the variable

Declaring Primitive Variables Declare the variable – Tell the computer to reserve a space in memory for the variable. You need to tell the computer 2 things: 1. Name of the variable 2. Type of the variable (What kind of variable you have) Primitive types  byte  short  int  long  float  double  char  boolean Type Name

Declaring Primitive Variables Guidelines Use a name that is easy to remember.  Do not use x, y, z Variable names must start with a letter, underscore or dollar sign. Begin variables with a lowercase character. Then, use a capital letter for each word. Examples  firstName  customerID

Initializing Primitive Variables Initialize the variable – Assign an initial value to a variable.  Char values must be enclosed in single quotes.  String values must be enclosed in double quotes.  Boolean value should be True or False Initial Value

Declaring and Initializing Primitive Variables in 1 line You can declare and initialize a variable in 1 line.

Demonstration Declaring and Initializing Variables

Converting Data Types Smaller to Larger It is possible to automatically move a value from a smaller data type to larger data type Example Maximum Short Value Maximum Long Value Is 9.2 x 10 15

Converting Data Types Larger to Smaller If a value from a larger data type is moved into a smaller data type, the value may be too big for the new data type This will cause an error Using this: Results in: Do not do this! Maximum Short Value is 32767

Converting Data Types Force conversion of data types by casting it. To cast, use this format: ( ) If the Cast function isn’t able to convert the value, you may not get the answer you expect. int is bigger than a byte Without this, we would get an error Forcing a cast from a larger data type to a smaller data type is not recommended! But the final value Is -24, which is strange!

Demonstration Converting Data Types

Arithmetic Operators  Declare and Initialize x, y and z  Get values from x and y  Adds x and y together  Assigns the sum of x an y to z OperatorMeaningExample +Addition Subtraction22 – 11 *Multiplication5 * 6 /Division21 / 3 %Modulus12 percent 2

String Concatenation Addition  You can add strings. Adding strings is called concatenation.  This converts all non strings into strings. Make sure you add your numbers before converting them to strings.

Demonstration String Concatenation

Division Tricky because result may not be an integer. In this case the number gets cut off. NOT rounded! Even having a result of float doesn’t work. Because Java has rules for dealing with data types. The result of a division between two integers is always an integer. So the float just added a.0 to it. To get around this, we need to divide two floats!

Demonstration Division

Multiplication and Modulus Multiplication  Use asterisk (*) instead of x Modulus  The remainder of a division  Can be used to determine whether a number is divisible by another number  Can also determine if a number is even or odd

Order of Operations When you have a lot of operations, they are performed in a certain order.  Parentheses ()  Multiplication or Division operations from left to right  Addition or Subtraction operations from left to right. Examples:  / 3 = = 12  ( ) / 3 = 18 / 3 = 6