Variables Title slide variables.

Slides:



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

CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer Science NJIT.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Data Types. Every program must deal with data The data is usually described as a certain type This type determines what you can do with the data and how.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
National Diploma Unit 4 Introduction to Software Development Data types, variables and constants.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
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.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
CPS120: Introduction to Computer Science
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
Introduction to Programming
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
COMP Primitive and Class Types Yi Hong May 14, 2015.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CSI 3125, Preliminaries, page 1 Data Type, Variables.
CPS120: Introduction to Computer Science Variables and Constants.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
CIS 338: VB Variables Dr. Ralph D. Westfall April, 2011.
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.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Bill Tucker Austin Community College COSC 1315
Computer Organization and Design Pointers, Arrays and Strings in C
A variable is a name for a value stored in memory.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
Introduction to Computer Science / Procedural – 67130
Lecture 5: Some more Java!
Java Primer 1: Types, Classes and Operators
Object Oriented Programming
Multiple variables can be created in one declaration
Variables and Primative Types
Computer Science 3 Hobart College
L7 – Assembler Directives
Agenda Warmup Lesson 2.5 (Ascii, Method Overloading)
Instructor: Ioannis A. Vetsikas
Java Programming: From Problem Analysis to Program Design, 4e
Engineering Innovation Center
Lecture Set 4 Data Types and Variables
Variables ,Data Types and Constants
Chapter 2.
IDENTIFIERS CSC 111.
Object Oriented Programming COP3330 / CGS5409
Java - Data Types, Variables, and Arrays
Popping Items Off a Stack Lesson xx
Variables ICS2O.
Pointers, Dynamic Data, and Reference Types
Week 9 – Lesson 1 Arrays – Character Strings
Coding Concepts (Sub- Programs)
We’re moving on to more recap from other programming languages
Chapter 2: Java Fundamentals
Coding Concepts (Data- Types)
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
Introduction to Primitives
Introduction to Primitives
Java Programming Language
Java Basics Data Types in Java.
Primitive Types and Expressions
Java’s Central Casting
Unit 3: Variables in Java
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Pointers, Dynamic Data, and Reference Types
Variables and Constants
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Variables Title slide variables

Content What is a variable? Data types Mutability (constant vs variable) Scope and visibility Preallocation Content of the presentation: What is a variable? Data types Mutability (constant vs variable) Scope and visibility Preallocation

What is a variable Location where a value is stored Variable: a Address Value 000 NULL 001 5 002 8 003 Variable: a A variable is the location of where a value is stored. So for the computer the variable represent a physical address in the memory, in which the value (that we associate with the variable) is stored.

What is a variable Location where a value is stored Every variable has: Type (statically typed languages) Variable name (identifier) Value Every variable has an associated type (in statically typed languages), a variable name and a value. So in the java example of ‘int a = 5;’, int is the type, a is the variable name and 5 is the value.

Data types Integer (int) Floating point number (float) Name Shorthand What Integer int Whole numbers Floating-point number float 32-bit number with digits after decimal place Double precision float double 64-bit number with digits after decimal place Boolean (logical in MATLAB) bool (logical in MATLAB) Value that’s either true or false (0 or 1) Array (1D is a vector) ~ A list of variables, depending on the language this can be further divided into lists, arrays, vectors, queues, etc. Character char A single (ASCII) character String string or str A list or array of characters Integer (int) Floating point number (float) Double precision float (double) There are many different types, this slide lists the most important ones: Integer (int): whole numbers Floating-point number (float): 32-bit number with digits after decimal place Double precision float (double): 64-bit number with digits after decimal place Boolean (bool; logical in MATLAB): Value that’s either true or false (0 or 1) Array (1D usually called a vector, sometimes list): A list of variables, depending on the language this can be further divided into lists, arrays, vectors, queues, etc. Character (char): A single (ASCII) character String (string or str): A list or array of characters

Typing Statically typing Languages: C (Igor), Java, PEARL Variable has one type Type preassigned Programming languages can roughly be divided into two: statistically typed languages and dynamically typed languages Statistically typed languages, such as C, Java and PEARL have the type associated with the variable. The type is preassigned, usually explicitly in the language (etc. int a = 5; (Java), where a is associated with the type int)

Typing Statically typing Dynamically typing Languages: C (Igor), Java, PEARL Variable has one type Type preassigned Dynamically typing Languages: MATLAB, Python, R, Perl Value has one type Type of variable may change Secondly there are dynamically typed languages, such as MATLAB, Python, R and Perl. They have the type associated with the value, not the variable, so the type of the variable may change.

Type casting Explicitly converting type of a variable or value Only in dynamically typed languages Especially relevant for numerical types Reasons for casting: Increase precision/memory Reduce precision Cast base type to derived type Type casting is the act of transforming the type of a variable or value. The type of a variable can only be changed in dynamically typed languages. I find this especially useful for numerical types, for example if I want to increase my precision (cast from int to double, or double to float) or decrease my precision (double to int). Another application is to cast base types to derived types (object-oriented programming)

