Download presentation
Presentation is loading. Please wait.
Published byDiamond Frickey Modified over 10 years ago
1
STRING AN EXAMPLE OF REFERENCE DATA TYPE
2
2 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: !, &&, ||
3
3 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.
4
4 Memory Allocation Declaration of a primitive data type prepares memory to store a value of that type. int num1; num1 0x0010 memory address memory allocated to store an integer (4 bytes) variable name int num1 = 12; When a value is assigned to the variable num1, the value is stored at that memory location. 12
5
5 Memory Allocation Declaration of a reference data type prepares memory to store a memory address. String s1; s1 0x0010 memory address memory allocated to store an memory address variable name The new operator allocates memory to store the actual object. s1 = new String("Hello"); 0x4h12 "Hello" The memory address of the object will be stored in the space allocated for s1. 0x4h12 memory address
6
6 Comparing Strings A variable that is declared for a reference data type only refers to a memory location for an object. String s1 = new String("hi"); s1s2 0x4h120x3121 "hi" 0x3121 0x4h12 returns false because it is comparing the memory addresses 0x4h12 and 0x3121 String s3 = s2; s3 0x3121 returns true because it is comparing the memory addresses 0x3121 and 0x3121 (both s2 and s3 refer to the same object) String s2 = new String("hi"); System.out.println(s1 == s2); System.out.println(s2 == s3) System.out.println(s1.equals(s2) returns true because the String method equals is used to compare Strings.
7
7 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"
8
8 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");
9
9 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.
10
10 String Methods Assume that we have declared the String object: String subjectCode = new String(“JSI1026"); public int length() returns the length of the String: 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
11
11 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 "); whiteString.trim() returns the String "Lots of WhiteSpace"
12
12 public char charAt(int Position) returns the character at Position. whiteString.charAt(6) returns 's' 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 LotsofWhiteSpace 0123456789 1010101011 12121212 13131313 14141414 15151515 16161616 17171717 18181818 19191919 20202020 21212121 22222222 23232323 positions of characters whiteString
13
13 (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
14
14 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) String s1 = new String("Hello");// String objects s1 String s2 = new String("hello");// and s2 created System.out.println("Two strings equal " + s1.equals(s2)); System.out.print("Ignoring case: "+ s1.equalsIgnoreCase(s2)); the method is invoked by the String object s1.
15
15 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. Write a program that displays the String "Happy New Year", one character per line. HappyNewYearHappyNewYear see NewYearGreeting.java
16
16 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.