Explicit and Implicit Type Changes

Slides:



Advertisements
Similar presentations
Literals Why does this produce an error? float x = 2.5;
Advertisements

Type Conversion. C provides two methods of changing the type of an expression: Type conversion (done implicitly) Cast expressions (done explicitly) Examples.
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
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.
Expressions ► An expression can be a single variable, or can include a series of variables. If an expression includes multiple variables they are combined.
Constants and Variables  Memory cells used in program  Called constants and variables  Identifiers for constants and variables should be declared before.
Review Question What kind error is it when I try to multiply a number in a program by 1000 and store in a variable, but the variable is too small for the.
CS102 Data Types in Java CS 102 Java’s Central Casting.
Java Intro. Strings “This is a string.” –Enclosed in double quotes “This is “ + “another “ + “string” –+ concatenates strings “This is “ “ string”
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
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.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 3A Integral Data (Concepts)
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.
OOP with Java, David J. Barnes Adding Sequential Behavior1 Adding Behavior Previous class definitions have passively maintained attributes. Active behavior.
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.
Mixing integer and floating point numbers in an arithmetic operation.
Executable Statements 1. Def. Executable statements are instructions to the computer to perform specific tasks. 2. Two examples: method calls and assignment.
Autoboxing A new feature in Java 5.0. Primitive types and classes In Java we have primitive types –boolean, char, byte, short, int, long, float, double.
Introduction to Java Primitive Types Operators Basic input and output.
Programming with Visual C++: Concepts and Projects Chapter 3A: Integral Data (Concepts)
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
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
“Great leaders are never satisfied with current levels of performance. They are restlessly driven by possibilities and potential achievements.” – Donna.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CS 884 (Prasad)Java Types1 Language Specification Semantics of constructs. –Definition and use of name-value bindings: Name Resolution. Soundness : No.
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Object Oriented Programming Lecture 2: BallWorld.
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.
ISBN Chapter 7 Expressions and Assignments Statements.
April 13, 1998CS102-02Lecture 3-1 Data Types in Java CS Lecture 3-1 Java’s Central Casting.
Object-Oriented Programming (OOP) Lecture No. 16
7.2 Arithmetic Expressions
Chapter 4 Assignment Statement
Chapter 7: Expressions and Assignment Statements
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Java Primer 1: Types, Classes and Operators
Chapter 7: Expressions and Assignment Statements
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.
Assignment and Arithmetic expressions
Primitive and Reference Data Values
Chapter 3 Assignment Statement
Object-Oriented Programming (OOP) Lecture No. 16
Fundamental of Java Programming Basics of Java Programming
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
Fundamental Data Types
Expressions and Assignment Statements
Arithmetic Expressions
Conversions of the type of the value of an expression
Unit-2 Objects and Classes
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
Numerical Data Types.
More about Numerical Computation
Doing Arithmetic Today’s lecture is in chapter 2 Assignment statement:
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.
Pointer Operations.
Chapter 3 Operators and Expressions
Fundamental Data Types
Java Programming Language
Java Basics Data Types in Java.
Java’s Central Casting
Type Conversion It is a procedure of converting one data type values into another data type In C programming language we are having two types of type conversion.
B=15 b = a + fun(a); a=10 fun(a)=5.
Operator King Saud University
Review of Java Fundamentals
Presentation transcript:

Explicit and Implicit Type Changes casting = explicitly changing the value Integer i = (Integer)(myVector.ElementAt(0)) implicit casting( a.k.a. conversion ) myVector.addElement( myColor )

Primitives and Conversion int, float, double, etc Object data types classes you create classes in the JDK

Primitive Conversion: Assignment Rules Boolean cannot be converted to another type non-booleans can be converted to other non-booleans only if they are widening conversions char int long float double byte short

Primitive Conversion: Method Call When you pass a value of one type as an argument to a method that expects a different type public void addFloat( float f ) {…} int i = 2; addFloat( i ); Cannot convert from primitive to Object types int i = 10; myVector.addElement(i) // compiler error - int is not an object

Primitive Conversion: Arithmetic Promotion Unary operator rules byte, short, char, are converted to an int any other type is not converted Binary operator rules if one operand is a double the other is converted to a double elseif one operand is a float the other is converted to a float elseif one operand is a long the other operand is converted to a long else both operands are converted to ints

Primitives and Casting int i = 69 double d = (double) i Booleans cannot be cast to other types and vice versa If you are doing a shortening conversion - data will be dropped page 107 - Figure 4.5

Object Reference and Method Call Conversion Rules An interface type may only be converted to an interface type or Object A class type may be converted to a class type or an interface type( old class must implement the interface ) An array can be converted to: class Object Interface Cloneable to an array Figure 4.7 - page 110

Object Reference Casting: Compile Time When casting from OldType to NewType, one must be a subclass of the other When OldType and NewType are arrays they must be arrays of object references and not primitives and you must be able to cast from OldType to NewType You can always cast from an interface to a non-final object

Object Reference Casting: Run-Time If NewType is a class then the class of the expression being converted must be of NewType or inherit from it If NewType is an interface then the class of the expression being converted must implement it Figure 4.11 116