Java basics 3

Source: Internet
Author: User

1. Is there any memory leakage in Java? Please briefly describe it.
Yes. For example: int I, I2; Return (i-i2); // when I is a positive number large enough, I2 is a negative number large enough. The result may cause overflow and errors.
2. What is the mechanism for implementing polymorphism in Java?
Overriding and overloading are different manifestations of Java polymorphism. Overriding is a manifestation of the polymorphism between the parent class and the Child class, and overloading is a manifestation of the polymorphism in a class.

3. What is Java serialization and how to implement Java serialization?
Serialization is a mechanism for processing object streams. Object streams are called object streams that stream object content. You can perform read and write operations on Streaming objects, or transfer streaming objects between networks. Serialization aims to solve the problems caused by read/write operations on Object streams.

Serialization implementation: implement the serializable interface for the class to be serialized. There is no implementation method for this interface. implements serializable is only to mark that the object can be serialized, then, an output stream (such as fileoutputstream) is used to construct an objectoutputstream (Object stream) object. Then, the writeobject (Object OBJ) of the objectoutputstream object is used) method, you can write the object whose parameter is OBJ (that is, save its status). To restore the object, use the input stream.

4. When writing the clone () method, there is usually a line of code. What is it?
Clone has a default action. Super. Clone () is responsible for generating the correct size of space and copying data by bit.

5. What are the features of the interfaces list, map, and set when accessing elements?
A list holds elements in a specific order, and can contain duplicate elements. Set cannot have repeated elements and can be sorted internally. Map stores the key-value, which can be multiple values.

6,
Abstract class name {
Private string name;
Public Abstract Boolean isstupidname (string name ){}
}
What is the error?
Answer: Yes. Abstract method must end with a semicolon without curly braces.

7. Public class something {
Void dosomething (){
Private string S = "";
Int L = S. Length ();
}
}
Is it wrong?
Answer: Yes. You cannot place any access modifiers (private, public, and protected) before local variables ). Final can be used to modify local variables.
(Like abstract and strictfp, final is not an access modifier. strictfp can only modify class and method rather than variable ).

8. abstract class something {
Private abstract string dosomething ();
}
Is there nothing wrong with this?
Answer: Yes. Abstract methods cannot be modified in private mode. Abstract METHODS is to make the sub-class implement (Implementation) specific details, how can I use private to abstract

What about method blocking? (Likewise, final cannot be added before abstract method ).

