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.

Slides:



Advertisements
Similar presentations
© 2007 Lawrenceville Press Slide 1 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5;
Advertisements

Lecture 5 Types, Expressions and Simple I/O COMP1681 / SE15 Introduction to Programming.
© Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.
1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,
CSci 1130 Intro to Programming in Java
1 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1 Operators and Expressions.
COMP 14 Introduction to Programming
Introduction to Programming G51PRG University of Nottingham Revision 1
Today’s lecture Review of Chapter 1 Go over homework exercises for chapter 1.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
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.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
1 Chapter 3 Arithmetic Expressions. 2 Chapter 3 Topics l Overview of Java Data Types l Numeric Data Types l Declarations for Numeric Expressions l Simple.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
String Escape Sequences
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
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 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.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
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.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Chapter 2 Variables.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
COMP Primitive and Class Types Yi Hong May 14, 2015.
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.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
Data Types, Variables, and Arithmetic Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
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.
Data Types References:  Data Type:  In computer science and computer programming, a data type or simply type is a.
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
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.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 2 Variables.
Chapter 2 Basic Computation
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Object Oriented Programming
Multiple variables can be created in one declaration
Java package classes Java package classes.
Type Conversion, Constants, and the String Object
Chapter 2 Edited by JJ Shepherd
Introduction to Java, and DrJava part 1
Chapter 2 Variables.
Chapter 2: Java Fundamentals
Primitive Types and Expressions
Unit 3: Variables in Java
Names of variables, functions, classes
Module 2 Variables, Data Types and Arithmetic
Chapter 2 Variables.
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

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 can change while the program runs n = 25 name = “john” a = 75.15

6-3 Variables (cont’d) Variables can be of different data types: int, char, double, boolean, etc. Variables can hold objects; then the type is the class of the object. The programmer gives names to variables. Names of variables usually start with a lowercase letter. Variable names cannot start with a number Variable names cannot be Java reserved words

Declare A variable must be declared before it can be used: We have to let Java know what types of information it will store int count; String name; double deposit;

Assignment of value The assignment operator = sets the variable’s value: count = 10; name = “Geno Smith”; deposit = 60.75;

Declare and initialize You can declare a variable and initialize it at the same time int count = 10; String name = “Geno Smith”; double deposit = 60.75;

Primitive data types in Java byte short int-----APCS long float double ----APCS boolean ---APCS char-----APCS

Primitive data types in Java byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation.arrays short: The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.

int: By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of and a maximum value of In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of Use the Integer class to use int data type as an unsigned integer. See the section The Number Classes for more information. Static methods like compareUnsigned, divideUnsigned etc have been added to the Integer class to support the arithmetic operations for unsigned integers.Integer long: The long data type is a 64-bit two's complement integer. The signed long has a minimum value of and a maximum value of In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of Use this data type when you need a range of values wider than those provided by int. The Long class also contains methods like compareUnsigned, divideUnsigned etc to support arithmetic operations for unsigned long.Long

float: The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead. Numbers and Strings covers BigDecimal and other useful classes provided by the Java platform.Floating-Point Types, Formats, and Valuesjava.math.BigDecimal Numbers and Strings double: The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. For decimal values, this data type is generally the default choice.Floating-Point Types, Formats, and Values

boolean: The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that's precisely defined. char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

6-12 Strings String is not a primitive data type Strings work like any other objects, with two exceptions: Strings in double quotes are recognized as literal constants + and += concatenate strings (or a string and a number or an object, which is converted into a string) “John” + “Taylor” = “JohnTaylor” “22” + “47” 2247 "Catch " + 22 "Catch 22"

6-13 Arithmetic Operators: +, -, /, *, % The precedence of operators and parentheses is the same as in algebra m % n means the remainder when m is divided by n (for example, 17 % 5 is 2; 2 % 8 is 2) % has the same rank as / and * Same-rank binary operators are performed in order from left to right

6-14 Arithmetic (cont’d) The type of the result is determined by the types of the operands, not their values; this rule applies to all intermediate results in expressions. If one operand is an int and another is a double, the result is a double; if both operands are ints, the result is an int.

6-15 Arithmetic (cont’d) Caution: if a and b are ints, then a / b is truncated to an int… 17 / 5 gives 3 3 / 4 gives 0 …even if you assign the result to a double: double ratio = 2 / 3; The double type of the result doesn’t help: ratio still gets the value 0.0.

6-16 Arithmetic (cont’d) To get the correct double result, use double constants or the cast operator: double ratio = 2.0 / 3; double ratio = 2 / 3.0; int m =..., n =...; double factor = (double)m / (double)n; double factor = (double)m / n; double r2 = n / 2.0; Casts

6-17 Arithmetic (cont’d) A cast to int can be useful: int ptsOnDie = (int)(Math.random() * 6) + 1; int miles = (int)(km * ); Returns a double Converts kilometers to miles, rounded to the nearest integer

6-18 Arithmetic (cont’d) Compound assignment operators: a = a + b; a += b; a = a - b; a - = b; a = a * b; a *= b; a = a / b; a /= b; a = a % b; a %= b; Increment and decrement operators: a = a + 1; a++; a = a - 1; a -- ; Do not use these in larger expressions

6-19 From Numbers to Strings The easiest way to convert x into a string is to concatenate x with an empty string: String s = x + ""; The same rules apply to System.out.print(x) 'A' Math.PI "A" "123" " - 1" "0.1" "3.14" " " Empty string