In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Overview
Anything enclosed in double quotation marks is called a string.
The String class is in the package java.lang.String. As long as it is a package in java.lang, you do not need import to come in when writing the program.
Once the content of the string is created, it can no longer be changed and is a constant
It is precisely because strings are immutable that strings can be shared. Think that if you can modify what you share and use it once, who wants to share it? it's disgusting to think about it, and everyone can use it, which means you can spoil it and dismantle it when you use it.
The string effect is equivalent to the char [] character array, but the underlying principle is the byte [] array.
The creation of String class (in 31ways)
2.1 three construction methods
Public String (); create a blank string that contains nothing
Public String (char [] array); create the corresponding string based on the contents of the string array
Public String (byte [] array); create the corresponding string based on the contents of the byte array
Three construction methods are demonstrated in code.
Public class ConstructString {
Public static void main (String [] args) {
/ / the first construction method: public String (); create a blank string without any content
String str1 = new String ()
System.out.println ("first string:" + str1); / / first string:
/ / the second construction method: public String (char [] array); according to the contents of the string array, create the corresponding string char [] charArray = {'Aids,' strings,'C'}; String str2 = new String (charArray); System.out.println ("second string:" + str2); / / second string: ABC// third construction method: public String (byte [] array) According to the contents of the byte array, create the corresponding string byte [] byteArray = {97,98,99}; / / Note that the element of the byte array is the character corresponding value String str3 = new String (byteArray) of the ASCII code table; System.out.println ("third string:" + str3); / / third string: abc
}
}
2.2 A direct creation
String str4 = "HelloWorld!"
one
With or without new, as long as it is in double quotes, JVM will determine that it is a string, so you can create it directly.
Constant pool
3.1 A little knowledge about comparison in the string
We need to know that in java, comparing two strings with if (String1 = = String2) compares whether the addresses of the two strings are the same, and if you want to know whether their values are the same, XM returns the www.fx61.com/brokerlist/xm.html using the equals method of String, such as:
Public static void main (String [] args) {
String str1 = new String ("hello")
String str2 = new String ("hello")
/ / compare whether the address of str1 is the same as that of str2 (str1 = = str2); / / false is different / / compare whether the value of str1 and str2 are the same System.out.println (str1.equals (str2)); / / true is the same, both are hello
}
3.2 constant pools in the heap
The comparison result of str1 and str2 is true, which proves that they are the same string, only have two names, both point to the same address in memory, and they are located in the constant pool, so the hello string of the constant pool can be shared and cannot be modified.
Str1 is different from str3, which proves that their addresses are different. The strings created by new are not in the constant pool, but they are also constant.
Public class ConstantPool {
Public static void main (String [] args) {
String str1 = "hello"
String str2 = "hello"
String str3 = new String ("hello")
System.out.println (str1 = = str2); / / trueSystem.out.println (str1 = = str3); / / falseSystem.out.println (str2 = = str3); / / false
}
}
The comparison method of strings
4.1equals method of String
By 3. A little knowledge in the constant pool, we know to compare whether the value of two strings is the same, using the equals method, the return value is a Boolean value, the same is true, the difference is false.
Public boolean equals (Object obj)
Add: any object can be received with Object, and so can String class.
The equals method is symmetrical, that is, the results of str1.equals (str2) and str2.equals (str1) are the same.
Note: if the comparison is a constant and a variable, it is recommended to write the string constant in front of it.
That is, "abc" .equals (str) is not recommended: str.equals ("abc")
Because if the string str is a null, the result of "abc" .equals (str) must be false, but writing str.equals ("abc") like this will cause a null pointer exception NullPointerException, so don't casually make an exception.
4.1equalsIgnoreCase method of String
It differs from the equals method in that it ignores case, Ignore ignores, and Case case
Public static void main (String [] args) {
String str1 = "hello"
String str2 = "HellO"
/ / compare whether the value of str1 is the same as that of str2, strictly case-sensitive System.out.println (str1.equals (str2)); / / false / / compare whether the value of str1 is the same as that of str2, ignoring the case of System.out.println (str1.equalsIgnoreCase (str2)); / / true}
Methods related to acquisition in String
Overview
Public int length (); get the number of characters in the string and get the string length
Public String concat (String str); concatenates the current string and the parameter string into the return value as a new string
Public char charAt (int index); gets a single character at the specified index position. (index starts at 0)
Public int indexOf (String str); find the index position where the parameter string first appears in this string, if there is no-1 value
5.1 length () and concat (String str)
Public static void main (String [] args) {
/ / length () method, which gets the length of the string
Int length = "abcdefghijk" .length ()
System.out.println ("length of string is:" + length)
/ / concat (String str), the concatenated string String str1 = "Hello"; String str2 = "World"; String str3 = str1.concat (str2); / / str1 produces a new string after splicing, but the str1 itself remains unchanged / / prints the stitched string System.out.println (str1); System.out.println (str2); System.out.println (str3)
}
/ output result:
The length of the string is 11
Hello
World
HelloWorld
/
5.2 charAt (int index) and indexOf (String str)
Public static void main (String [] args) {
/ / charAt (int index) to get a single character at the specified index location
Char ch = "hello" .charat (1)
System.out.println ("the character at index 1 is:" + ch)
/ / indexOf (String str) to find the first index position of the parameter string in the original string / / if not, return the-1 value String original = "helloworld!"; int index = original.indexOf ("ll0"); System.out.println ("the first index value is:" + index); / / 2}
6. The difference between Struts1 and Struts2
(1) analyze from the action class:
Struts1 requires that the Action class inherit an abstract base class. A common problem with Struts1 is programming with abstract classes rather than interfaces.
The Struts 2 Action class can implement an Action interface as well as other interfaces, making optional and customized services possible. Struts2 provides an ActionSupport base class to implement common interfaces. The Action interface is not required, and any POJO object identified by execute can be used as an Action object for Struts2.
(2) from the Servlet dependency analysis:
Struts1 Action depends on Servlet API because HttpServletRequest and HttpServletResponse are passed to the execute method when an Action is called.
Struts 2 Action does not depend on the container, allowing Action to be tested separately from the container. Struts2 Action can still access the original request and response if needed. However, other elements reduce or eliminate the need for direct access to HttpServetRequest and HttpServletResponse.
(3) Analysis from action thread mode:
Struts1 Action is singleton and must be thread-safe because there is only one instance of Action to handle all requests. The singleton policy limits what Struts1 Action can do, and you have to be very careful when developing. Action resources must be thread-safe or synchronized.
The Struts2 Action object generates an instance for each request, so there is no thread safety problem. (in fact, the servlet container produces many disposable objects for each request and does not cause performance and garbage collection problems.)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.