Sie sind auf Seite 1von 16

STRING

AN EXAMPLE OF REFERENCE DATA TYPE

Primitive Data Types


The eight Java primitive data types are:
byte short int long float double char boolean

We can use the following operators with primitive data


types:
arithmetic operators: *, /, %, +, relational operators: >, >=, <, <=, ==, != logical operators: !, &&, ||
2

Primitive vs Reference Data Types


A Primitive type is one that holds a simple,
indecomposable value, such as:
a single number a single character

A Reference type is a type for a class:


it can hold objects that have data and methods

Reference data types are stored in memory


differently from primitive data types.

Memory Allocation
Declaration of a primitive data type prepares
memory to store a value of that type.
int num1;
variable name memory allocated to store an integer (4 bytes)

num1

12
0x0010

memory address

int num1 = 12;

When a value is assigned to the variable num1, the value is stored at that memory location.
4

Memory Allocation
Declaration of a reference data type
prepares memory to store a memory address.
String s1;
variable name

s1

0x4h12 0x0010 memory address

memory allocated to store an memory address

The new operator allocates memory to store the actual object.


s1 = new String("Hello");

"Hello"
0x4h12

The memory address of the object will be stored in the space allocated for s1. memory address
5

Comparing Strings
A variable that is declared for a reference
data type only refers to a memory location for an object. "hi" s1 0x4h12 String s1 = new String("hi");
String s2 = new String("hi"); System.out.println(s1 == s2);
returns false because it is comparing the memory addresses 0x4h12 and 0x3121 0x4h12

s2 s3

0x3121

"hi"
0x3121

0x3121

String s3 = s2; System.out.println(s2 == s3) System.out.println(s1.equals(s2)

returns true because it is comparing the memory addresses 0x3121 and 0x3121 (both s2 and s3 refer to the same object) returns true because the String method equals is used to compare Strings. 6

The String Data Type


A String type is an example of a reference data
type. A string is defined as a sequence of characters. Examples of String literals:
" " (space, not the character ' ') "" (empty String) "a" "HELLO" "This is a String" "\tThis is also a String\n"

Declaring a String
Strings can be used to store names, titles, etc. We can declare a String data type by giving it a
variable name:
String name;

We can also initialize the variable upon


declaration:
String subjectCode = JSI1026";

Because String is a class type, the correct way


to declare it is to use the new operator:
String subjectCode = new String(JSI1026");
8

String Objects
When you declare a String using:
String subjectCode = new String(JSI1026");

The String subjectCode refers to a String object. String objects have


data : they hold a sequence of characters methods : the data can be manipulated in a certain way.

We will look at some of the more common


methods associated with Strings.
9

String Methods
Assume that we have declared the String object: public int length()
returns the length of the String:

String subjectCode = new String(JSI1026");

subjectCode.length() returns the value 6 public boolean equals(AnotherString)


checks if the calling string is equal to AnotherString

subjectCode.equals(JSI1029") returns false public boolean equalsIgnoreCase(AnotherString)


checks if it is equal to AnotherString, considering lowercase and uppercase letters to be equal.

subjectCode.equalsIgnoreCase(jsi1026") returns true


10

String Methods
public String toLowerCase()
returns a String that is the calling String converted to lowercase.

subjectCode.toLowerCase() returns the String jsi1026" Similarly for the method toUpperCase() public String trim() removes leading and trailing whitespace:
String whiteString = new String(" Lots of WhiteSpace "); 11 whiteString.trim() returns the String "Lots of WhiteSpace"

whiteString

L o t s
0 1 2 3 4 5 6 7

o f
8 9 1 0

Wh i t e S p a c e
11 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3

public char charAt(int Position)


returns the character at Position. whiteString.charAt(6) returns 's'

positions of characters

public String substring(int Start)


returns a String beginning from Start whiteString.substring(11) returns "WhiteSpace "

public String substring(int Start, int End)


returns a String beginning at Start up until just before End whiteString(3,7) returns "Lots"

public int indexOf(String A_String)


returns the first position of A_String if found, -1 if not found. indexOf("it") returns 13
12

(More) String Methods


public int compareTo(A_String)
compares the string to A_String lexicographically and returns a ve number if the calling string comes first, 0 if they are equal and a +ve number if A_String comes first.

public boolean endsWith(A_String)


returns true if the string ends with A_String, false otherwise.

public boolean startsWith(A_String)


returns true if the string starts with A_String. see example: StringExamples.java
13

Example the String class


Let's consider some of the String methods we
have considered previously:
public boolean equals(String anotherString) public boolean equalsIgnoreCase(String otherString)
the method is invoked by the String object s1. String s1 = new String("Hello"); String s2 = new String("hello"); // String objects s1 // and s2 created

System.out.println("Two strings equal " + s1.equals(s2)); System.out.print("Ignoring case: "+ s1.equalsIgnoreCase(s2));


14

Exercise
Consider the methods:
public int length() Returns the length of this string. public char charAt(int pos) Returns the character at the
specified index.

H a p p y N e w

Write a program that displays the


String "Happy New Year", one character per line.
see NewYearGreeting.java

Y e a r
15

Exercise
Write a program that asks for the user's name
and gender. If the gender is l' or L' then display "Halo Tuan XXX" or "Halo Nona XXX Sample run 1:
Please enter name: Lina Please enter gender: P Halo Nona Lina

Sample run 2:
Please enter name: Joko Please enter gender: L Halo Tuan Joko
16