Java’s Central Casting

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

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.
Numeric literals and named constants. Numeric literals Numeric literal: Example: A numeric literal is a constant value that appears in a Java program.
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.
1 Java Basics. 2 Compiling A “compiler” is a program that translates from one language to another Typically from easy-to-read to fast-to-run e.g. from.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
CS102 Data Types in Java CS 102 Java’s Central Casting.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
BASIC JAVA. Hello World n // Hello world program public class MyFirstJavaProgram { public static void main(String args[]) { char c = 'H'; String s =
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
P Object type and wrapper classes p Object methods p Generic classes p Interfaces and iterators Generic Programming Data Structures and Other Objects Using.
February 11, 1999CSPP503 Week 6 Bring on the Caffeine CSPP503 Week 6.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
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.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Data type, Type Casting,Constants. DATA TYPES IN JAVA.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
“Great leaders are never satisfied with current levels of performance. They are restlessly driven by possibilities and potential achievements.” – Donna.
Programming in java Lecture-3 (a). Java Data Type TypeDescription byte8 bit signed integer short16 but signed integer int32 bit signed integer long64.
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
1 Primitive Types n Four integer types:  byte  short  int (most common)  long n Two floating-point types:  float  double (most common) n One character.
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 Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
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.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Object Oriented Programming Lecture 2: BallWorld.
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.
April 13, 1998CS102-02Lecture 3-1 Data Types in Java CS Lecture 3-1 Java’s Central Casting.
Copyright © Texas Education Agency, Computer Programming Variables and Data Types.
Intro to ETEC Java.
Chapter 7: Expressions and Assignment Statements
Elementary Programming
Java Primer 1: Types, Classes and Operators
Chapter 7: Expressions and Assignment Statements
Multiple variables can be created in one declaration
Programming Language Concepts (CIS 635)
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2.
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Explicit and Implicit Type Changes
Type Conversion, Constants, and the String Object
Lecture 8.
An Introduction to Java – Part I, language basics
Chapter 2: Basic Elements of Java
Introduction to Primitive Data types
Building Java Programs Chapter 2
Building Java Programs
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
Introduction to Primitives
Introduction to Primitives
Java Programming Language
Java Basics Data Types in Java.
Building Java Programs Chapter 2
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.
Names of variables, functions, classes
Chapter 2: Java Fundamentals cont’d
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Subtype Substitution Principle
Introduction to Primitive Data types
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Presentation transcript:

Java’s Central Casting Data Types in Java Java’s Central Casting CS 102-02 Lecture 1-2 1

Are You Java’s Type? Type: a set of values that are semantically similar Java is a strongly typed language Every variable and every expression has a type that is known at compile time. Strong typing helps detect errors at compile time.

What’s the Role of Types? Types limit the: Values that a variable can hold or that an expression can produce Limit the operations supported on those values Determine the meaning of the operations.

Java Types Primitive types boolean numeric Integral: byte, short, int, long, and char Floating-point: float and double Variables of primitive types hold the actual value

Inside a Primitive Type Actual values for integral types: byte: -128 to 127 short: -32768 to 32767 int: -2147483648 to 2147483647 long: -9223372036854775808 to 9223372036854775807 char: from '\u0000' to '\uffff’ (from 0 to 65535) Why use int instead of long?

Boolean Type boolean type represents a logical quantity with two possible values, indicated by the literals true and false

Building a Boolean from a Number Can’t say (why not?): if (x) System.out.println(“Congratulations, it’s a Boole!”); Convert an integer x (following the C language convention that any nonzero value is true): if (x != 0) System.out.println(“Congratulations, it’s a Boole!”);

Building a Boolean from an Object Object reference obj can be converted (any reference other than null is true): obj! = null

The Other Kind of Type Reference types Variables of reference types don’t hold values, but references to values Classes, interfaces and arrays are all reference types

A Graphical View 0010010 1110010 The data of the midway object int counter Airport midway 1110010 The data of the midway object

Array Types If T is a primitive type, then a variable of type "array of T"can hold: Null reference Reference to any array of type "array of T" If T is a reference type, then a variable of type "array of T" can hold: Reference to any array of type "array of S" such that type S is assignable to type T

Object is the Root of All Java Variable of type Object can hold: Null reference Reference to any object, whether class instance or array.

Class  Type Variables have types Objects (and arrays) don’t have a type, but belong to a class Usually we’ll consider them the same

Casting Against Type A value could be two different types Is 12 an int or a float? Compiler isn’t smart, so it’s conservative (signals an error) Override the compiler with a cast Cast says: Treat this variable as the type I say To cast in Java, write: (newType) variable

Examples of casting average = (double) total / counter;

Can’t Always Cast Can’t do this: if ((boolean) x) System.out.println(“Congratulations, it’s a Boole!”); Sometimes casts are automatic, and are called conversions

One of Two Ways Create an expression in a context where the type of the expression is not appropriate and either: Error at compile time (if statement has any type other than boolean) May be able to accept a type that is related to the type of the expression

Automatic Conversion For convenience, Java performs an implicit conversion From the type of the expression to a type acceptable for its surrounding context Kinds of conversions: Identity, Widening primitive, Narrowing primitive, Widening reference, Narrowing reference, String

Funky Conversions What does this print? class Test { public static void main(String[] args) { int big = 1234567890; float approx = big; System.out.println(big -(int)approx); }

Coming Attractions Arrays: Grouping related values together