Wrapper classes in Java
I believe that all of you are very familiar with the basic data types, such as int, float, double, Boolean, Char, and so on.
Basic data types do not have the properties of the object, such as the basic type cannot invoke the method, the function is simple ... ,
In order for the basic data type to have the properties of the object, Java provides a wrapper class for each of the basic data types, so that we can manipulate the basic data type as if it were an object.
The wrapper class provides two main types of methods:
1. ways to convert this type and other basic types
2. Ways to convert strings and this type and wrapper class to each other
An Integer common method
Public classHelloWorld { Public Static voidMain (string[] args) {//defines an int type variable with a value of intScore1 = 86; //Create an Integer wrapper class object that represents the value of the variable score1Integer score2=NewInteger (Score1); //converting an Integer wrapper class to a double type Doublescore3=Score2.doublevalue (); //converting an Integer wrapper class to a float type floatscore4=Score2.floatvalue (); //converting an Integer wrapper class to an int type intScore5 =Score2.intvalue (); System.out.println ("Integer wrapper class:" +score2); System.out.println ("Double type:" +score3); System.out.println ("Float type:" +Score4); System.out.println ("Int type:" +Score5); }}Conversions between basic types and wrapper classes in Java
The basic types and wrapper classes often need to be converted to each other, in the case of an Integer (the operation of several other packaging classes is similar):
With the introduction of the automatic boxing and unpacking mechanism in JDK1.5, the conversion between the wrapper class and the base type is easier and more convenient.
So what is packing and unpacking? Let's look at each other.
Boxing: converts the basic type into a wrapper class, which has the properties of the object and can be divided into manual boxing and automatic packing.
unpacking: In contrast to boxing, the wrapper class object is converted to a basic type of value, and can be divided into manual unpacking and automatic unpacking
Public classHelloWorld { Public Static voidMain (string[] args) {//defining a double type variable DoubleA = 91.5; //Manual BoxingDouble B =NewDouble (a); //Automatic BoxingDouble C =A; System.out.println ("After boxing the result is:" + B + "and" +c); //defines a double wrapper class object with a value of 8Double d =NewDouble (87.0); //Manual Unpacking DoubleE =D.doublevalue (); //Automatic Unpacking Doublef =D; System.out.println ("The result after unpacking is:" + E + "and" +f); }}
Conversions between basic types and strings in Java
In program development, we often need to convert between the basic data type and the string.
There are three ways to convert the base type to a string :
1. Use the ToString () method of the wrapper class
2. Using the valueOf () method of the String class
3. Using an empty string with the base type, you get the string that corresponds to the base type data
Again, there are two ways to convert a string to a basic type :
1. Call the parsexxx static method of the wrapper class
2. The valueOf () method that calls the wrapper class is converted to the basic type of wrapper class, and the box is automatically disassembled
PS: Other basic types and string conversions are no longer listed here, and the methods are similar
Public classHelloWorld { Public Static voidMain (string[] args) {Doublem = 78.5; //converting a base type to a stringString str1 =double.tostring (m); System.out.println ("M is converted to string and the sum of the integer 20 is:" + (str1+20)); String Str= "180.20"; //Convert a string to a base typeDouble A =double.parsedouble (str); System.out.println ("The sum of the integer 20 after the conversion of STR to type Double is:" + (a+20)); }}
Using the date and SimpleDateFormat classes to represent time
In program development, it is often necessary to deal with date and time related data , when we can use the date class in the java.util package. The main function of this class is to get the current time, let's look at the use of the Date class:
The object created by using the default parameterless construction method of the date class represents the current time , and we can directly output a Date object that displays the current time, showing the following results:
Among them, Wed represents Wednesday (Wednesday), June stands for June (June), 11 for 11th, CST for China Standard Time (Beijing time, the East eight district).
From the above output, we found that the default time format is not very friendly, and we see the date format is not the same, if you want to display in the specified format, such as 2014-06-11 09:22:30, then how to do?
At this time to the java.text package of SimpleDateFormat class to do their time!! You can use simpledateformat to format datetime, such as to convert a date to text in a specified format, or to convert text to a date.
1. Use the format () method to convert a date to a text in the specified format
The code "YYYY-MM-DD HH:mm:ss" is a predefined string, YYYY represents four-bit years, MM for two-bit months, DD for two-bit dates, HH for hours (using 24-hour system), MM for minutes, SS for seconds, so that the target format of the transformation is specified, and the last Call The format () method converts the time to a string of the specified format.
Running result: 2014-06-11 09:55:48
2. Convert text to date using the parse () method
The "yyyy mm DD day HH:mm:ss" in the code specifies the date format of the string, and the parse () method is called to convert the text to a date.
Operation Result:
Must pay attention to OH:
1. A conversion exception may occur when invoking the parse () method of the SimpleDateFormat object , or parseexception, so exception handling is required
2. You need to import the java.util package when using the Date class, you need to import the java.text package when using SimpleDateFormat
Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;Importjava.util.Date; Public classHelloWorld { Public Static voidMain (string[] args)throwsParseException {//use the Format () method to convert a date to text in the specified formatSimpleDateFormat SDF1 =NewSimpleDateFormat ("yyyy mm month dd Day hh mm min ss sec"); SimpleDateFormat SDF2=NewSimpleDateFormat ("Yyyy/mm/dd hh:mm"); SimpleDateFormat sdf3=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); //creates a Date object that represents the current timeDate now =NewDate (); //call the Format () method to convert the date to a string and outputSystem.out.println (Sdf1.format (now)); System.out.println (Sdf2.format (now)); System.out.println (Sdf3.format (now)); //convert text to a date using the parse () methodString d = "2014-6-1 21:05:36"; SimpleDateFormat SDF=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); //Call the Parse () method to convert the string to a dateDate Date =Sdf.parse (d); SYSTEM.OUT.PRINTLN (date); }}Application of Calendar class
The most important role of the date class is to get the current time, while the class also has set time and some other functions, but because of its own design problems, these methods have been criticized many, not recommended, it is recommended to use the Calendar class for time and date processing .
The Java.util.Calendar class is an abstract class that can get a Calendar object by calling the GetInstance () static method , which is initialized by the current DateTime. That is, the default represents the current time, such as Calendar C = calendar.getinstance ();
So how do you use Calendar to get information about the year, month, day, time, and more? Let's look at the following code:
Where you call the GetInstance () method of the Calendar class to get an instance, and then get the datetime information by calling the Get () method, the parameter is the value of the field that needs to be obtained, calendar.year, and so on as the static constant defined in the Calendar class.
Operation Result:
The Calendar class provides the GetTime () method for getting the Date object , completing the conversion of the calendar and date, and the Gettimeinmillis () method to get the time of the calendar The value, in milliseconds. As shown below:
Operation Result:
ImportJava.text.SimpleDateFormat;ImportJava.util.Calendar;Importjava.util.Date; Public classHelloWorld { Public Static voidMain (string[] args) {//Create a Canlendar objectCalendar C =calendar.getinstance (); //convert a Calendar object to a Date objectDate Date =C.gettime (); //Create a SimpleDateFormat object that specifies the destination formatSimpleDateFormat SDF =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); //converts a date to a string of the specified formatString now =Sdf.format (date); System.out.println ("Current time:" +Now ); }}Manipulating data using the Math class
The math class is located in the Java.lang package and contains methods for performing basic mathematical operations, all methods of the math class are static methods, so you can use the class name directly when using the methods in that class. Method name, such as: Math.Round ();
Common methods:
Through the case we come to know their use it!!
Operation Result:
The Ps:math class also offers a number of other ways that you can focus on wikis and find out more about them.
Public classHelloWorld { Public Static voidMain (string[] args) {//defines an integer array with a length of ten int[] Nums =New int[10]; //Assigning a value to an array by looping for(inti = 0; i < nums.length; i++) { //generates a random number within 10 intx = (int) (Math.random () *10); Nums[i]= x;//Assigning a value to an element } //using the Foreach loop to output elements in an array for(intnum:nums) {System.out.print (num+ " "); } }}
Common classes in Java