We think you have liked this presentation. If you wish to download it, please recommend it to your friends in any social system. Share buttons are a little bit lower. Thank you!
Presentation is loading. Please wait.
Modified over 4 years ago
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Data in a Java Program In this chapter we are going to talk about how data is stored in a Java program There are two basic kinds of data in a Java program –Primitive types (e.g. numbers) –Reference types (e.g. objects)
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Primitive Data Types Java has 8 primitive types that are part of the language Numeric types –Integers : byte, short, int, long –Real numbers : float, double Characters - char Boolean - boolean
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Integers Integers are stored using their base-two value –See section 1.8 if you are interested in the details Different types take different amounts of memory and store different ranges of values Default integer type is int –Literal representation is a number with no decimal point in it 123-52 For long, use an L after the number (123456789L)
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Real Numbers Also called floating point numbers Use the IEEE 754 standard –See appendix I for details Two formats for different ranged and precision Default type is double –Literal representation is a number with a decimal point 1.23-.045 1.6e-19 6.02E23 For float, use an F after the number (1.23F)
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Characters A char variable can store a single character Literal representation is the character enclosed in single quotes ('a' or 'Z') –Use the escape character for characters that need to be handled specially ('\n' or '\'') Characters are stored in memory as their Unicode values (16 bits)
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Boolean type The boolean type is used for data that can only have two values Literal values are true and false We'll talk more about these when we need them
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Primitive Data Types
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Arithmetic in Java Java has 5 binary operators to do arithmetic + Addition -Subtraction * Multiplication / Division works differently for integer and floating point numbers % Modulo
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Integer Division When both operands of the / operator are integers, the result will be an integer Any fractional part will be truncated –2/3 ---> 0 –4/3 ---> 1 –19/10 ---> 1 –19999/10000 -> 1
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Modulo Operator The % operator gives the remainder from an integer division –10 % 7 ---> 3 –9 % 10 ---> 9 –4 % 2 ---> 0
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Precedence and Associativity What happens when an expression has several operators? 5 + 3 * 7 Precedence rules to tell you what order the operations will be done Associativity rules tell you the order for several operations that have the same precedence
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Precedence 1.Parenthesized expressions 2.Negation 3.Multiplication, division and modulo 4.Addition and subtraction 3 + 5 * 7 will be 38 because multiplication is done first
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Associativity Arithmetic operators with same precedence associate from left to right a * b / c % d ---> (((a * b) / c) % d) 8 / 2 / 2 ---> 2
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 String Concatenation The + operator has a second meaning in Java If + has two strings as operands, it will concatenate the two strings –"top" + "cat" ---> "topcat" If either operand is a string, the result is a string –"answer " + 10 ---> "answer 10"
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Displaying Numerical Data The print() and println() methods can have numbers instead of strings for their argument –These are exampled of overloaded methods System.out.println( 6) will print the number 6. System.out.println( 6 + 9) will print the number 15.
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types The eight Java primitive data types are: byte short int long float double.
CSci 1130 Intro to Programming in Java
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.
Types and Arithmetic Operators
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.
Primitives in Java Java has eight primitive types –boolean –integral types: signed: long, int, short, byte unsigned: char –floating point types: double,
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
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.
Introduction to Computers and Programming Lecture 7:
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
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.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Data types and variables
© 2019 SlidePlayer.com Inc. All rights reserved.