1. Why do we need packaging classes? What is the purpose of the packaging class?
A:
Because the java language is object-oriented, but the basic data type in java is not object-oriented. In practice, it is often necessary to convert the basic data into objects for easy operation.
Effect:
1. As a class type corresponding to the basic data type, it is convenient to operate on objects.
2. Include attributes related to each basic data type and how to operate them
2. Convert the string "12345" to a basic type of number?
A:
Convert by static method of Integer class
Integer.parseInt("12345");
3. Auto-packing and auto-unpacking refer to?
A:
Auto-boxing and auto-unboxing refer to the automatic conversion of basic data types and packaging classes to each other
4. Why is the String class called an immutable character sequence?
A:
Strings are constants, their values cannot be changed after they are created, String objects are immutable, so can
To share;
The underlying structure of the String class is an array value of type char, which is decorated with final
5. What is the relationship between the equals method of String class and the equals method of Object?
A:
The equals method of the String class overrides the equals method of the Object class to compare whether the contents of two String objects are the same
6. What is the function of the trim() method of the String class?
A:
Returns a copy of the string, removing spaces before and after the string
7. "JavaString". What is the result returned by substring(3, 7)?
A:
aStr
8. Analyse the results of the following code, draw a memory structure diagram, and say the results
public static void main(String[] args) { String a = "asd"; String a1 = "asd"; String a3 = "asd"+"fgh"; //Here the compiler is optimized! String a4 = "asdfgh"; String a5 = a1+"fgh"; String a2 = new String("asd"); System.out.println(a==a1);//true System.out.println(a3==a4);//true System.out.println(a4==a5);//false System.out.println(a4.equals(a5));//true }
9. What is the connection between StringBuffer and StringBuilder? What is the difference?
A:
StringBuffer and StringBuilder are variable character sequences, and the underlying array structure is an array of type char
StringBuffer: jdk1. Version 0, thread-safe but inefficient
StringBuilder: jdk1. Version 5, thread insecure but efficient
10. What happens to the following code? How many objects will be generated during the run?
String str= ""; for(int i=0;i<100;i++){ str+=i; }
A:
Causes a large number of replica string objects to remain in memory, resulting in 100 objects during inefficient operation. Using StringBuffer will have only one object
StringBuffer sb=new StringBuffer(); for(int i=0;i<100;i++){ sb.append(i); }
11. System. What does currentTimeMillis () mean?
A:
Returns the current time in milliseconds
12. Does Date d = new Date() represent the current time?
A:
Yes, to milliseconds
13. Write out the SimpleDateFormat class to convert time to string?
A:
public static void main(String[] args) { //String to Date SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); try { Date parse = sdf.parse("2000-2-3"); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } //Date to String SimpleDateFormat sdfe=new SimpleDateFormat("yyyy-MM-dd"); String format = sdfe.format(new Date()); }
14. Does the File class represent a directory?
A:
can
15. What is the difference between the File class method mkdir and mkdirs?
A:
mkdir: folders can only be created in existing directories
mkdirs: folders can be created in non-existent directories
16. When do you use enumerations? What is the definition of enumeration?
A:
Use enumeration types when you need to define a set of constants
Definition of enumeration:
Only one of the specified values can be taken
Use enum keyword
All enumeration types implicitly inherit from java.lang.Enum
public enum EnumTest {
MON, TUE, WED, THU, FRI, SAT, SUN;
}
17. Can the result of an expression in a switch statement be an enumeration?
A:
enum EnumTest { MON, TUE, WED, THU, FRI, SAT, SUN; } public class Stringstr { public static void main(String[] args) { EnumTest test = EnumTest.TUE; switch (test) { case MON: System.out.println("Today is Monday"); break; case TUE: System.out.println("Today is Tuesday"); break; default: System.out.println(test); break; } } }