Java knowledge,

Source: Internet
Author: User

Java knowledge,

1. Conversion encoding:
Str = new String (str. getBytes ("ISO-8859-1"), "UTF-8 ");
**************************************** **********************
2. Conversion between data types:
Int a = (int) B;
Int a = B. intValue ();
Int a = Ingtger. parseInt (B );
Integer a = new Integer (B );
Int I = Integer. valueOf (B). intValue ();
String type:
String a = B. toString ();
String a = B + "";
A. startsWith (B); // contains
A. endsWith (B); // contains
A. equals (B); // equal
A. Repeated signorecase (B); // case insensitive
A. contains (B); // contains
**************************************** ************************
3. size comparison of double Data Types
Double a = 0.01;
Double B = 0.001;
BigDecimal data1 = new BigDecimal ();
BigDecimal data2 = new BigDecimal (B );
Double result = data1.compareTo (data2 );
Data1> data2 result = 1;
Data1 <data2 result =-1;
Data1 = data2 result = 0;
**************************************** **********************************
4. the formula for converting an IP address to a Long value is as follows:
String [] oraginal = this. value. split ("\\.");
Long o = 0l; // Original Value
O + = Long. parseLong (oraginal [I]) <(24-I * 8 );
**************************************** **************************************** ****
5. Obtain the name of the current method.
String methodName = Thread. currentThread (). getStackTrace () [1]. getMethodName ();
**************************************** **************************************** **************
6. Singleton example for a single instance
Public class SimpleSingleton {
Private static SimpleSingleton singleInstance = new SimpleSingleton ();

// Marking default constructor private
// To avoid direct instantiation.
Private SimpleSingleton (){
}

// Get instance for class SimpleSingleton
Public static SimpleSingleton getInstance (){

Return singleInstance;
}
}
**************************************** **************************************** ***********************
7. encryption and decryption
Encrypt. encode ("admin ");
Encrypt. decode (str1), "UTF-8 ")
**************************************** **************************************** *******************************
8. mongodb usage
DBCursor result1 = templates. first1_template (). getCollection ("alarm "). find (bdb, keys ). sort (new BasicDBObject ("_ id",-1 )). limit (1, 1000 );
Gson gson = new Gson ();
While (result1.hasNext ()){
DBObject obj = result1.next ();
Map <String, Object> mapp = obj. toMap ();
Iterator <Map. Entry <String, Object> entries = mapp. entrySet (). iterator ();
While (entries. hasNext ()){
Map. Entry <String, Object> entry = entries. next ();
}
}

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.