Chapter VII common class

1. Why do you need packaging? Wrapper class action is?

Because the java language is object-oriented language, but the basic data types in the java is not object-oriented, but in actual use is often necessary basic data into objects, easy to operate. For example, when storing data sets, you can store objects

effect:

[1] and the presence of basic data types as corresponding to the class type, easy operation relates to the subject

[2] contains basic data type associated with each property and the related method of operation

 

2. The string "123" into the digital basic types of options?

Integer class static method to convert

Integer.parseInt(“123”);

 

 

3. Auto boxing and auto-unboxing mean? Illustration.

Automatic boxing and unboxing automatically refers to the basic data types and automatic packaging interconversion

 

237. [machine] to complete the classroom teacher Integer test, and write their own test code class Double

public class TestDouble {

public static void main(String[] args) {

Double int1 = new Double(10);

Double int2 = Double.valueOf(20);

double a = int1.doubleValue();

Double int3 = Double.parseDouble("334");

Double int4 = new Double("999");

String str1 = int3.toString();

}

}

 

238. Why String class is called immutable character sequence? From the String class source code analysis, explanation is given.

Strings are constants, their values ​​can not be changed after creation, String objects are immutable, so you can share

String class underlying structure is an array of char value, and this array can be modified with the final

 

239. [machine] equals method with the String class equals method of Object What is the relationship? And read more of the String class equals method source code analysis of its internal processes.

String class equals method of Object class overrides the equals method for comparing two objects have the same contents String

 

Source String class equals method of analysis:

If the memory address (reference) two String objects are the same, then return true

If two String object memory address (reference) are not the same, then the type determination, if it is not of type String, so direct returns false, if it is of type String downcast is performed, is converted into String type, and then change the char type variable arrays, compare the contents of two arrays are the same corresponding position, if the same returns true, if different returns false

 

240.String class trim () method is what role?

Returns a copy of string, remove the spaces before and after the string

 

241. The results "hamburger" .substring (4, 8) returns?

 urge

 

[242.] Analysis of the machine of the following code, and memory configuration shown in FIG, gives the results for each printed character

Explanation.

String s = "abc";

String ss = "abc";

String s3 = "abc" + "def"; // here compilers optimized!

String s4 = "abcdef";

String s5 = ss+"def";

String s2 = new String("abc");

System.out.println(s==ss); 

System.out.println(s3==s4);

System.out.println(s4==s5);

System.out.println(s4.equals(s5));

 

System.out.println (s == ss); the result is true

System.out.println (s3 == s4); the result is true

System.out.println (s4 == s5); the result is false

System.out.println (s4.equals (s5)); the result is true

 

 

 

 

243. [machine] practice commonly used method of the String class

Length of string:

System.out.println(“hello”.length());

Remove the spaces before and after the string:

System.out.println(“  hello  world  ”.trim());

Interception substring:

System.out.println(“helloworld”.substring(0,4));

 

244.StringBuffer and StringBuilder links are? The difference is?

contact:

StringBuffer and StringBuilder are variable sequence of characters, the underlying structure of the array is an array of char

the difference:

StringBuffer: jdk1.0 version, thread-safe, but low efficiency

StringBuilder: jdk1.5 version, thread-safe, but efficient

The following code 245. What are the consequences? How many objects produced during operation? Modify the code to use StringBuilder.

String s = "";

for(int i=0;i<10000;i++){

s +=i;

}

It will lead to a large number of copies string objects remain in memory, reducing efficiency

It will produce 10,001 objects during operation

StringBuffer sb=new StringBuffer();

for(int i=0;i<10000;i++){

sb.append(i);

}

 

246. How computer time is represented?

Obtained from 1970-1-10: 0: 0 milliseconds to the current elapsed time, date or time and then converted to

 

247.System.currentTimeMillis () What does it mean?

Returns the current time in milliseconds

 

248.Date d = new Date () indicates the current time it is?

The precise millisecond

 

249. [machine] we use SimpleDateFormat classes to implement with time into a string. That two commonly used

A method? And write code, illustration

// string transfer date

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

sdf.parse("1990-3-3");

// Date to String

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

sdf.format(new Date());

 

250. The March 3, 1990 is represented by a Calendar, and come to this day is the first day of the year? The Day

Period increased by 35 days, is the day? Use the code to illustrate.