Mutability Variable Constant Value dynamically assigned Value may change during run-time Constant Value assigned at initialization Value cannot be changed In explicit languages, different levels of mutability exist for variables. A variable is dynamically assigned, and thus the value may change during run-time. An constant is assigned at initialization and cannot be changed during run-time. An identifier shows the mutability (i.e. const float PI = 3.14; is a constant).

Mutability Depending on the language more subtypes available Other languages don’t have constant modifiers Workarounds if needed Use naming conventions: ALL_CAPS Some languages offer more subtypes of mutability (e.g. java has protected). Other languages don’t allow constants. There are usually workarounds, but otherwise you could use the naming convention of using ALL_CAPS for constants.

Mutability: why use constants? Users are stupid! Reminder for yourself Physical constants (pi, N of Avogadro, etc) Why would you use constants? If your program is going to be used by users, they will find a way to mess up variables. It is also good as a reminder for yourself that certain variables should not be changed. Lastly, it is often used for physical constants such as pi, the number of Avogadro, etc.

Scope Lifetime of a variable MATLAB is an exception Important for memory allocations The scope determines the lifetime of a variable; or another way to look at it, were the computer can read the variable. Matlab is an exception and will have its own slide. Scope is important for memory allocations, and it’s good to keep an eye on it.

Scope Output: ? In this Java example, a for-loop is shown where the println displays the value of i. The question is what the output in each case is. For(int I = 0; I < 10; i++) { System.out.println(i); } Output: ?

What is happening? Memory cleared: Memory for an int allocated Initialization: i is created Memory cleared: Space in memory available for new assignments In the same example, it shows that I is created within the for-loop in the line ‘int I = 0;, at which point memory is allocated for it. After the loop has ran, this memory will be cleared, so I no longer exists in the second println. The scope of I is the for-loop, in only exists inside the forloop, not outside of it. i no longer exists

Function example Output: ? Output: ? The same goes for funcions. In this example a function myFunction is declared, which takes an integer and adds 100 to it, after which it returns it. If I make a variable int a = 100 and apply the function to it: res = myFunction(a);, what is the value of res and the value of a afterwards? Res will be 200, while a will remain 100.

Levels of scope Blocks (not all languages!) Functions Module/Class Global Different levels of scope exist. Some languages have block scope, where every block of code (usually denoted using curly brackets), has its own scope. Functions have their own scope (variables within a function cannot be accessed unless they are exported. Modules and classes usually have their own scope. Lastly the global scope means a variable is always available.

Scope modifiers Most language allow you to make a variable global Automatic if outside function Only use in dire need! In most languages there are ways to make the variables global, or define the scope of a variable. Usually you do not want to make a variable global, so only use it in dire need. This is matlab example using the function: function addValue(v) global i; i = i + v; end When executing the lines: i = 0; addValue(5); disp(i) addValue(3); What values of I will be displayed? The values will be added to i, because it is global, so this global I will be modified. It will thus be 5 and then 8.

MATLAB Workspace shows scope Variables don’t get deleted after for-loop Each function has it’s own workspace Matlab is a special case, because the scope is explicit in the workspace. It furthermore doesn’t delete variables after a loop. However, it does have function scope, where each function has its own workspace, showing variables in its scope (access using debug function). Variables initiated, so available in scope

A word on preallocation You might have gotten this error (MATLAB): What’s going on here? A word on preallocation. You might have gotten a MATLAB warning that you have a variable that changes size on every loop iteration. This warning suggests you should preallocate.

Allocation example Allocate space for empty list (say 8 bit) Add int to the allocated space, so need 8 bits more Memory not always linear! An example of allocation in matlab: newArray = []; For I = 1:10 newArray = [newArray, i]; End In this case, the size of newArray changes on each iteration. After initialization it’s an empty list (roughly 8 bits). On each iteration, the computer has to find an extra 8 bits for the value that is added. Since memory is not always linear, this can take a lot of overhead. On small iterations like this, it’s not a big problem, but it will be on very big iterations (e.g. a million times).

What to do? No problem if small array Preallocate the largest possible number of values What can you do about this? As said, it’s not a big problem for smaller arrays and smaller iterations, so usually it’s fine to leave it. However, a better practice is the allocate the largest amount of values you think will be added to the array, e.g.: newArray = nan(10,1);

What to do? No problem if small array Preallocate the largest possible number of values Preallocate in chunks (i.e. every 1000 values add an extra 1000 values) Another solution is to preallocate in chunks. So instead of letting the computer find memory on each iteration, preallocate say a 1000 values, and each time you hit this limit, preallocate another 1000.

Summary What is a variable? Data types Location where a value is stored Data types Statically vs dynamically Type casting Mutability (constant vs variable) Single value for a variable Scope and visibility Memory allocation MATLAB: use functions! Preallocation May speed up your code Summary, things we discussed: What is a variable? Location where a value is stored Data types Statically vs dynamically Type casting Mutability (constant vs variable) Single value for a variable Scope and visibility Memory allocation MATLAB: use functions! Preallocation May speed up your code