Download Kinds of Data Primitive Data Numeric Primitive Data

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CS257 Computer Science II
Kevin Sahr, PhD
Lecture 3:
Kinds of Data
1
Kinds of Data
✴ In Java there are two distinct kinds of data:
1. primitive data: any data value or variable that has as its data
type one of the predefined keyword Java data types
2. objects: data values or variables that are derived from an
object class
1-
2
1-
3
1-
4
Primitive Data
✴ There are eight primitive data types in Java
✴ Four of them represent integers:
•
byte, short, int, long
✴ Two of them represent floating point numbers:
•
float, double
✴ One of them represents characters:
•
char
✴ And one of them represents boolean values:
•
boolean
Numeric Primitive Data
✴ The difference between the various numeric primitive
types is their size, which determines the range of
values they can represent:
Type
Storage
Min Value
Max Value
byte
short
int
long
8 bits
16 bits
32 bits
64 bits
-128
-32,768
-2,147,483,648
< -9 x 1018
127
32,767
2,147,483,647
> 9 x 1018
float
double
32 bits
64 bits
+/- 3.4 x 1038 with 7 significant digits
+/- 1.7 x 10308 with 15 significant digits
Objects
✴ objects consist of behavior and attributes
✴ each object is an instance of a particular class
• the class is a template or blueprint for creating objects
• the class defines the behavior and attributes of instances of
that class
✦ attributes are defined by instance variables
✦ behavior is defined by instance methods
✴ primitive data variables are created by declaring
them, but objects require a two-step creation
process:
• declaration of the object variable
• instantiation of the object using the new operator
1-
5
1-
6
1-
7
1-
8
Application Programming Interface
✴ the capabilities that a class provides are
documented in the class Application Programming
Interface (API)
✴ the API is external documentation for human use
✴ the API includes publicly visible:
•
•
•
•
class/static variables (usually constants only)
class/static methods
constructors
instance methods
✴ for each service method (method with public
visibility) in the class the API contains all the
information you need to invoke that method:
• the method header
• a brief description of what the method does
Special Classes
✴ Java provides some classes with special abilities
that are not available to the classes you create
✴ EX: Java provides the String class with these
abilities:
• abbreviated instantiation syntax (no new operator
required)
• concatenation using the + operator
Wrapper Classes
• Primitive data variables have limited functionality
compared to objects
§ in some OOP languages all variables are objects
• It would be useful if they had more object-like
functionality
• To solve this problem, Java provides wrapper classes
that allow primitive data values to be represented as
objects with additional functionality
Wrapper Classes
✴ The java.lang package contains wrapper
classes that correspond to each primitive type:
Primitive Type
byte
Wrapper Class
Byte
short
Short
int
Integer
long
Long
float
Float
double
Double
char
Character
boolean
Boolean
void
Void
1-
9
Wrapper Classes
✴ The API for each wrapper class lists the services
that that class provides
✴see the link to the Java Standard Class Library
class API’s on the website
✴ A wrapper class object can always be created
from a corresponding primitive data value (and
vice versa)
✴ EX: The following statement creates an Integer
object that represents the integer 40 as an object
Integer age = new Integer(40);
1-
10
Wrapper Classes
• An object of a wrapper class can be used in any
situation where a primitive value will not suffice
• For example, many Java data structures (EX:
ArrayList) can only store objects
§ Primitive data values can not be stored in such
data structures, but wrapper objects can be
1-
11
1-
12
Wrapper Classes
✴ Wrapper classes contain static methods that help
manage the associated type
• EX: the Integer class contains a method to convert an integer
stored in a String to an int value:
int num = Integer.parseInt(str);
✴ Wrapper classes often contain useful constants as well
• EX: the Integer class contains Integer.MIN_VALUE and
Integer.MAX_VALUE which hold the smallest and largest int
values respectively
Autoboxing
✴ To make using wrapper classes more convenient, Java
provides special instantiation syntax for wrapper objects
• like the String class, no new operator is required
✴ Autoboxing is the automatic conversion of a primitive
value to a corresponding wrapper object, EX:
Integer age = 40;
• The assignment creates the appropriate Integer object
✴ The reverse conversion (called unboxing) also occurs
automatically as needed, EX:
int older = age + 5;
• age is converted to an int before the addition is
performed
1-
13
Command Line Programs
• Our first few programs in this class will be command
line programs
§ command line programs are those that use only text input
from the keyboard and produce only text output
• Later we will write programs that use a Graphical User
Interface (GUI)
1-
14
1-
15
1-
16
System I/O Objects
✴ The System class contains two public class/static
objects that can be used for command line I/O:
• The System.out object is of class PrintStream and
represents a command line destination to which we can send
output (the Monitor)
• The System.in object is of class InputStream and
represents a command line source we can use for text input
(the Keyboard)
✴ The System, PrintStream, and InputStream
classes are all in the java.lang package
✴ available to your programs without an import statement
Command Line Output
✴ The PrintStream instance method println can
be used to send text output to an output destination
✴ the println method automatically appends a new
line character (‘\n’) to the output
System.out.println ("Whatever you are, be a good one.");
PrintStream method
object
name
single String argument
The print Method
✴ The PrintStream instance method print can also
be used to send text output to an output destination
✴ The print method is similar to the println method,
except that it does not append a newline character to
the output
• does not advance to the next line
• anything printed after a print statement will appear on the
same line
1-
17
1-
18
1-
19
The Scanner Class
• The Scanner class provides convenient methods for
reading input values of various types from an
InputStream object
• The Scanner class is part of the java.util
package, and therefore must be imported
• The following line creates a Scanner object that reads
from the keyboard:
§
Scanner scan = new Scanner (System.in);
• Once created, the Scanner object can be used to
invoke various input methods, such as:
§
String answer = scan.nextLine();
Input Tokens
✴ Unless specified otherwise, white space is used to
separate the elements (called tokens) of the input
✴ White space includes space characters, tabs, new line
characters
✴ The next method of the Scanner class reads the
next input token and returns it as a String
✴ Methods such as nextInt and nextDouble read the
next token and convert it to a particular type
• if token is the entire line, the ‘\n’ character remains (see
ScanProblem.java and ScanProblem2.java)
• Invalid input will throw an exception
References
✴ Note that a primitive variable contains the value itself,
but an object variable contains the address of the
object
✴ An object reference can be thought of as a pointer to
the location of the object
✴ Rather than dealing with arbitrary addresses, we often
depict a reference graphically
num1
name1
38
"Steve Jobs"
1-
20
Assignment Revisited
✴ The act of assignment takes a copy of a value and
stores it in a variable
✴ For primitive types:
Before:
num1
38
num2
96
num2 = num1;
After:
num1
38
num2
38
1-
21
Reference Assignment
✴ For object references, assignment copies the address:
Before:
name1
"Steve Jobs"
name2
"Steve Wozniak"
name2 = name1;
After:
name1
"Steve Jobs"
name2
1-
22
1-
23
1-
24
Aliases
✴ Two or more references that refer to the same object
are called aliases of each other
✴ That creates an interesting situation: one object can
be accessed using multiple reference variables
✴ Aliases can be useful, but should be managed
carefully
✴ Changing an object through one reference changes it
for all of its aliases, because there is really only one
object
Garbage Collection
✴ When an object no longer has any valid references to
it, it can no longer be accessed by the program
✴ The object is useless, and therefore is called garbage
✴ Java performs automatic garbage collection
periodically, returning an object's memory to the
system for future use
✴ In some other languages, the programmer is
responsible for performing garbage collection
Lecture 3 Vocabulary
primitive data
Application Programming Interface (API)
service method
wrapper classes
autoboxing
unboxing
command line programs
references
aliases
garbage collection
1-
25