public static void main(String[] args) throws ParseException {

Calendar c=new GregorianCalendar();

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

c.setTime(sdf.parse("1990-3-3"));

System.out.println(c.get(Calendar.DAY_OF_YEAR));

c.add(Calendar.DATE, 35);

System.out.println(c.get(Calendar.YEAR)+"年"

+(c.get(Calendar.MONTH)+1)+"月"+c.get(Calendar.DATE));

}

 

251. [machine] to write test code usage Date / SimpleDateFormat / Calendar of.

// Date of usage

public static void main(String[] args) {

Date date1 = new Date();

System.out.println(date1.toString());

}

// SimpleDateFormat usage

SimpleDateFormat s1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

String daytime = s1.format(new Date());

// Calendar of usage

GregorianCalendar calendar2 = new GregorianCalendar();

calendar2.set(Calendar.YEAR, 2009);

 

252. [machine] to imitate the teacher's written, visual calendar to complete the development program.

public class AppMain {

public static void main(String[] args) throws ParseException {

System.out.println ( "Please enter the date (format: 2010-3-3):");

Scanner  scanner = new Scanner(System.in);

String  dateString =scanner.nextLine();   

System.out.println ( "date that you just entered is:" + dateString);

String[]  str = dateString.split("-");

int year = Integer.parseInt(str[0]);

int month = new Integer(str[1]);

int day = new Integer(str[2]);

Calendar c = new GregorianCalendar(year,month-1, day);   

c.set(Calendar.DATE, 1);

// week: 1-7 day one hundred twenty-three thousand four hundred fifty-six

int  dow = c.get(Calendar.DAY_OF_WEEK); 

System.out.println ( "Day \ t a \ t two \ t three \ t four \ t five \ t six");

for(int i=0;i<dow-1;i++){

System.out.print("\t");

}

int maxDate = c.getActualMaximum(Calendar.DATE);

for(int i=1;i<=maxDate;i++){

StringBuilder  sBuilder = new StringBuilder();

if(c.get(Calendar.DATE)==day){

sBuilder.append(c.get(Calendar.DATE)+"*\t");

}else{

sBuilder.append(c.get(Calendar.DATE)+"\t");

}

System.out.print(sBuilder);

if(c.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY){

System.out.print("\n");

}

c.add(Calendar.DATE, 1);

}

}

 

253.File class to represent a catalog? 

can

 

254.File class method with mkdir mkdirs, what's the difference?

mkdir: you can create a folder in the directory already exists

mkdirs: You can create a folder in the directory does not exist

 

255. File class using an analog implementation of DOS dir and dir / s command

public static void main(String[] args) {

File file = new File("e:/教学1");

showTree(file, 1);

}

public static void showTree(File file, int level){

File[] files = file.listFiles();

for (File f : files) {

for(int i = 0; i < level; i++){

System.out.print("-");

}

if (f.isDirectory()) {

System.out.println(f.getName());

showTree(f, level+1);

}else{

System.out.println(f.getName());

}

}

}

 

256. [machine] using a recursive algorithm, based on the tree structure of the code of the teacher, showing a complete directory tree

import java.io.File;

public class FileTree {

public static void main(String[] args) {

File f = new File("d:/1005班");

printFile(f, 0);

}

 

static void printFile(File file,int level){

for (int i = 0; i < level; i++) {

System.out.print("-");

}

System.out.println(file.getName()); 

if(file.isDirectory()){

File[]  files = file.listFiles();

for (File temp : files) {

printFile(temp, level+1);

}

}

}

}

257. When using enumeration? What is the definition of enumeration?

When it is desired defining a set of constants , the enum

Enum definition:

You can only take one particular value of

Use the keyword enum

All enumerated types implicitly inherit from java.lang.Enum

 

258. [machine] manually define an enumeration representing the twelve months of the English month.

public enum Month {

JANUARY,FEBRUARY,MARCH,APRIL,MAY,JUNE,JULY,AUGUST,SEPTEMBER,OCTOBER,NOVEMBER,DECEMBER

}

 

259. The result of the expression [machine] switch statement can enumerate it? Imitate classroom code to write an example to show.

public static String testEnum(Month m){

String str=null;

switch (m) {

case JANUARY:

str = "January";

break;

case FEBRUARY:

str = "February";

break;

default:

break;

}

return str;

}


Guess you like

Origin www.cnblogs.com/ren549047861/p/11293981.html
Recommended