The conversion between string variables and shaping is common in any programming language, and it is not possible to confuse a forced type conversion (int) like string, which is wrong, (string) int, such that the conversion between string variables and shaping cannot be accomplished. Coercion type conversions are only used to convert an int to a floating-point float, and the parent class is converted to a subclass.
String strings in Java are converted into shaped int, can take advantage of integer.parseint (), and shaping int into string Although many people know the famous ToString (), but should not form a stereotype of thinking, In fact, you are in the case of any of the shaping, followed by + "" can be converted directly to a string.
For example, the following methods:
public static void Parse () {String Stringa = "1"; String STRINGB = "2"; int intA = 0, IntB = 0;try {IntA = Integer.parseint (Stringa); IntB = Integer.parseint (STRINGB); System.out.println (IntA + IntB); System.out.println ((IntA + "") + (IntB + "")); catch (Exception e) {System.out.println ("either of these two things is not a number! ");}}
Show the string Stringa, B conversion to shaping, the string into the shape of the time must throw an exception, if the string a, b any one is not a number, you have to fallback all the code in the try, that is, the code inside the try is not written, and then execute all the code inside the catch. If there is no exception, then the code in the catch is equal to nothing. Executes only the code of the try. Now, although I do not see the effect, because I have guaranteed that the string A, B is a number in this program, but in some programs that involve user interaction, this must be done to ensure our robustness. Since Integer.parseint () is not required to be written in an exception structure, it is often forgotten.
What is static method Statics? Generally there is nothing, especially in such a tool class method, what is the tool class? In other words, this class is no longer useless when executed like this, and you can consider writing the method inside the class as such a public static no return value method. Looking at a program, compare static and non-static differences to understand. It is useless to speak more. I can't tell you, you don't understand:
Class ClassA {public static void parse () {String Stringa = "1"; String STRINGB = "2"; int intA = 0, IntB = 0;try {IntA = Integer.parseint (Stringa); IntB = Integer.parseint (STRINGB); System.out.println (IntA + IntB); System.out.println ((IntA + "") + (IntB + "")); catch (Exception e) {System.out.println ("either of these two things is not a number! ");}} public void Notstaticmethod () {System.out.println ("I am not a static method");}} public class Statictest {public static void main (string[] args) {classa.parse (); new ClassA (). Notstaticmethod ();}}
The results of the above program are as follows:
In the main function, to execute the two methods in ClassA, there must be a classa, but for the static method parse () The call is no more than the first ClassA, if the class members are called as simple, for the static method of the parse () run out, the ClassA is dead-it is useless. , you do not have to use this method first, you will no longer waste space in Java memory, high efficiency. If you do not define a static method, first you have to write your own annoying, first new ClassA (), to call this method. Of course, if you want to use new ClassA () in the future, but you will never use any of this classA (), there is no need to define static methods. So when you're programming, consider "tool methods" such as "tool methods" like my previous "Java" two ArrayList (click Open link) and so on, you should define a tool class, which is full of static methods. This is simple to call.
"Java" string variables and the transformation between shaping and static method Statics