Variables. Store information needed by the program

Similar documents
Basics of Java Programming

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

Getting started with Java

Full file at

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

The C++ Language. Arizona State University 1

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

Values and Variables 1 / 30

Full file at

Computational Expression

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

An Introduction to Processing

Declaration and Memory

Chapter 2 Elementary Programming

Program Fundamentals

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

MODULE 02: BASIC COMPUTATION IN JAVA

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Lesson 2A Data. Data Types, Variables, Constants, Naming Rules, Limits. A Lesson in Java Programming

Visual C# Instructor s Manual Table of Contents

A First Program - Greeting.cpp

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

BASIC ELEMENTS OF A COMPUTER PROGRAM

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Computer Components. Software{ User Programs. Operating System. Hardware

3. Java - Language Constructs I

Reserved Words and Identifiers

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Java Notes. 10th ICSE. Saravanan Ganesh

LECTURE 3 C++ Basics Part 2

Algorithms and Programming I. Lecture#12 Spring 2015

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

VARIABLES AND TYPES CITS1001

Java Basic Datatypees

Datatypes, Variables, and Operations

JAVA Programming Fundamentals

DATA TYPES AND EXPRESSIONS

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

Define a method vs. calling a method. Chapter Goals. Contents 1/21/13

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

Lecture 2 Tao Wang 1

Chapter 12 Variables and Operators

4 Programming Fundamentals. Introduction to Programming 1 1

CMPT 125: Lecture 3 Data and Expressions

Computer System and programming in C

Ex: If you use a program to record sales, you will want to remember data:

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

egrapher Language Reference Manual

Arithmetic Expressions in C

Chapter 2: Using Data

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Fundamentals of Programming CS-110. Lecture 2

Java Fall 2018 Margaret Reid-Miller

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Introduction to Programming EC-105. Lecture 2

Chapter 2: Basic Elements of C++

CEN 414 Java Programming

BTE2313. Chapter 2: Introduction to C++ Programming

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Chapter 2 Primitive Data Types and Operations. Objectives

Pointers (continued), arrays and strings

Chapter 2: Introduction to C++

CSI33 Data Structures

Java Programming Fundamentals. Visit for more.

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

COMP Primitive and Class Types. Yi Hong May 14, 2015

CS Programming I: Primitives and Expressions

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Types and Expressions. Chapter 3

Informatica e Sistemi in Tempo Reale

Java Module Lesson 2A Practice Exercise

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Lec 3. Compilers, Debugging, Hello World, and Variables

Pointers (continued), arrays and strings

Fundamentals of Programming

Variables, Constants, and Data Types

Data types Expressions Variables Assignment. COMP1400 Week 2

CHAPTER 7 OBJECTS AND CLASSES

4. If the following Java statements are executed, what will be displayed?

Programming with Java

Types, Values, Variables & Assignment. EECS 211 Winter 2018

Accelerating Information Technology Innovation

Chapter 2: Data and Expressions

Chapter 2: Using Data

Lecture 1. Types, Expressions, & Variables

LECTURE 02 INTRODUCTION TO C++

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

Princeton University. Computer Science 217: Introduction to Programming Systems. Data Types in C

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

ARG! Language Reference Manual

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Transcription:

Variables Store information needed by the program Must have a TYPE int - can only store a number without a fractional part float, double - can store any number, with or without a fractional part (double has more precision) char individual character (letter,digit,punctuation etc.) String - text data boolean store value true or false More later on values of true/false Must have a VALUE, the value can change as the program runs if you don t provide an initial value, the compiler will supply one for you. That initial value is zero for the primitive types, null for strings. 1

Literal values Every primitive type (and Strings) can be initialized using a literal of the type. Here are examples of literal values: Type Literal value Example initialization String Hello World String greeting = Hello World ; char A char letter = A ; boolean true boolean flag = true; int 1024 int kilo = 1024; double 3.154159 double pi = 3.14159; Note the difference between a char literal and a String literal. A char literal is in single quotes while a String literal is in double quotes. A String may consist of only one character but a char cannot consist of multiple characters. String s = A ; // LEGAL char c = AB ; // ILLEGAL 2

Must have a NAME Naming Variables the name must start with an alphabetic (or _ ) the name may include alphabetics, digits, and underscores the name should indicate the purpose of the variable in the overall design and purpose of the program lousy variable names - larry, moe, curly, ah67, gh_78 reasonable variable names - width, filename, cursorposition, count, response You should not have a program that consists entirely of single letter variables names. It is too hard to read. When code is being explained in class I may however use single letter variable names, not because it is good programming style but because I don t have a lot of room on the slides. Java strongly encourages variables to be named all lower case except as noted below: If your variable name is a multi-word name then make the first word of the name lowercase and subsequent words start with a capitol. Examples of multi-word variable names: filename remoteuseraddress phonebook 3

the = operator assigns Assignment the value on the right to the variable on the left WARNING! Not to be confused with the == operator which compares (asks are they equal? ). it is extremely counter-intuitive because it assigns from the right to the left and we read from the left to the right. the left hand side MUST be a variable the right hand side can be a variable or an expression int x = 0, y = 3, z = 4; z = 10; // z is given the value of 10 x = y + 5; // x is given the value of y + 5 (i.e. 8) x = x + 1; // takes the current value of x and increments it by one at the end of these 3 statements, what are the values of x, y, and z? 10 = z; // WRONG, can t have number on left hand side of = 3 + y = y; // WRONG, can t have expression (3 + y) on left hand side 4

Named Constants A magic number is a numeric constant that appears in your code without explanation. e.g. answer = rate * 1.67; // what is the significance of 1.67? instead make a named constant using keyword final // this is the scheduled rate increase final float RATE_INCREASE = 1.67; answer = rate * RATE_INCREASE ; The value of a named constant cannot be assigned another value. ------------------------------------------------------------------------ Java strongly encourages use of ALL CAPS for constants. Multi word constant names can have an UNDERSCORE between words 5

Conversions Implicit Conversion float op float is float float op int is float int op float is float int op int is int e.g. int op float is float 5 + 13.5 is 18.5 int op int is int 5 + 8 is 13 3.0 / 4 is 0.75 3 / 4 is 0 3/4 * 10 is 7.5 or 0???? 6

Practice If a number has a decimal point, it s a float. No decimal point, it s an int. int i,j; double x,y; String numstr = 456973 ; String word = catnip ; String str; i = 5 + 9; y = 5.9 + 8; i = 5.9 + 8; y = 5.0 + 4.0; y = 10 / 4; y = 10 / 4.0; i = 10 / 4; y = 5 / 10; i = 5 / 10; y = 5.0 / 10; y = y % 10; y + i = y; i = numstr.length(); y = numstr + 10; str = word.substring(1); str = word + numstr.substring(2,2); str = y.length() + i.length(); 7

Operator Precedence * / % are performed before + and - operations performed left to right 3 + 5 * 5 is? 8 % 3 + 9 is? 2 % 6 * 10 is? 5 + 4 + 3 / 3 is? (5 + 4 + 3) / 3 is? 8

Shortcuts x = x + 5; same as x += 5; x = x * 10; same as x *= 10; x = x - 9; same as x -= 9; x = x / 10; same as x /= 10; x = x % 3; same as x %= 3; x = x + 1; same as x ++; // post incr same as ++ x; // pre incr x +=1; 9