Type Conversion, Constants, and the String Object

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

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.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
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.
Primitive Data Types There are exactly eight primitive data types in Java four of them represent integers: byte (class Byte), short (class Short), int.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
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.
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
***** SWTJC STEM ***** Chapter 2-3 cg 29 Java Operators Recall Java’s programming components: Packages - Collection of classes (Programs) Classes - Collections.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
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.
Sahar Mosleh California State University San MarcosPage 1 A for loop can contain multiple initialization actions separated with commas Caution must be.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Mathematical Calculations in Java Mrs. G. Chapman.
Mathematical Calculations in Java Mrs. C. Furman.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
COMP Primitive and Class Types Yi Hong May 14, 2015.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
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.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Numeric Data Types There are six numeric data types: byte, short, int, long, float, and double. Sample variable declarations: int i, j, k; float numberOne,
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
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.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
© 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
Lecture 3 Java Operators.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Object Oriented Programming
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Primitive and Reference Data Values
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2 Basic Computation
Primitive and Reference Data Values
Type Conversion, Constants, and the String Object
Increment and Decrement
Lecture 3 Expressions Richard Gesick.
Numerical Data Types.
Chapter 2: Basic Elements of Java
Arithmetic Expressions & Data Conversions
Chapter 2 Variables.
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Data Types and Expressions
In this class, we will cover:
Primitive Types and Expressions
Unit 3: Variables in Java
Chapter 2 Variables.
Chapter 2: Java Fundamentals cont’d
Data Types and Expressions
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

Type Conversion, Constants, and the String Object CS0007: Introduction to Computer Programming

Review Integer Data Types Floating-Point Data Types byte short int long Floating-Point Data Types float double String Concatenation is… the process of appending to the end of a string. String Concatenation Operator +

Review Entities of the boolean data type can have one of two values: true false Entities of the char data type can take one what kind of values: Single Characters Characters are represented in memory as… 2 bytes integers Character Literals are encapsulated in Single Quotes (') String Literals are encapsulated in Double Quotes (")

Review You can declare multiple variables on the same line by separating them with a… Comma (,) You can assign a variable a value in the same line as you declare it through a process called… Initialization Some Arithmetic Operators +, -, *, /, % You can do more complex mathematical operations like square root and exponents with the Math class.

Combined Assignment Operators Often you want to do a mathematical operation to the value in a variable and store the result back into the same variable: Computing the sum: sum = sum + newNumber; Counting count = count + 1; Doubling a number: number = number * 2; Many others You can shorten these with combined assignment operators: sum += newNumber; count += 1; number *= 2; You can do this with any of the mathematical operators. There is also an operator to add one to (increment) a variable : ++ Example: sum++;

Sample Segment of Code number = 5; number += 2; System.out.println("number += 2 makes number = " + number); number -= 3; System.out.println("number -= 3 makes number = " + number); number *= 4; System.out.println("number *= 4 makes number = " + number); number /= 8; System.out.println("number /= 8 makes number = " + number); number++; System.out.println("number++ makes number = " + number); number--; System.out.println("number-- makes number = " + number); } }

Conversion Between Primitive Data Types Java is known as a strongly typed language. In a Strongly Typed Language before a value is assigned to a variable, Java checks the types of the variable and the value being assigned to it to determine if they are compatible. For example: int x; double y = 2.5; x = y; This will cause an error short y; This will NOT cause an error …but, why?

Conversion Between Primitive Data Types Types in Java have “ranks”. Ranks here means that if a type has a higher rank than another, it can hold more numbers, and thus, will not lose any precision. Ranks (Highest to Lowest): double float long int short byte

Conversion Between Primitive Data Types In assignment statements where values of a lower-ranked data types are stored in variables of higher-ranked data types, Java automatically converts the lower-ranked value to the higher- ranked type. This is called a Widening Conversion. double x; int y = 10; x = y; A Narrowing Conversion is a conversion of a value to a lower-ranked type. These can cause a loss of data, so Java does not automatically perform them. Imagine converting from double to int… You can perform narrowing conversions with type casting operators.

Type Cast Operators Type Cast Operators allow you to manually convert from one type to another, even if it is a narrowing conversion. In Java they are unary operators that appear before what you want to convert. They are written as the type you want to convert to in parentheses. Example: x = (int)number; If number is of a numeric data type, it will convert the value in number to the int type and assign that value to x. If number is a floating-point type, the fractional part of the value would be lost converting it to int type and the value assigned to x. This is called truncation. The value in number would not be changed.

Type Casting double number1 = 2.0; int number2; byte number3; number2 = (int)number1; number3 = (byte)number2; System.out.println("Type Casting with NO Side Effects"); System.out.println("---------------------------------"); System.out.print ("number1 = " + number1); System.out.print ("number2 = " + number2); System.out.println("number3 = " + number3 + "\n");

Type Casting with side effects number1 = 400.59; number2 = (int)number1; number3 = (byte)number2; System.out.println("Type Casting with Side Effects"); System.out.println("------------------------------"); System.out.println("number1 = " + number1); System.out.println("number2 = " + number2); System.out.println("number3 = " + number3);

Type Conversion in Arithmetic Operations Recall integer division: int number1 = 10, number2 = 4; double number3 = number1 / number2; number3 will have 2.0 stored in it as a result of the division because both operands of the division are of type int. We can use type casting on one of the operands to make sure the result is a double: double number3 = (double)number1 / number2; number3 will have 2.5 stored in it as a result of the division because one of the operands is of type double. Note that type casting operators can be applied to expressions enclosed in parentheses: double number3 = (double)(number1 / number2); number3 will have 2.0 stored in it as a result of the division because the type casting operator is applied to the result of the integer division, which is 2.

Type Casting Example 2 int number1 = 7, number2 = 4; double number3; number3 = number1 / number2; System.out.println(number1 + " / " + number2 + " = " + number3); number3 = (double)number1 / number2; System.out.print((double)number1 + " / " + number2 + " = "); System.out.println(number3); number3 = (double)(number1 / number2);

Mixed Integer Operations One of the problems in Java is that when you use any integer type in an arithmetic operation, it temporarily converts them to int. This can cause problems: short number1 = 10, number2 = 20, number3; number3 = number1 + number2; The second line will cause an error! Why? Because the result of the addition of number1 and number 2 is of the int type, which is over a higher rank than number3’s type, short. Cannot make the narrowing conversion. The way to fix this is to cast the entire expression to short: number3 = (short)(number1 + number2);

Other Mixed Mathematical Expressions When Java sees that an expression has operands of double, float, or long, it attempts to convert all the operands of lower rank to that type. For Example: double number1 = 2.5, number3; int number2 = 4; number3 = number1 + number2; Before the addition number2 is converted to type double, and the result is a double.