Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Several forms of converting objects into strings
Several forms of converting objects into strings
This paper will summarize the commonly used conversion methods. Common methods include Object.toString (), (String) the object to be converted, String.valueOf(Object) and so on. The following methods are analyzed one by one. Method 1: Object.toString () method is adopted. See the following example: objectobject = getobject (); system . out . println(object . tostring()); In this usage, because there is already a public method. ToString () in the java.lang.Object class can call this method for any strictly java object. But when using it, it should be noted that the object must not be null, otherwise a NullPointerException exception will be thrown. When using this method, usually derived classes override the toString () method in the object. Method 2: Use the String object method, which is a standard type conversion to convert an object into a value of type String. When using this method, it should be noted that the type must be converted to a string type. So it's best to use instanceof to do type checking to determine whether it can be converted. Otherwise it is easy to throw a CalssCastException exception. In addition, we need to be very careful, because when an object defined as an object type is converted to a string, the syntax check will not report an error, which may lead to a potential error. Be extra careful at this time. For example: Objectobj = New Integer (100); String strVal =(String)obj; At runtime, an error will occur because the conversion from integer type to string type cannot pass. However, Integerobj = new integer (100); String strVal =(String)obj; If it is a format code, a syntax error will be reported. In addition, because null values can be converted to any java class type, (String)null is also legal. Method 3: The basis of adopting string.valueof (object) string.valueof (object) is Object.toString (). But it is different from Object.toString (). In the analysis of method 1, it is mentioned that when using the first method, it is necessary to ensure that it is not empty. However, when using the third method, you don't have to worry about whether the object is null. To illustrate the problem, let's analyze the relevant source code. The source code of String.valueOf(Object) in Jdk is as follows:/* * * Returns the string representation of object parameters. * * @ param objan object. * @ If the parameter is null, a string equal to * "null "is returned; Otherwise, the value of * obj.toString () is returned. * @ see Java . lang . Object . tostring()*/public static String value of(Object obj){ return(obj = = null)? “null”:obj . tostring(); From the source code above, you can clearly see the reason why you don't have to worry about null values. However, this also brings us hidden dangers. We should note that when the object is null, the value of String.valueOf(object) is the string "null", not null! Remember to pay attention during use. Imagine if we used the system. out . println(string . value of(null)); system . out . println(null); The output we see will be exactly the same thing: null, but do they have the same meaning? How to judge whether a string is empty S is a string and how to judge whether it is empty: if (null==s || ". Equal to (s)) {...} Note: null = = s and ". Equals (s) here should not be written as s==nullpoint s.equals(s) because ". Not here, because there is an if(null==s) in front, but habit has nothing to do with using it there. Uncertainty equals method, including many other processing methods. If the problem is handled with certain values, there will be fewer bugs than uncertain processing. The conversion between string type and date type converts a string into a date: string s = "2007-06-2110: 50: 50"; Java . text . simple date format format date = new Java . text . simple date format(" yyyy-MM-DD HH:MM:ss "); Java . util . date date = format date . parse(s); //Convert a date to a string. Valueof (date); How does Java get the system time? Recently, when learning Java, I often encounter some minor problems in my work. Baidu later found a solution to the problem and recorded it. Otherwise, with my brain, I will forget it soon. Ha ha. Want to get the system time, not the date, just the time, but Baidu comes out with a date and time case, and there is no single example of only time. After thinking about it for a while, I found a solution to the problem. Import java.util.date; Import java.text.dateformat; Date now = new Date(); // Date () is the constructor of java.util.Date class dateformat d = dateformat.gettimeinstance (); //getTimeInstance () gets the system time string str = d.format (now) without date; System.out.println ("Today is"+str); //Output comments: 1. Date now = new Date(); The Date () in this sentence is the constructor of the java.util.Date class, not the class in java.sql, so import java.util.date should be added before it; Instead of importing java.sql.date; Because of this low-level error, there is a compilation error here.