Variables in Java x = 3;. What is a variable?  A variable is a placeholder in memory used by programmers to store information for a certain amount of.

Slides:



Advertisements
Similar presentations
CSci 1130 Intro to Programming in Java
Advertisements

JAVA Variables Declaring Variables Over writing Variables Data types Displaying contents of a Variable Naming a Variable Strings.
08/2012Tanya Mishra1 EASYC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817.
 Variables  What are they?  Declaring and initializing variables  Common uses for variables  Variables you get “for free” in Processing ▪ Aka: Built-in.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Objective: Dealing with data in C++ Agenda: Notes Essay Help.
Chapter 2 Review Questions
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,
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.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
String Escape Sequences
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
Storing data Getting data out Data types Ruby as a foundation Program Variables Click icon to hear comments (the download may take a minute)
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.
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
1 Please switch off your mobile phones. 2 Data Representation Instructor: Mainak Chaudhuri
National Diploma Unit 4 Introduction to Software Development Data types, variables and constants.
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.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CPS120: Introduction to Computer Science
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Lecture #5 Introduction to C++
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Primitive Variables.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
 Character set is a set of valid characters that a language can recognise.  A character represents any letter, digit or any other sign  Java uses the.
Primitive Data Types. Identifiers What word does it sound like?
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
Variables 1. What is a variable? Something whose value can change over time This value can change more than once over the time period (no limit!) Example:
CPS120: Introduction to Computer Science Variables and Constants.
1 MATERI PENDUKUNG TIPE DATA Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
Values, Types, and Variables. Values Data Information Numbers Text Pretty much anything.
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
Variables in VB. What is a variable? ► A named memory location that stores a value.
Primitive Data Types 1 In PowerPoint, point at the speaker icon, then click the "Play" button.
MIT AITI Lecture 2 The of Java In the following lectures you will learn the basic structures that make up the Java programming language: »Variables.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.
Data Handling in Algorithms. Activity 1 Starter Task: Quickly complete the sheet 5mins!
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
A Sample Program #include using namespace std; int main(void) { cout
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Review by Mr. Maasz, Summary of Chapter 2: Starting Out with Java.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
Copyright © Texas Education Agency, Computer Programming Variables and Data Types.
Fundamentals 2.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 2 Basic Computation
Variables Mr. Crone.
Multiple variables can be created in one declaration
Chapter 2 Basic Computation
IDENTIFIERS CSC 111.
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.
Introduction to Primitive Data types
Chapter 2: Java Fundamentals
Chapter 2: Java Fundamentals
The Data Element.
Variables Here we go.
The Data Element.
Data Types and Maths Programming Guides.
Variables and Constants
Introduction to Primitive Data types
Presentation transcript:

Variables in Java x = 3;

What is a variable?  A variable is a placeholder in memory used by programmers to store information for a certain amount of time.  Similar to math class, variables hold values and can be involved in operations.  For example, a numeric variable can be multiplied by 2. Or a text variable might be joined with another text variable.

Properties of Variables  Variables must have a unique identifier (name).  The name must be alphabetic and not contain any spaces.  A variable can only contain one value at a time. This value can be overwritten as many times as you want.  Variables also have a specific data type. The data type indicates what kind of data the variable can hold.  Behind the scenes, the data type also indicates how much memory is required to store the data.

Data Types  We will be working with five primary data types: TypeMeaningExample Values int Integer in the range -2,147,483,648 to 2,147,483,647. 4, -6, 0, doubleA decimal number , , char A single character. This can be a letter, a number, a space, or special characters like punctuation. char values are surrounded by single quotations. ‘a’, ‘1’, ‘.’ boolean A type that can only hold the value true or false. true, false StringStrings are used to hold text such as sentences, serial numbers, etc. “Bob” “Holy cow I love milk!” “My phone number is 911!”

Declaring a Variable To create a variable, we must declare it as follows: ; The following are all valid variable declarations. int age; // Creates an int variable called age. String name; // Creates a String variable called name. double PI; // Creates a double variable called PI. None of the above variables hold any values! They simply represents boxes of memory that been reserved and named.

Assigning a Value to a Variable To assign values to a variable, simply apply algebra! After a variable has been declared, use the following to assign values: = ; Note that the type of the variable must match the type of the value. We cannot assign a String value to an int variable!

Assigning a Value to a Variable - Example int age; // Creates an int variable called age. String name; // Creates a String variable called name. double PI; // Creates a double variable called PI. age = 16; name = “Mitchell”; PI = 3.14; Note that only String values are contained in quotations.

Printing the Value of a Variable To print the value of a variable to the screen, use the System.out.print() statement using the variable’s name as an argument. age = 5; System.out.println(age);// Prints 5. You can also print both text and variables to the screen using the “+” symbol. age = 5; System.out.println(“My age is “ + age);