9. Public class something {
Public static void main (string [] ARGs ){
Other o = new other ();
New something (). addone (O );
}
Public void addone (final other O ){
O. I ++;
}
}
Class other {
Public int I;
}
Similar to the above, they are all about final. Is this wrong?
Answer: Correct. In addone method, the parameter o is modified to final. If we modify the reference of o in addone method
(For example, O = new other. But here we modify the member vairable of O.
(Member variable), and O's reference has not changed.

10. amount conversion: The amount of Arabic numerals is converted to the traditional Chinese format, for example, (¥1011)-> (one thousand and one yuan) output.
Package test. format;
Import java. Text. numberformat;
Import java. util. hashmap;
Public class simplemoneyformat {
Public static final string empty = "";
Public static final string zero = "zero ";
Public static final string one = "one ";
Public static final string two = "";
Public static final string three = "";
Public static final string four = "Si ";
Public static final string five = "Wu ";
Public static final string six = "";
Public static final string seven = "success ";
Public static final string eight = "success ";
Public static final string nine = "Hangzhou ";
Public static final string ten = "pick up ";
Public static final string hundred = "success ";
Public static final string thousand = "Hangzhou ";
Public static final string ten_thousand = "Ten Thousand ";
Public static final string hundred_million = "";
Public static final string yuan = "Yuan ";
Public static final string Jiao = "";
Public static final string Fen = "min ";
Public static final string dot = ".";

Private Static simplemoneyformat formatter = NULL;
Private hashmap chinesenumbermap = new hashmap ();
Private hashmap chinesemoneypattern = new hashmap ();
Private numberformat = numberformat. getinstance ();

Private simplemoneyformat (){
Numberformat. setmaximumfractiondigits (4 );
Numberformat. setminimumfractiondigits (2 );
Numberformat. setgroupingused (false );

Chinesenumbermap. Put ("0", zero );
Chinesenumbermap. Put ("1", one );
Chinesenumbermap. Put ("2", two );
Chinesenumbermap. Put ("3", three );
Chinesenumbermap. Put ("4", four );
Chinesenumbermap. Put ("5", five );
Chinesenumbermap. Put ("6", six );
Chinesenumbermap. Put ("7", seven );
Chinesenumbermap. Put ("8", eight );
Chinesenumbermap. Put ("9", nine );
Chinesenumbermap. Put (dot, dot );

Chinesemoneypattern. Put ("1", ten );
Chinesemoneypattern. Put ("2", hundred );
Chinesemoneypattern. Put ("3", thousand );
Chinesemoneypattern. Put ("4", ten_thousand );
Chinesemoneypattern. Put ("5", ten );
Chinesemoneypattern. Put ("6", hundred );
Chinesemoneypattern. Put ("7", thousand );
Chinesemoneypattern. Put ("8", hundred_million );
}

Public static simplemoneyformat getinstance (){
If (formatter = NULL)
Formatter = new simplemoneyformat ();
Return formatter;
}

Public string format (string moneystr ){
Checkprecision (moneystr );
String result;
Result = converttochinesenumber (moneystr );
Result = addunitstochinesemoneystring (result );
Return result;
}

Public string format (double moneydouble ){
Return format (numberformat. Format (moneydouble ));
}

Public string format (INT moneyint ){
Return format (numberformat. Format (moneyint ));
}

Public string format (long moneylong ){
Return format (numberformat. Format (moneylong ));
}

Public string format (number moneynum ){
Return format (numberformat. Format (moneynum ));
}

Private string converttochinesenumber (string moneystr ){
String result;
Stringbuffer cmoneystringbuffer = new stringbuffer ();
For (INT I = 0; I <moneystr. Length (); I ++ ){
Cmoneystringbuffer. append (chinesenumbermap. Get (moneystr. substring (I, I + 1 )));

}
// Hundreds of millions of characters are all units in Chinese characters.
Int indexofdot = cmoneystringbuffer. indexof (DOT );
Int moneypatterncursor = 1;
For (INT I = indexofdot-1; I> 0; I --){
Cmoneystringbuffer. insert (I, chinesemoneypattern. Get (empty + moneypatterncursor ));

Moneypatterncursor = 8? 1: moneypatterncursor + 1;
}

String fractionpart = cmoneystringbuffer. substring (cmoneystringbuffer. indexof ("."));

Cmoneystringbuffer. Delete (cmoneystringbuffer. indexof ("."), cmoneystringbuffer. Length ());

While (cmoneystringbuffer. indexof ("0 ")! =-1 ){
Cmoneystringbuffer. Replace (cmoneystringbuffer. indexof ("Zero pick"), cmoneystringbuffer. indexof ("Zero pick") + 2, zero );

}
While (cmoneystringbuffer. indexof ("0 bytes ")! =-1 ){
Cmoneystringbuffer. Replace (cmoneystringbuffer. indexof ("zero latency"), cmoneystringbuffer. indexof ("zero latency") + 2, zero );

}
While (cmoneystringbuffer. indexof ("0 bytes ")! =-1 ){
Cmoneystringbuffer. Replace (cmoneystringbuffer. indexof ("zero latency"), cmoneystringbuffer. indexof ("zero latency") + 2, zero );

}
While (cmoneystringbuffer. indexof ("0 thousand ")! =-1 ){
Cmoneystringbuffer. Replace (cmoneystringbuffer. indexof ("0 thousand"), cmoneystringbuffer. indexof ("0 thousand") + 2, ten_thousand );

}
While (cmoneystringbuffer. indexof ("0 billion ")! =-1 ){
Cmoneystringbuffer. Replace (cmoneystringbuffer. indexof ("0 billion"), cmoneystringbuffer. indexof ("0 billion") + 2, hundred_million );

}
While (cmoneystringbuffer. indexof ("0 ")! =-1 ){
Cmoneystringbuffer. Replace (cmoneystringbuffer. indexof ("zero"), cmoneystringbuffer. indexof ("zero") + 2, zero );

}
If (cmoneystringbuffer. lastindexof (zero) = cmoneystringbuffer. Length ()-1)
Cmoneystringbuffer. Delete (cmoneystringbuffer. Length ()-1, cmoneystringbuffer. Length ());

Cmoneystringbuffer. append (fractionpart );

Result = cmoneystringbuffer. tostring ();
Return result;
}

Private string addunitstochinesemoneystring (string moneystr ){
String result;
Stringbuffer cmoneystringbuffer = new stringbuffer (moneystr );
Int indexofdot = cmoneystringbuffer. indexof (DOT );
Cmoneystringbuffer. Replace (indexofdot, indexofdot + 1, Yuan );

1. Is there any memory leakage in Java? Please briefly describe it.
Yes. For example: int I, I2; Return (i-i2); // when I is a positive number large enough, I2 is a negative number large enough. The result may cause overflow and errors.
2. What is the mechanism for implementing polymorphism in Java?
Overriding and overloading are different manifestations of Java polymorphism. Overriding is a manifestation of the polymorphism between the parent class and the Child class, and overloading is a manifestation of the polymorphism in a class.

3. What is Java serialization and how to implement Java serialization?
Serialization is a mechanism for processing object streams. Object streams are called object streams that stream object content. You can perform read and write operations on Streaming objects, or transfer streaming objects between networks. Serialization aims to solve the problems caused by read/write operations on Object streams.

Serialization implementation: implement the serializable interface for the class to be serialized. There is no implementation method for this interface. implements serializable is only to mark that the object can be serialized, then, an output stream (such as fileoutputstream) is used to construct an objectoutputstream (Object stream) object. Then, the writeobject (Object OBJ) of the objectoutputstream object is used) method, you can write the object whose parameter is OBJ (that is, save its status). To restore the object, use the input stream.

4. When writing the clone () method, there is usually a line of code. What is it?
Clone has a default action. Super. Clone () is responsible for generating the correct size of space and copying data by bit.

5. What are the features of the interfaces list, map, and set when accessing elements?
A list holds elements in a specific order, and can contain duplicate elements. Set cannot have repeated elements and can be sorted internally. Map stores the key-value, which can be multiple values.

6,
Abstract class name {
Private string name;
Public Abstract Boolean isstupidname (string name ){}
}
What is the error?
Answer: Yes. Abstract method must end with a semicolon without curly braces.

7. Public class something {
Void dosomething (){
Private string S = "";
Int L = S. Length ();
}
}
Is it wrong?
Answer: Yes. You cannot place any access modifiers (private, public, and protected) before local variables ). Final can be used to modify local variables.
(Like abstract and strictfp, final is not an access modifier. strictfp can only modify class and method rather than variable ).

8. abstract class something {
Private abstract string dosomething ();
}
Is there nothing wrong with this?
Answer: Yes. Abstract methods cannot be modified in private mode. Abstract METHODS is to make the sub-class implement (Implementation) specific details, how can I use private to abstract

What about method blocking? (Likewise, final cannot be added before abstract method ).

9. Public class something {
Public static void main (string [] ARGs ){
Other o = new other ();
New something (). addone (O );
}
Public void addone (final other O ){
O. I ++;
}
}
Class other {
Public int I;
}
Similar to the above, they are all about final. Is this wrong?
Answer: Correct. In addone method, the parameter o is modified to final. If we modify the reference of o in addone method
(For example, O = new other. But here we modify the member vairable of O.
(Member variable), and O's reference has not changed.

10. amount conversion: The amount of Arabic numerals is converted to the traditional Chinese format, for example, (¥1011)-> (one thousand and one yuan) output.
Package test. format;
Import java. Text. numberformat;
Import java. util. hashmap;
Public class simplemoneyformat {
Public static final string empty = "";
Public static final string zero = "zero ";
Public static final string one = "one ";
Public static final string two = "";
Public static final string three = "";
Public static final string four = "Si ";
Public static final string five = "Wu ";
Public static final string six = "";
Public static final string seven = "success ";
Public static final string eight = "success ";
Public static final string nine = "Hangzhou ";
Public static final string ten = "pick up ";
Public static final string hundred = "success ";
Public static final string thousand = "Hangzhou ";
Public static final string ten_thousand = "Ten Thousand ";
Public static final string hundred_million = "";
Public static final string yuan = "Yuan ";
Public static final string Jiao = "";
Public static final string Fen = "min ";
Public static final string dot = ".";

Private Static simplemoneyformat formatter = NULL;
Private hashmap chinesenumbermap = new hashmap ();
Private hashmap chinesemoneypattern = new hashmap ();
Private numberformat = numberformat. getinstance ();

Private simplemoneyformat (){
Numberformat. setmaximumfractiondigits (4 );
Numberformat. setminimumfractiondigits (2 );
Numberformat. setgroupingused (false );

Chinesenumbermap. Put ("0", zero );
Chinesenumbermap. Put ("1", one );
Chinesenumbermap. Put ("2", two );
Chinesenumbermap. Put ("3", three );
Chinesenumbermap. Put ("4", four );
Chinesenumbermap. Put ("5", five );
Chinesenumbermap. Put ("6", six );
Chinesenumbermap. Put ("7", seven );
Chinesenumbermap. Put ("8", eight );
Chinesenumbermap. Put ("9", nine );
Chinesenumbermap. Put (dot, dot );

Chinesemoneypattern. Put ("1", ten );
Chinesemoneypattern. Put ("2", hundred );
Chinesemoneypattern. Put ("3", thousand );
Chinesemoneypattern. Put ("4", ten_thousand );
Chinesemoneypattern. Put ("5", ten );
Chinesemoneypattern. Put ("6", hundred );
Chinesemoneypattern. Put ("7", thousand );
Chinesemoneypattern. Put ("8", hundred_million );
}

Public static simplemoneyformat getinstance (){
If (formatter = NULL)
Formatter = new simplemoneyformat ();
Return formatter;
}

Public string format (string moneystr ){
Checkprecision (moneystr );
String result;
Result = converttochinesenumber (moneystr );
Result = addunitstochinesemoneystring (result );
Return result;
}

Public string format (double moneydouble ){
Return format (numberformat. Format (moneydouble ));
}

Public string format (INT moneyint ){
Return format (numberformat. Format (moneyint ));
}

Public string format (long moneylong ){
Return format (numberformat. Format (moneylong ));
}

Public string format (number moneynum ){
Return format (numberformat. Format (moneynum ));
}

Private string converttochinesenumber (string moneystr ){
String result;
Stringbuffer cmoneystringbuffer = new stringbuffer ();
For (INT I = 0; I <moneystr. Length (); I ++ ){
Cmoneystringbuffer. append (chinesenumbermap. Get (moneystr. substring (I, I + 1 )));

}
// Hundreds of millions of characters are all units in Chinese characters.
Int indexofdot = cmoneystringbuffer. indexof (DOT );
Int moneypatterncursor = 1;
For (INT I = indexofdot-1; I> 0; I --){
Cmoneystringbuffer. insert (I, chinesemoneypattern. Get (empty + moneypatterncursor ));

Moneypatterncursor = 8? 1: moneypatterncursor + 1;
}

String fractionpart = cmoneystringbuffer. substring (cmoneystringbuffer. indexof ("."));

Cmoneystringbuffer. Delete (cmoneystringbuffer. indexof ("."), cmoneystringbuffer. Length ());

While (cmoneystringbuffer. indexof ("0 ")! =-1 ){
Cmoneystringbuffer. Replace (cmoneystringbuffer. indexof ("Zero pick"), cmoneystringbuffer. indexof ("Zero pick") + 2, zero );

}
While (cmoneystringbuffer. indexof ("0 bytes ")! =-1 ){
Cmoneystringbuffer. Replace (cmoneystringbuffer. indexof ("zero latency"), cmoneystringbuffer. indexof ("zero latency") + 2, zero );

}
While (cmoneystringbuffer. indexof ("0 bytes ")! =-1 ){
Cmoneystringbuffer. Replace (cmoneystringbuffer. indexof ("zero latency"), cmoneystringbuffer. indexof ("zero latency") + 2, zero );

}
While (cmoneystringbuffer. indexof ("0 thousand ")! =-1 ){
Cmoneystringbuffer. Replace (cmoneystringbuffer. indexof ("0 thousand"), cmoneystringbuffer. indexof ("0 thousand") + 2, ten_thousand );

}
While (cmoneystringbuffer. indexof ("0 billion ")! =-1 ){
Cmoneystringbuffer. Replace (cmoneystringbuffer. indexof ("0 billion"), cmoneystringbuffer. indexof ("0 billion") + 2, hundred_million );

}
While (cmoneystringbuffer. indexof ("0 ")! =-1 ){
Cmoneystringbuffer. Replace (cmoneystringbuffer. indexof ("zero"), cmoneystringbuffer. indexof ("zero") + 2, zero );

}
If (cmoneystringbuffer. lastindexof (zero) = cmoneystringbuffer. Length ()-1)
Cmoneystringbuffer. Delete (cmoneystringbuffer. Length ()-1, cmoneystringbuffer. Length ());

Cmoneystringbuffer. append (fractionpart );

Result = cmoneystringbuffer. tostring ();
Return result;
}

Private string addunitstochinesemoneystring (string moneystr ){
String result;
Stringbuffer cmoneystringbuffer = new stringbuffer (moneystr );
Int indexofdot = cmoneystringbuffer. indexof (DOT );
Cmoneystringbuffer. Replace (indexofdot, indexofdot + 1, Yuan );

Related Article

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.