1、java中会存在内存泄漏吗,请简单描述。 会。如:int i,i2; return (i-i2); //when i为足够大的正数,i2为足够大的负数。结果会造成溢位,导致错误。 2、java中实现多态的机制是什么? 方法的重写Overriding和重载Overloading是Java多态性的不同表现。重写Overriding是父类与子类之间多态性的一种表现,重载Overloading是一个类中多态性的一种表现。
3、什么是java序列化,如何实现java序列化? 序列化就是一种用来处理对象流的机制,所谓对象流也就是将对象的内容进行流化。可以对流化后的对象进行读写操作,也可将流化后的对象传输于网络之间。序列化是为了解决在对对象流进行读写操作时所引发的问题。 序列化的实现:将需要被序列化的类实现Serializable接口,该接口没有需要实现的方法,implements Serializable只是为了标注该对象是可被序列化的,然后使用一个输出流(如:FileOutputStream)来构造一个ObjectOutputStream(对象流)对象,接着,使用ObjectOutputStream对象的writeObject(Object obj)方法就可以将参数为obj的对象写出(即保存其状态),要恢复的话则用输入流。
4、写clone()方法时,通常都有一行代码,是什么? Clone 有缺省行为,super.clone();他负责产生正确大小的空间,并逐位复制。
5、List、Map、Set三个接口,存取元素时,各有什么特点? List 以特定次序来持有元素,可有重复元素。Set 无法拥有重复元素,内部排序。Map 保存key-value值,value可多值。
6、 abstract class Name { private String name; public abstract boolean isStupidName(String name) {} } 这有何错误? 答案: 错。abstract method必须以分号结尾,且不带花括号。
7、public class Something { void doSomething () { private String s = ""; int l = s.length(); } } 有错吗? 答案: 错。局部变量前不能放置任何访问修饰符 (private,public,和protected)。final可以用来修饰局部变量 (final如同abstract和strictfp,都是非访问修饰符,strictfp只能修饰class和method而非variable)。
8、abstract class Something { private abstract String doSomething (); } 这好像没什么错吧? 答案: 错。abstract的methods不能以private修饰。abstract的methods就是让子类implement(实现)具体细节的,怎么可以用private把abstract method封锁起来呢? (同理,abstract method前不能加final)。
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; } 和上面的很相似,都是关于final的问题,这有错吗? 答案: 正确。在addOne method中,参数o被修饰成final。如果在addOne method里我们修改了o的reference (比如: o = new Other();),那么如同上例这题也是错的。但这里修改的是o的member vairable (成员变量),而o的reference并没有改变。
10、金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥1011)->(一千零一拾一元整)输出。 package test.format; import java.text.NumberFormat; import java.util.HashMap; public class SimpleMoneyFormat { public static final String EMPTY = ""; public static final String ZERO = "零"; public static final String ONE = "壹"; public static final String TWO = "贰"; public static final String THREE = "叁"; public static final String FOUR = "肆"; public static final String FIVE = "伍"; public static final String SIX = "陆"; public static final String SEVEN = "柒"; public static final String EIGHT = "捌"; public static final String NINE = "玖"; public static final String TEN = "拾"; public static final String HUNDRED = "佰"; public static final String THOUSAND = "仟"; public static final String TEN_THOUSAND = "万"; public static final String HUNDRED_MILLION = "亿"; public static final String YUAN = "元"; public static final String JIAO = "角"; public static final String FEN = "分"; public static final String DOT = ".";
private static SimpleMoneyFormat formatter = null; private HashMap chineseNumberMap = new HashMap(); private HashMap chineseMoneyPattern = new HashMap(); private NumberFormat 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))); } //拾佰仟万亿等都是汉字里面才有的单位,加上它们 int indexOfDot = cMoneyStringBuffer.indexOf(DOT); int moneyPatternCursor = 1; for (int i = indexOfDot - 1; i > 0; i--) { cMoneyStringBuffer.insert(i, chineseMoneyPattern.get(EMPTY + moneyPatternCursor)); moneyPatternCursor = moneyPatternCursor == 8 ? 1 : moneyPatternCursor + 1; }
String fractionPart = cMoneyStringBuffer.substring(cMoneyStringBuffer.indexOf(".")); cMoneyStringBuffer.delete(cMoneyStringBuffer.indexOf("."), cMoneyStringBuffer.length()); while (cMoneyStringBuffer.indexOf("零拾") != -1) { cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零拾"), cMoneyStringBuffer.indexOf("零拾") + 2, ZERO); } while (cMoneyStringBuffer.indexOf("零佰") != -1) { cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零佰"), cMoneyStringBuffer.indexOf("零佰") + 2, ZERO); } while (cMoneyStringBuffer.indexOf("零仟") != -1) { cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零仟"), cMoneyStringBuffer.indexOf("零仟") + 2, ZERO); } while (cMoneyStringBuffer.indexOf("零万") != -1) { cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零万"), cMoneyStringBuffer.indexOf("零万") + 2, TEN_THOUSAND); } while (cMoneyStringBuffer.indexOf("零亿") != -1) { cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零亿"), cMoneyStringBuffer.indexOf("零亿") + 2, HUNDRED_MILLION); } while (cMoneyStringBuffer.indexOf("零零") != -1) { cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零零"), cMoneyStringBuffer.indexOf("零零") + 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);
|