Primitives, References and Wrappers Java has the following types defined as part of the java language; int float double byte char boolean long short These.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Pointers Prasun Dewan Comp 114.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Java Programming Strings Chapter 7.
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.
Constants and Data Types Constants Data Types Reading for this class: L&L,
Java Syntax Part I Comments Identifiers Primitive Data Types Assignment.
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
Inheritance and interfaces A class C1 is derived from class C2, then C1 is called subclass, and C2 is called superclass Superclass-parent, base class Subclass.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Chapter 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
BPJ444: Business Programming using Java Java basics Tim McKenna
1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done has been output The reasons –Java I/O is based on.
Java Data Types. Primitive Data Types Java has 8 primitive data types: – char: used to store a single character eg. G – boolean: used to store true or.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Primitive Variables.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part C Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Java Basics.  To checkout, use: svn co scb07f12/UTORid  Before starting coding always use: svn update.
Some Standard Classes Goals The Object class The String class Wrapper classes The Math class Random Numbers.
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.
A variable is a storage location for some type of value. days 102 taxrate 7.75 int days = 102; double taxrate = 7.75; char grade = ‘A’; boolean done.
Copyright Curt Hill Variables What are they? Why do we need them?
CONTENTS Wrapper Class Enumeration Garbage Collection Import static.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
© A+ Computer Science - A reference variable stores the memory address of an object. Monster fred = new Monster(); Monster sally.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
JAVA Practical Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
Object Oriented Programming Lecture 2: BallWorld.
Sections Basic Data Structures. 1.5 Data Structures The way you view and structure the data that your programs manipulate greatly influences your.
Copyright © Texas Education Agency, Computer Programming Variables and Data Types.
Fundamentals 2.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Crash course in the Java Programming Language
C Basics.
Unit-2 Objects and Classes
Computers & Programming Languages
Comp 401 Metamorphosis: Casting vs. Conversion
C++ Data Types Data Type
Chapter 2: Java Fundamentals
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Recap Week 2 and 3.
Chapter 2: Java Fundamentals
Java Programming Review 1
Java Programming Language
Java Basics Data Types in Java.
© A+ Computer Science - Variables & Data Types © A+ Computer Science - ©A+ Computer Science
Chapter 10 Thinking in Objects Part 2
Chap 2. Identifiers, Keywords, and Types
Subtype Substitution Principle
Week 3 - Friday COMP 1600.
Presentation transcript:

Primitives, References and Wrappers Java has the following types defined as part of the java language; int float double byte char boolean long short These are known as the primitive types, since they are basic storage units built in to java. 1 AP Comp Sci wrapper notesheets

Primitives, References and Wrappers Java simply allocates memory for primitives when they are in scope. Example int input=5, sum; double average; 2 AP Comp Sci wrapper notesheets 5 input 0 sum 0.0 average

Primitives, References and Wrappers References, however, are not part of the java language. They are defined in libraries and start with capital letters JLabel titleLbl; 3 AP Comp Sci wrapper notesheets null JLabel titleLbl = new JLabel(“Hangman”); After the new command, the reference now points to an object JLabel “Hangman” JLabel 8F10

The Integer and Double Wrapper Classes Java has provided wrapper classes for all of the primitive types The AP subset contains two of these java.lang.Integer java.lang.Double These bad boys are both immutable, once given a value, they cannot be changed Creating objects using the wrappers Must set the values when creating the object Double x = new Double(3.14); //you try, create two Integer objects with values Integer i1 = new Integer(1999); Integer i2; i2 = new Integer(2003); //Create your own Double with a value Double myDoub; myDoub = new Double( ); NOTE: to assign a new value, myDoub = new Double(2.7); Methods to use with the wrappers int compareTo(Object other) Write an if statement to compare your two integers if(i1.compareTo(i2) < 0) { SOP(“i1 less than i2”); } 4 AP Comp Sci wrapper notesheets

The Integer and Double Wrapper Classes boolean equals(Object other) equals is the only way to compare the VALUES of two Integer or Double objects == will compare the _____________ memory addresses write a statement to use equals to compare your two Integer objects if(i1.equals(i2)) { SOP(“the two ints are equal”); } 5 AP Comp Sci wrapper notesheets

The Integer and Double Wrapper Classes Methods to use with the wrappers (cont’d) int intValue() Gets the VALUE of the integer from the Integer wrapper int x = i1.intValue() + i2.intValue(); double doubleValue() //You try, create a double primitive and assign into it the product of a Double called d1 and a Double called d2 double product = d1.doubleValue() * d2.doubleValue(); String toString() returns the integer or double in a string 6 AP Comp Sci wrapper notesheets