www.1001TopWords.com |
String in Java
Handling character strings in Java is supported through two final classes: String and StringBuffer. The String class implements immutable character strings, which are read-only once the string has been created and initialized, whereas the StringBuffer class implements dynamic character strings. All string literals in Java programs, are implemented as instances of String class. Strings in Java are 16-bit Unicode. Note : In JDK 1.5+ you can use StringBuilder, which works exactly like StringBuffer, but it is faster and not thread-safe The easiest way of creating a String object is using a string literal: String str1 = "I cant be changed once created!"; A string literal is a reference to a String object. Since a string literal is a reference, it can be manipulated like any other String reference. i.e. it can be used to invoke methods of String class. For example, Int myLength = "Hello world".length(); The Java language provides special support for the string concatenation operator ( + ), which has been overloaded for Strings objects. String concatenation is implemented through the StringBuffer class and its append method. For example, String finalString = "Hello" + "World" Would be executed as String finalString = new StringBuffer().append("Hello").append("World").toString(); The Java compiler optimizes handling of string literals. Only one String object is shared by all string having same character sequence. Such strings are said to be interned, meaning that they share a unique String object. The String class maintains a private pool where such strings are interned. For example, String str1="Hello"; String str2="Hello"; If(str1 == str2) System.out.println("Equal"); Would print Equal when run. Since the String objects are immutable. Any operation performed on one String reference will never have any effect on other references denoting the same object. Constructors String class provides various types of constructors to create String objects. Some of them are, String() Creates a new String object whose content is empty i.e. "". String(String s) Creates a new String object whose content is same as the String object passed as an argument. Note: Constructor creates a new string means it does not intern the String. Interned String object reference can be obtained by using intern() method of the String class String also provides constructors that take byte and char array as argument and returns String object. String equality String class overrides the equals() method of the Object class. It compares the content of the two string object and returns the boolean value accordingly. For example, String str1="Hello"; String str2="Hello"; String str3=new String("Hello") //Using constructor. If(str1 == str2) System.out.println("Equal 1"); Else System.out.println("Not Equal 1"); If(str1 == str3) System.out.println("Equal 2"); Else System.out.println("I am constructed using constructor, hence not interned"); If( str1.equals(str3) ) System.out.println("Equal 3"); Else System.out.println("Not Equal 3"); The output would be, Equal 1 Not Equal 2 Equal 3 Note that == compares the references not the actual contents of the String object; Where as equals method compares actual contents of two String objects. String class also provides another method equalsIgnoreCase() which ignores the case of contents while comparing. Apart from these methods String class also provides compareTo methods. int compareTo(String str2) This method compares two Strings and returns an int value. It returns value 0, if this string is equal to the string argument a value less than 0, if this string is less than the string argument a value greater than 0, if this string is greater than the string argument int compareTo(Object object) This method behaves exactly like the first method if the argument object is actually a String object; otherwise, it throws a ClassCastException. String Manipulations Reading characters from String: char charAt(index i) Returns char at specified index. An index ranges from 0 to length() -1. Searching characters in String String class provides indexOf method which searches for the specified character inside the string object. This method has been overloaded. If the search is successful, then it returns the index of the char otherwise -1 is returned. int indexOf(int c) Returns the index of first occurrence of the argument char. int indexOf(int c, int fromIndex) Finds the index of the first occurrence of the argument character in a string, starting at the index specified in the second argument. int indexOf(String str) Finds the start index of the first occurrence of the substring argument in a String. int indexOf(String str, int fromIndex) Finds the start index of the first occurrence of the substring argument in a String, starting at the index specified in the second argument. The String class also provides methods to search for a character or string in backward direction. These methods are given below. int lastIndexOf(int ch) int lastIndexOf(int ch, int fromIndex) int lastIndexOf(String str) int lastIndexOf(String str, int fromIndex) Replacing characters in String The replace method of String can be used to replace all occurrences of the specified character with given character. String replace(char oldChar, int newchar) Getting substrings String class provides substring method to extract specified portion of the given String. This method has been overloaded. String substring(int startIndex) String substring(int startIndex, int endIndex) Note: A new String object containing the substring is created and returned. The original String won't be affected. If the index value is not valid, a StringIndexOutOfBoundsException is thrown. Conversions String class provides set of static overloaded valueOf method to convert primitives and object into strings. static String valueOf(Object obj) static String valueOf(char[] character) static String valueOf(boolean b) static String valueOf(char c) static String valueOf(int i) static String valueOf(long l) static String valueOf(float f) static String valueOf(double d) Manipulating Character Case String class provides following methods to manipulate character case in String. String toUpperCase() String toUpperCase(Locale locale) String toLowerCase() String toLowerCase(Locale locale) Note : Original String object is returned if none of the characters changed, otherwise new String object is constructed and returned. Miscellaneous methods String trim() This method removes white space from the front and the end of a String. int length() Returns length of the String. String intern() This method returns interned String object, if already present in the String pool. Otherwise this String is added into the pool, and then interned reference is returned. Rahim Vindhani
|
RELATED ARTICLES
Manufacturing Outsourcing: Microsoft Great Plains Implementation, Customization & Reporting Manufacturing in the USA is far away down from mid 20th century top of the hill or its golden time. However we are in very competitive business environment and we have to do what market tends us to do. Nowadays competitive offshore manufacturing labor cost outweighs custom clearance fees and forces us to have not full-size manufacturing, but rather final assembly facilities here in the US distribution centers, such as Chicago, Nashville, Los Angeles. We'd like to give you successful implementation/downsizing/outsourcing scenario of mid-size machinery manufacturer. Who Is Minding Your Sensitive Data? Stealing company information used to be the specialty of spies and conspirators. It was something that only happened to the most powerful of corporations and branches of government. Five Tips For A Great Software Demo Whether you need to close a sale, gather end-user feedback, show progress to your customer, or simply explain how your product works, sooner or later, you will need to demo your software product. Need of Document Management System (DMS) Document Management or Enterprise Information Management is perhaps one of the most important of the enterprise solutions that will provide a solution to the various requirements of SOX. Several sections of SOX have a direct bearing on the manner in which the digital documents/records of the enterprise are created, reviewed, approved, stored, retrieved, transferred, and destroyed. History of Java The java programming language is becoming more and more popular each day. It is the language without which one cannot even hope to a land a job thesedays. But has somebody even wondered how this language came about? There are many stories about, many books have been written. Here is my version (not approved by Sun Microsystems). .Net Charts and Graphs Interact with Businesses and Customers Bar charts, bar graphs, and any other chart or graph used in financial statements, inventory reports, sales reports, and a slew of other types of reports have typically been paper reports or online reports depicting various levels of complex information for tracking, investing, planning, and buying. However, until the .net graph, .net chart, .net map, and other .net charts came along, the relationship between management and the company, companies and their investors, and companies and their customers was missing. What You Should Know About Installing Screensavers Do you remember that frustrating feeling when you find an interesting screensaver and can't install it on your computer? This article will help you to never have it again. Microsoft CRM Integration with Microsoft Retail Management System (RMS) ? Overview Microsoft Client Relation Management system (Microsoft CRM) and Microsoft RMS are both Microsoft SQL Server based applications, however historically Microsoft was purchasing industry leading software applications, such as QuickSell which is now Microsoft RMS. So, RMS design fundamentals were minted a long time before Microsoft CRM. But, still integration between the two is straightforward and what you need is consultant, who is familiar with both systems structures and customization scenarios. Microsoft CRM is currently integrated with Microsoft Great Plains and will be integrated with Microsoft Navision in the close future, RMS integration is not planned to our knowledge, so you should seek third party solutions or create your own integration. We see the need for such the integration when you sell on account in RMS and have customer support call center automated with Microsoft CRM. Let us give you highlights: Software Process Improvement -A Successful Journey Background:For many organizations like ours, the interim target of achieving ISO 9001 or CMM levels is a daunting task. Discipline is no fun -Organizations readily acknowledge that striving to address projects according to ISO 9001 or CMM guidelines requires the creation of new procedures. It is vital to the success of improvement efforts to realize that process change entails cultural change and its human nature to resist change. Numerous social and technical barriers must be overcome to effect lasting improvement. What Stands Behind The Software Description? With thousands of web pages added to the Net every day, it's getting more and more frustrating both to submit the information to the attention of those, who are interested in it and to find the information one needs. String in Java Handling character strings in Java is supported through two final classes: String and StringBuffer. The String class implements immutable character strings, which are read-only once the string has been created and initialized, whereas the StringBuffer class implements dynamic character strings. All string literals in Java programs, are implemented as instances of String class. Strings in Java are 16-bit Unicode. Configure Windows Indexing Service for Performance The Windows Indexing Service provides you with the ability to perform advanced searches on directories located on your computer and on shared directories on the network. The Indexing Service was introduced with IIS (Internet Information Services) and is now installed with Windows 2000 and Windows XP. Snort for Network IDS What is Snort? Selecting Corporate ERP: Microsoft Business Solutions Great Plains ? Estimation Parameters As we could imagine, if you are reading this article ? you are in the decision making mode, probably replacing legacy system with the new one or abandoning either Unix/Mainframe or Apple direction for your corporation. In any case, nobody should make an outside influence on your decision ? you should be maximally objective. Let us give you our estimates and the parameters to check on the pool of the possible ERPs/MRPs. Microsoft Navision Integration with Microsoft RMS - Overview for IT Specialist Microsoft Business Solutions Navision serves both European and American megamarkets. It was originally written by Denmark-based Navision Software in its own proprietary language C/SIDE (Client/Server Integrated Development Environment). Currently Navision is supported on two platforms - Navision native (C/SIDE) database and on Microsoft SQL Server. In this small article we would like to give you the clue on Navision integration with Microsoft Retail Management System - Microsoft RMS. Razzle Dazzle Them Once upon a time not so long ago, there was a little boy who went to the market to try to sell his wares. He wasn't having any luck. "Who will buy my sweet red roses? Two blooms for a penny," he sang out hauntingly. What is Software Piracy? There are several kinds of software piracy. The bottom line is when software is pirated, the developer does not receive compensation for their work. Artificial Intelligence And Intuition The intuitive algorithm. Adware and Spyware Blockers The most important things you can do for your computer right now is to install adware and spyware blockers. Everyone is aware of computer viruses and run an anti-virus program to guard your computer against them. Microsoft Update - 5 Reasons to Update Service Pack 2 Microsoft released Service Pack 2 (SP2) for the Windows XP operating system back in August (2004), representing significant improvements in many areas, especially system security. For those people that are still unsure whether they should update to it, the following five reasons may help make the decision easier. |
© Athifea Distribution LLC - 2013 |