CS102 Data Types in Java CS 102 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

CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
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.
C#: Data Types Based on slides by Joe Hummel. 2 UCN Technology: Computer Science Content: “.NET is designed around the CTS, or Common Type System.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
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.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
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 Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent.
CS 363 Comparative Programming Languages Expressions and Assignment Statements.
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.
Chapter 3: Data Types and Operators JavaScript - Introductory.
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.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
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.
CPS120: Introduction to Computer Science
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.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
JAVA 0. HAFTA Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
1 Programming Java Java Basics. 2 Java Program Java Application Program Application Program written in general programming language Applet Program running.
Arithmetic Expressions
Generic Programming  Object Type  Autoboxing  Bag of Objects  JCL Collections  Nodes of Objects  Iterators.
August 6, 2009 Data Types, Variables, and Arrays.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
Types in programming languages1 What are types, and why do we need them?
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.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CPS120: Introduction to Computer Science Variables and Constants.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
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 Primitive Types n Four integer types:  byte  short  int (most common)  long n Two floating-point types:  float  double (most common) n One character.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
1 Identifiers: Names of variables, functions, classes (all user defined objects), Examples: a b gcd GCD A COSC1373 TAX Tax_Rate Tax Rate if else while.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
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.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
April 13, 1998CS102-02Lecture 3-1 Data Types in Java CS Lecture 3-1 Java’s Central Casting.
Java Primer 1: Types, Classes and Operators
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.
Explicit and Implicit Type Changes
Type Conversion, Constants, and the String Object
null, true, and false are also reserved.
Numerical Data Types.
Introduction to Primitive Data types
Chapter 7 Expressions and Assignment Statements.
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.
Names of variables, functions, classes
Agenda Types and identifiers Practice Assignment Keywords in Java
Introduction to Primitive Data types
Presentation transcript:

CS102 Data Types in Java CS 102 Java’s Central Casting

CS102 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.

CS102 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.

CS102 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

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

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

CS102 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!”);

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

CS102 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

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

CS102 Classes, Interfaces and Arrays, oh my! Classes we’ve already seen Interfaces are programming contracts –An interface is a set of constants and methods –In Java, a class implements an interface

CS102 Implementing an Interface An example from the Comparison applet: public class Comparison extends Applet implements ActionListener Comparison promises to do everything an ActionListener does

CS102 Comparison Does What It Has To What does an ActionListener have to do? actionPerformed public abstract void actionPerformed(ActionEvent e)ActionEvent Provides an implementation for the interface: public void actionPerformed(ActionEvent e) { number1 = Integer.parseInt(input1.getText() ); number2 = Integer.parseInt(input2.getText() ); repaint(); }

CS102 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: –Null reference –Reference to any array of type "array of S" such that type S is assignable to type T

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

CS102 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

CS102 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

CS102 Examples of casting // Casting a float literal to a type int. // Without the cast operator, this would be a // compile-time error, because it’s a narrowing // conversion: int i = (int)12.5f; // From class average applet (Figure 2.9) if ( counter != 0 ) { average = (double) total / counter; System.out.println( "Class average is " + average ); } else...

CS102 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

CS102 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

CS102 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

CS102 Coming Attractions Arrays: Grouping related values together