Variables & Operators 1 Variables Store information needed by the program Must have a TYPE int - can only store a number without a fractional part float,

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

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.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this.
Lecture 2: Variables and Expressions Yoni Fridman 6/29/01 6/29/01.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
String Escape Sequences
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
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.
 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)
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
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.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
 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.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
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.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Chapter 2 Variables.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
COM S 207 Literal, Operator, and Expression Instructor: Ying Cai Department of Computer Science Iowa State University
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.
CompSci Primitive Types  Primitive Types (base types)  Built-in data types; native to most hardware  Note: not objects (will use mostly first.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
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.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
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.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Fundamentals 2.
Chapter 2 Variables.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Java Variables and Types
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Data Types, Identifiers, and Expressions
Multiple variables can be created in one declaration
Variables and Primative Types
Identifiers - symbolic names
Java Programming: From Problem Analysis to Program Design, 4e
Variables Store information needed by the program Must have a TYPE
IDENTIFIERS CSC 111.
Data Types, Identifiers, and Expressions
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Chapter 2: Basic Elements of Java
Chapter 2 Variables.
Chapter 2: Java Fundamentals
elementary programming
In this class, we will cover:
Primitive Types and Expressions
Module 2 Variables, Data Types and Arithmetic
Chapter 2 Variables.
Chapter 2 Primitive Data Types and Operations
Variables and Constants
Presentation transcript:

Variables & Operators 1 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.

Literal values Every primitive type (and Strings) can be initialized using a literal of the type. Here are examples of literal values: TypeLiteral value Example initialization String“Hello World”String greeting = “Hello World”; char‘A’char letter = ‘A’; booleantrueboolean flag = true; int 1024int kilo = 1024; double double pi = ; 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 Variables & Operators 2

3 Naming Variables Must have a NAME 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

Variables & Operators 4 Assignment the = operator assigns 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

Variables & Operators 5 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

Variables & Operators 6 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 is 18.5 int op int is int is / 4 is / 4 is 0 3/4 * 10 is 7.5 or 0 ????

Variables & Operators 7 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 = ; i = ; y = ; 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();

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

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