Object Oriented Programming (OOP) LAB # 8 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal
Working with Strings
Strings The string class is one of the most commonly used complex types in Java. When you create a string, you're creating an instance of a class. It's not a primitive variable, such as char, int, short and the others.
Declaring Strings in java We can declare String in java in two ways. First, by assigning the a literal string directly to the equals assignment operator. For example, String S1=“Welcome to java”; Notice that Because the string class is a complex class, you are actually creating an instance of the class in the memory. Second, by calling the constructor method for the string class. A constructor method is a method of a class that has the same name as the class itself, doesn’t return any value and should be public. So, we are going to pass in exactly the same value of the string to the constructor method. For example, String s2= new String (“Welcome to java”);
Comparing between two Strings If you want to compare two string values, you shouldn't use the double equals operator. It's unpredictable. If you declare one string using the shorthand syntax with the equals assignment operator and just a literal string, and the second string using the constructor method, then you can't compare the values using double equals. There are two methods in the class String in which you can compare the two Strings. First, The equals method which is case sensitive. Second, equalsIgnoreCase in which you can do a non-case sensitive comparison.
Comparing between two Strings Declaring two strings within the same way. Comparing between these strings using double equals operator. The result is true since the two strings were declared within the same syntax.
Comparing between two Strings Declaring two strings with different ways. Comparing between these strings using double equals operator. The result is False. Why?
Comparing between two Strings Using equals method to compare between the two Strings. Notice that s1 and s2 are objects of the String class. The result Now is correct!!
Comparing between two Strings Using equalIgnoreCase method. So, you can do a non-case sensitive comparison. The result is right if we ignore the cases.
Building strings with StringBuilder Let’s begin with an example. If s1=“Welcome to java” and you want to change the value by adding something to it. Let’s say that you want the text above to be altered to “Welcome to java programming”. The solution might seem easy to you from the first glance. Definitely, your answer will be as the following: s1=s1+ “ programming”; So, everything will work fine with you but certainly everything is not alright from the background. What's really happening is that when you append a value or otherwise change the value of the string variable, you're actually creating a new instance of the string class, and you're abandoning the reference to the old object. That old object is still out there in memory. That means you use more memory than what you actually need.
Building strings with StringBuilder Java provides two utility classes to solve this problem. They are called StringBuffer and StringBuilder, these two classes have the same API or programming interface, and they both implement the same methods named insert and append. The insert method can be used to insert text into a string at either the beginning or any other position in the string. The append method can be used to append text to a string. You can use either the StringBuilder or the StringBuffer in most environments, but in general, the StringBuilder is more slender, it takes less memory and resources.
Example#1 of using StringBuilder Using append method to append the new text at the end of the existing string (s1). Print the object sb which contains the two strings. Declare an object from StringBuilder class. As usual every thing is an object in java!
Example#2 of using StringBuilder Insert method takes two arguments. The first one is to indicate the position of the new string. Notice “7” here means insert the new text after 7 characters. That means after “welcome” word. The second argument is the text you want to add. The result of the program.
Parsing Strings value The string class has many methods that you can use to manipulate and parse its values An example in the next slide shows you some of the most common methods used.
Parsing The String Value Compute the length of the String s1. Notice that the spaces are taken into the consideration. Parsing The String Value If you want to know which text is located in specific index, use substring method. If you want to know the location or the position of a word in a string, use indexOf method and pass the value you want to know its index. trim method is used to delete the spaces located at the end of the string.
More on Strings We encourage you to look at the documentation for the string class. You'll find in list of methods and many tools that you can use to extract, manipulate, convert and otherwise use the string class you need to in your applications. You can access the documentation by highlighting the word you are looking for, then from the help menu choose dynamic help and you will find something related to the topic you want to find. See the next slide.
Looking for String class documentation
Inheritance One of the most important term you have to know about object oriented programming is called inheritance. Inheritance means that there's a relationship between classes in your application. An inheritance relationship lets you inherit or extend functionality from one class to another. In the world of C++ there is multiple inheritance. In which a single class can inherit functionality from multiple other classes but in Java, there is only single inheritance, each new class can extend or inherit functionality from only one other class.
Describing the Inheritance There are a number of ways to describe an inheritance relationship and they are just different vocabularies. some developers describe the relationship as a parent-child relationship, where the parent has the functionality and the child is inheriting. Another developers describe it as base and derived, where a base class has the functionality and the derived class is extending it. In the world of Java, the term that is most commonly used is Superclass and Subclass. The Superclass has the functionality and the Subclass is extending the Superclass.
Example of Inheritance Lets suppose that we have two classes. The first one called Shape and the other one called Square. We can describe the relationship between Shape and Square class as the following: “Square is a Shape”. So, the supper class or the parent is Shape class and the subclass is Square class. That means square inherits all methods and attributes from Shape class. To do that, Look at the next slides.
Attributes of the class Attributes of the class. Notice that some attributes defined as static Why? Super class “Shape” Defined two methods. One of them used to return the Area value and the other one is used to return perimeter value.
Use extends keyword which refers to an inheritance Use extends keyword which refers to an inheritance. That means Square inherits Shape. Where did we use this keyword??? sub class “Square” Assigning values to some of the variables. Notice that these variables are inherited from the super class. We didn’t define data types for them. Also, because they are static we didn’t need to call them through an object. The color attribute here inherited from the super class. How? Define an object from the sub class Square and call the methods using this object. Why? Notice that the methods are also inherited from the super class. They aren’t in the sub Class Square.
Using @Override If you want to use a method in the Super class but with different statements, you can use @ Override before the method and change the statements. You can’t change the arguments or what the function return. An example of that in the next slide.
We change some statements of getArea method that exists in the super class. Using @Override
The End !!