You are on page 1of 16

Chapter 2: IMMUTABLE

OBJECTS

PROGRAMMING 2
COMP 112
Introduction
2

— Java is an object-oriented language, which means that


it uses objects to:
1. represent data.
2. provide methods related to them.

— An object is a collection of data that provides a set of


methods. For example (Scanner).
— Strings are objects. They contain characters and
provide methods for manipulating character data.

Programming 2 COMP 112 Spring 20-21


Primitives vs Objects
3

— int, double, char, and boolean are primitive types.


— When you declare a variable with a primitive type, Java
reserves a small amount of memory to store its value.

int number = -2;


char symbol = '!';
char[ ] array = {'c', 'a', 't'};

Programming 2 COMP 112 Spring 20-21


Primitives vs Objects
4

— Objects are usually created with the new keyword,


which allocates memory for them.
— For convenience, you don't have to use new to create
strings:
//create a string object
¡ String word = new String("dog");
//implicitly creates a string object
¡ String word = "dog";

Programming 2 COMP 112 Spring 20-21


Primitives vs Objects
5

— The Null keyword:


— Often when you declare an object variable, you assign
it to reference an object. But sometimes you want to
declare variable that doesn't refer to an object.
— In Java, the keyword null is a special value that means
"no object". You can initialize object and array
variables this way:
¡ String name = null;
¡ int[] combo = null;

Programming 2 COMP 112 Spring 20-21


Strings are Immutable
6

— In java, string objects are immutable. Immutable


simply means unmodifiable or unchangeable.
— Once string object is created its data or state can't be
changed but a new string object is created.
— Example to illustrate how String is immutable:
¡ We consider the following method:
÷toUpperCase()

¢ used to convert the string letters into upper case.

Programming 2 COMP 112 Spring 20-21


Strings are Immutable (Cont’d)
7

String name = "java programming";


String upperName = name.toUpperCase();
System.out.println(upperName);
System.out.println(name);
— The output will be
JAVA PROGRAMMING
java programming
— After these statements run, upperName refers to the string
“JAVA PROGRAMMING“ but name still refers to “java
programming”.

— The original name=“java Programming” is not changed because


the string is immutable or unchangeable and a creation of new
string object with uppercase letters occurs.
Programming 2 COMP 112 Spring 20-21
Wrapper Classes
8

— Primitive types like int, double, and char cannot be


null, and they do not provide methods. For example,
you can't invoke equals on an int:
int i = 5;
System.out.println(i.equals(5)); //compiler error

— But for each primitive type, there is a corresponding


wrapper class in the Java library. The wrapper class for
int is named Integer, with a capital I.
Integer i = Integer.valueOf(5);
System.out.println(i.equals(5)); //displays true

Programming 2 COMP 112 Spring 20-21


Wrapper Classes
9

— Primitive Data types and their Corresponding Wrapper


class

Primitive Data Type Wrapper Class


char Character
byte Byte
short Short
long Long
Long
int Integer
float Float
double Double
boolean Boolean

Programming 2 COMP 112 Spring 20-21


BigInteger Arithmetic
10

— BigInteger is a Java class that can represent arbitrarily


large integers. There is no upper bound except the
limitations of memory size and processing speed.
— BigInteger class is used for mathematical operation
which involves very big integer calculations that are
outside the limit of all available primitive data types.
— For example, factorial of 100 contains 158 digits in it
so we can’t store it in any primitive data type available.
— To use BigInteger, you must import
java.math.BigInteger
Programming 2 COMP 112 Spring 20-21
BigInteger Arithmetic
11

— The following code converts a long to a BigInteger:


long x = 17;
BigInteger big = BigInteger.valueOf(x);

— BigIntegers from strings can be also created. For


example, a 20-digit integer that is too big to store
using a long.
String s = "12345678901234567890";
BigInteger bigger = new BigInteger(s);

Programming 2 COMP 112 Spring 20-21


Incremental Design
12

— Design Process:
That allows the programmer to divide a program into
methods while coding. The process is called
"encapsulation and generalization". The essential steps
are:
1. Write a few lines of code in main or another method
and test them.
2. When they are working, wrap them in a new method
and test again.
3. If it's appropriate, replace literal values with variables
and parameters.

Programming 2 COMP 112 Spring 20-21


Incremental Design
13

— To apply the incremental process, a method to display


multiplication table will be designed as following:
1: Loop that displays the multiples of 2:
for (int i = 1; i <= 6; i++) {
System.out.printf("%4d", 2 * i);
}
System.out.println();
2: Encapsulation which wrapping the code in a method:
public static void printRow() {
for (int i = 1; i <= 6; i++) {
System.out.printf("%4d", 2 * i);
}
System.out.println();
}
Programming 2 COMP 112 Spring 20-21
Incremental Design
14

3: Generalize the method to print multiples of other


numbers by re-placing the constant value 2 with a
parameter n. This step is called generalization,
because it makes the method more general.
public static void printRow(int n) {
for (int i = 1; i <= 6; i++) {
//generalized n
System.out.printf("%4d", n * i);
}
System.out.println();
}
Programming 2 COMP 112 Spring 20-21
Important Definitions
15

— Object-Oriented: A way of organizing code and data


into objects, rather than independent methods.
— Object: A collection of related data that comes with a
set of methods that operate on the data.
— Primitive: A data type that stores a single value and
provides no methods.
— Immutable: An object that, once created, cannot be
modified. Strings are immutable by design.
— Wrapper class: Classes in java.lang that provide
constants and methods for working with primitive
types.
Programming 2 COMP 112 Spring 20-21
Important definitions
16

— Design process:
A process for determining what methods a class or
program should have.
— Encapsulate:
To wrap data inside of an object, or to wrap statements
inside of a method.
— Generalize:
To replace something unnecessarily specific (like a
constant value) with something appropriately general
(like a variable or parameter).

Programming 2 COMP 112 Spring 20-21

You might also like