"Learning Notes" Java basic type conversion and date manipulation and Math

Source: Internet
Author: User

I. Basic type conversions

In Java, everything is an object, but the basic data type is not an object. Therefore, the wrapper class object for the base data type is provided in the JDK.
Package class for eight basic data types (wrapper class):
Short---> Short
The eight basic data types override the Hashcode (), Equals (), toString () method in object.

Basic Type---> Package type
Integer inobj = new Integer (10);
System.out.println (Inobj);
Package Type---> Basic type
int i = Inobj.intvalue ();
System.out.println (i);
Basic type----> string
String stri = string.valueof (i);
System.out.println (Stri);
String----> Basic type
int inti = integer.parseint ("100");
System.out.println (Inti);

Examples (other usages are similar):

 Package Com.fjnu.study;  Public class Testchange {    publicstaticvoid  main (string[] args) {        int  i;         double b = 123.123;                 New Integer (ten);        System.out.println (int1);        System.out.println (integer.tobinarystring (3));}            }

Double turns into a string

 Package Com.fjnu.study;  Public class Testchange {    publicstaticvoid  main (string[] args) {          Double i = 1234.344;         = string.valueof (i);        System.out.println (str);        System.out.println (Str.length ());}    }

String converted to int type

 Package Com.fjnu.study;  Public class Testchange {    publicstaticvoid  main (string[] args) {        = "12345" ;         int i = integer.parseint (str);        System.out.println (str);}    }

Character method

Common methods:
Static Boolean isdigit (char ch)//Determine if a number
Static Boolean isletter (char ch)
Static Boolean isletterordigit (char ch)
Static Boolean islowercase (char ch)
Static Boolean isuppercase (char ch)
Static char tolowercase (char ch)
Static char touppercase (char ch)
Static Boolean Isspacechar (char ch)
String toString ()

Two. How to get the system time

Using the System class
Using the Java.util.Date class
Using the Java.util.Calendar class

1. Using the System class

Consult the Documentation:

Currenttimemillis  Public Static Long  The time difference (measured in milliseconds) between midnight on January 1, 1970.

2. Using the Java.util.Date class

To get the system time, and the time inside is used to process the database.

Package ImportPublicclass  testtime {    public  staticvoid  main (string[] args) {        new  Date ();        SYSTEM.OUT.PRINTLN (date);}    }
Sat APR 11:26:16 CST 2015

3. Using the Java.util.Calendar class

The Calendar class is an abstract class used to describe a calendar. This class cannot be initialized directly, but there is a class method getinstance () used to create a calendar object.
Common methods:
Static Calendar getinstance ();
int get (int field);
Date GetTime ()
int get (int field);
void set (int field, int value);
void SetTime (date date);
void Settimeinmillis (long Millis);

To illustrate:

  PackageCom.fjnu.study;Importjava.util.Date;ImportJava.util.Calendar; Public classTesttime { Public Static voidMain (string[] args) {Testtime.calendarmethod (); }    Private Static voidCalendarmethod () {Calendar Calendar=calendar.getinstance ();        SYSTEM.OUT.PRINTLN (Calendar);        System.out.println (Calendar.gettime ()); System.out.println (Calendar.get (Calendar.    year)); }}
Java.util.gregoriancalendar[time=1428723507803,arefieldsset=true, areallfieldsset=true, Lenient=true, zone=sun.util.calendar.zoneinfo[id= "Asia/shanghai", Offset=28800000,dstsavings=0, Usedaylight=false, transitions=19,lastrule=null],firstdayofweek=1,minimaldaysinfirstweek=1, Era=1,year=2015,month=3,week_of_year=15,week_of_month=2,day_of_month=11,day_of_year=101,day_of_week=7,day_of_ Week_in_month=2,am_pm=0,hour=11,hour_of_day=11,minute=38,second=27,millisecond=803,zone_offset=28800000,dst_ Offset=011:38:27 CST 20152015

Note: System.out.println (calendar.get (Calendar. year));
Get is the Get field here, you can't use calendar here. Year, no one came up with the result is 1

Three. Math

Common methods:
Static double abs (double A); There are overloaded methods
Static double Ceil (double A); There are overloaded methods
Static double floor (double a); There are overloaded methods
Static Long round (double a); There are overloaded methods
Static Double pow (double A, double b);
static double random ();

 Package Com.fjnu.study;  Public class Testmath {    publicstaticvoid  main (string[] args) {one        ();    }         privatestaticvoid One () {        System.out.println (Math.max (Math.max ( 1,30));}    }

Decimal operations:


Generate random numbers

Math.random ();
Using the Random Object

Important point: BigDecimal operation

"Learning Notes" Java basic type conversion and date manipulation and Math

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.