The use of some common Java classes

Source: Internet
Author: User
Tags square root

 Public classDemo { Public Static voidMain (string[] args) {//DecimalFormat class under Java.text package, date formatting class SimpleDateFormat//#表示这一位存在就显示, does not exist on the display, #后的0表示存在就显示, does not exist display 0,. The following means to retain 2 decimal placesJava.text.DecimalFormat dfdecimalformat=NewJava.text.DecimalFormat ("#0.00"); System.out.println (Dfdecimalformat.format (12.43453));//12.43Java.text.DecimalFormat dfdecimalformat1=NewJava.text.DecimalFormat ("#.00"); System.out.println (Dfdecimalformat1.format (0.43453));//output. 43, front 0. A few words, 0 will be omitted.System.out.println (Dfdecimalformat1.format (4.43453));//output 4.43, so format ("#0.00") GoodSimpleDateFormat SF=NewSimpleDateFormat ("yyyy mm month DD Day"); Date D=NewDate (); System.out.println (Sf.format (d));//Formatting Date Objects January 10, 2015sf=NewSimpleDateFormat ("yyyy mm month DD Day HH:MM:SS"); System.out.println (Sf.format (d));//January 10, 2015 04:46:06//Java.lang Package: Math Mathematical Formula ClassSystem.out.println (Math.PI);//PiSystem.out.println (Math.Abs (-100));//Absolute ValueSystem.out.println (Math.ceil (4.1232));//Rounding up 5.0System.out.println (Math.ceil (-4.1232));//Rounding up -4.0System.out.println (Math.floor (4.1232));//Rounding down 4.0System.out.println (Math.floor (-4.1232));//Rounding down -5.0System.out.println (Math.Round (4.432));//Rounding 4System.out.println (Math.Round (4.654));//5System.out.println (MATH.SQRT (100));//Square root 10.0System.out.println (Math.pow (2, 3));//2 of the three-time Square 8.0System.out.println (Math.random ());//generate random numbers between 0 and 1//The random class under the Java.util package, date class, Calendar classRandom random=NewRandom (); System.out.println (Random.nextint ());//print random integers plus or minus, with no definite rangeSystem.out.println (Random.nextint (100));//print random integers [0 to]java.util.Date Date=NewJava.util.Date ();//get the current system date ObjectSYSTEM.OUT.PRINTLN (date);//Standard Time Format//Sat Jan 13:57:21 CST//constructs the Java.sql.Date object first through the Java.util.Data objectJava.sql.Date date2=Newjava.sql.Date (Date.gettime ());  System.out.println (DATE2); //2015-01-10Calendar Calendar=calendar.getinstance (); //Calendar provides a class method getinstance to get a generic object of this typeSystem.out.println (Calendar); intYear=Calendar.get (calendar.year); intMonth=Calendar.get (Calendar.month); intday=Calendar.get (Calendar.day_of_month); intHour=Calendar.get (Calendar.hour_of_day); intMinute=Calendar.get (Calendar.minute); intSecond=Calendar.get (Calendar.second); System.out.println ( Year+ "-" +month+ "-" +day+ "" +hour+ ":" +minute+ ":" +second); //the String class and the StringBuffer class//String: Content is immutable, changing content is creating new objects//stringbuffer: Content variable, change content does not change objectString s=NewString ("I Love You");//S is a reference to the stack memory, the right is the value of the heap memory, s points to the heap memory "I Love You"System.out.println (s);        System.out.println (S.hashcode ()); S+ = "China";        System.out.println (s); System.out.println (S.hashcode ());//Hashcode, the equivalent of creating a new objectStringBuffer SB=NewStringBuffer ("I Love You");        System.out.println (SB); System.out.println (Sb.hashcode ());//508839021Sb.append ("China"); System.out.println (SB);//I Love your ChinaSystem.out.println (Sb.reverse ());//inverse of string anihc uoy Evol ISystem.out.println (Sb.hashcode ());//508839021,hashcode, equivalent to appending on a far object, without creating a new object//StringBuilder: Thread non-secure, StringBuffer: Thread-Safe//StringBuffer and StringBuilder, they are string variables, are objects that can be changed, and whenever we use them to manipulate strings,//actually operates on an object and does not create as many objects as a string, so the speed is fast.//1. If you want to manipulate a small amount of data with = String//2. Manipulating large amounts of data under a single-threaded operation string buffer = StringBuilder//3. Multi-threaded operation string buffer operation large amount of data = StringBuffer    }

The use of some common Java classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.