data types in java - binghamton bits size encoding value ... byte 8 -128 to +127 ... •specifies...
TRANSCRIPT
Binghamton
University
CS-140
Fall 2017
Binghamton
University
CS-211
Fall 2015
Example Class: Car
How Cars are Described
• Make
• Model
• Year
• Color
• Owner
• Location
• Mileage
Actions that can be applied to cars
• Create a new car
• Transfer ownership
• Move to a new location
• Repaint
• Delete a car
2
Binghamton
University
CS-140
Fall 2017
What’s in a class?
• Fields – The data used to describe an object in the class
• Methods – The functions used to manipulate an object in the class
3
Binghamton
University
CS-140
Fall 2017
What are Data Types?
• A data type describes how many bits make up the data
• A data type describes how bits should be interpreted
• A data type describes what operations are allowed
4
Type Bits Size Encoding Value
int 0000 0000 0000 0000 0000 0000 0000 0110
32
𝑖=0
30
𝑏𝑖 × 2𝑖6
char 0000 0000 0100 0111 16 UTF-16 ‘G’
float 0100 0000 0100 1000 1111 0101 1100 0011
32 −1𝑆 × 1. 𝑠𝑖𝑔 × 2𝑒𝑥𝑝 3.14
Chap 4
Binghamton
University
CS-140
Fall 2017
Java Primitive Data Types
Flavor Type Bits Range Precision
logic boolean ? (8) true or false
exactInteger
byte 8 -128 to +127
short 16 -32,768 to +32,767
char 16 0 to 65,535
int 32 ~-2.14x109 to ~2.14x109
long 64 ~-9.22x1018 to ~9.22x1018
floating pointfloat 32 ~-1038 to ~+1038 ~7 digits
double 64 ~-10308 to +10308 ~15 digits
5
Sect. 4.1.1
Binghamton
University
CS-140
Fall 2017
Declaring Local Variables
• Syntax (within a method)
type name;
or
type name=constant;
• Examples
int j;
int count=0;
6
count is an instance of an integer whose initial value is zero
Binghamton
University
CS-140
Fall 2017
Declaring Variables
• Variables must be declared and initialized before they are used• Java is an imperative language, and this is typical
• Enables compiler to check to make sure variables are used correctly
• Declaration and initialization can be combined
• Declaration • specifies “type” – how this variable will be used and what values are valid
• specifies “name” – how you will refer to this value from now on
• Initialization• specifies the first value that the variable will have.
7
Sect. 2.2
Binghamton
University
CS-140
Fall 2017
Examples of primitive variable declaration
int value = 1; // declaration and initialization
int input; // declaration
input = 5; // initial value or a change of value
char letter = 'g';
char aKoreanChar = '\ud55c'; // 한
8
Binghamton
University
CS-140
Fall 2017
More examples:
long large = 1234567890123456789L;
boolean choice = false;
double precipAug = 3.59;
float precipJuly = 2.69F;
byte tiny = (byte)110; // values from -128 to 127
short small = (short) 25000; // from -32768 to 32767
9
Binghamton
University
CS-140
Fall 2017
Declaring Fields (or Instance Variables)
• For each data that is used to define an object• We need to define the name of the field• We need to define the type of the field
• Field Declaration Syntax:
class classname {
type fieldname;
type fieldname;
…
}
10
Binghamton
University
CS-140
Fall 2017
Simple example with Primitive Fields
class Account {
long id;
int dollars;
byte cents;
…
}
• Fields used to define what we want to model about an account
11
Account
id 7789435
dollars 12417
cents 35
Schematic Account Object
Fields
Class name
Values
Binghamton
University
CS-140
Fall 2017
longitude is a reference to an Angle object
Field Declarations with References
class Angle {
byte degrees;
byte minutes;
double seconds;
…
}
class Position {
Angle latitude;
Angle longitude;
…
}
12
seconds is an instance of a double value
Binghamton
University
CS-140
Fall 2017
Field Declarations with References
class Angle {
byte degrees;
byte minutes;
double seconds;
…
}
class Position {
Angle latitude;
Angle longitude; …
}
13
Position
latitude
longitude
Angle
degrees 42
minutes 5
seconds 14.78Angle
degrees 75
minutes 58
seconds 5.35
Binghamton
University
CS-140
Fall 2017
Classes as Types
• Primitive types• pre-defined types known to Java
• only 8 : boolean, byte, short, int, long, char, float, double
• Java allows types which are references to objects• “Type” is the class name
• Result is a reference variable… like a pointer in C, but with extra rules
• Reference variables can be thought of as a “handle” on the encapsulated object
14
Binghamton
University
CS-140
Fall 2017
Binghamton
University
CS-211
Fall 2015
Comparing Types and Classes as Types
Data Types
• A data type describes how many bits make up the data
• A data type describes how bits should be interpreted
• A data type describes what operations are allowed
Classes as Types
• A class describes how much data make up the object
• A class describes how to interpret its fields
• A class defines what methods may be applied to an object
15
Binghamton
University
CS-140
Fall 2017
Example Field Declarations
class Car {String make;String model;short year;Color color;FullName owner;Position location;double mileage;…
}
16
• How Cars are Described• Make
• Model
• Year
• Color
• Owner
• Location
• Mileage
Binghamton
University
CS-140
Fall 2017
Local Reference Variable Examples
• We can declare reference variables:Counter myCounter;
Account myAcc;
String str;
java.io.PrintWriter output;
java.awt.Rectangle rect;
• Reference Variables need to be instantiated before they are used• Instantiation defines which object the reference variable refers to
17
Chap. 3
Binghamton
University
CS-140
Fall 2017
Instantiating Reference Variables
• Need to call class constructor to create a new object
myCounter = new Counter();
myAcc = new Account(150.0);
String str= "My String";
//same as: String str = new String("My String");
output = new java.io.PrintWriter(
new java.io.FileOutputStream("out.txt"));
java.awt.Rectangle rect = newjava.awt.Rectangle(10,30,25,60);
18
Binghamton
University
CS-140
Fall 2017
Combined Declaration & Instantiation
• Most typically, java code declares a reference variable, and instantiates that reference variable with a new object in the same statement
Counter myCounter = new Counter();
• Notice the required redundancy required• First “Counter” is the type of variable “myCounter”
• Second “Counter” is the invocation of the Counter class creation method
19
Binghamton
University
CS-140
Fall 2017
Reference Variables Schematically
Account myAcc = new Account(150.0);
20
Account
id 7789436
dollars 150
cents 0myAcc
Binghamton
University
CS-140
Fall 2017
Reference Variables
• Handles on the encapsulated object
• The handle only fits objects in a specific class
• We need the handle to be able to pass the object around and/or use that object
• The handle is either attached to a specific object or not attached to any object (null)
• The handle is attached to an object with an assignment statement
• NO ARITHMETIC ON HANDLES!!!!!
21
Binghamton
University
CS-140
Fall 2017
Instantiating Reference Variables
output = new java.io.PrintWriter(
new java.io.FileOutputStream("out.txt"));
java.awt.Rectangle rect = new java.awt.Rectangle(10,30,25,60);
• To reference the PrintWriter method in the io sub-package of the java package, we need to specify java.io.PrintWriter
• Writing package names every time is a nuisance
22
Binghamton
University
CS-140
Fall 2017
Importing Packages
• importing a package allows you to use components without package names
import java.io.*;
import java.awt.Rectangle;
output = new PrintWriter( new FileOutputStream("out.txt"));
Rectangle rect = new Rectangle(10,30,25,60);
23
Binghamton
University
CS-140
Fall 2017
Class (Static) Variables
• If you use the “static” keyword, that changes a field into a “class” variable instead of a normal field
• “Class” variables are shared by all objects• Created when the class is loaded – the start of the program
• Deleted when the class is dismissed – the end of the program
• May be initialized when declared
• Often used to keep track of things which a higher level class could take care of
24