Java程序员面试宝典-Java代码查错精品资料.doc
《Java程序员面试宝典-Java代码查错精品资料.doc》由会员分享,可在线阅读,更多相关《Java程序员面试宝典-Java代码查错精品资料.doc(39页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、JAVA代码查错1.abstract class Name private String name;public abstract boolean isStupidName(String name) 大侠们,这有何错误?答案: 错。abstract method必须以分号结尾,且不带花括号。2.public class Something void doSomething () private String s = ; int l = s.length(); 有错吗?答案: 错。局部变量前不能放置任何访问修饰符 (private,public,和protected)。final可以用来修饰局部
2、变量(final如同abstract和strictfp,都是非访问修饰符,strictfp只能修饰class和method而非variable)。3.abstract class Something private abstract String doSomething ();这好像没什么错吧?答案: 错。abstract的methods不能以private修饰。abstract的methods就是让子类implement(实现)具体细节的,怎么可以用private把abstract method封锁起来呢? (同理,abstract method前不能加final)。4.public cla
3、ss Something public int addOne(final int x) return +x; 这个比较明显。答案: 错。int x被修饰成final,意味着x不能在addOne method中被修改。5.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;和上面的很相似,都是关于fin
4、al的问题,这有错吗?答案: 正确。在addOne method中,参数o被修饰成final。如果在addOne method里我们修改了o的reference,(比如: o = new Other();),那么如同上例这题也是错的。但这里修改的是o的member vairable(成员变量),而o的reference并没有改变。6.class Something int i; public void doSomething() System.out.println(i = + i); 有什么错呢? 看不出来啊。答案: 正确。输出的是i = 0。int i属於instant variable
5、(实例变量,或叫成员变量)。instant variable有default value。int的default value是0。7.class Something final int i; public void doSomething() System.out.println(i = + i); 和上面一题只有一个地方不同,就是多了一个final。这难道就错了吗?答案: 错。final int i是个final的instant variable (实例变量,或叫成员变量)。final的instant variable没有default value,必须在constructor (构造器)结
6、束之前被赋予一个明确的值。可以修改为final int i = 0;。8.public class Something public static void main(String args) Something s = new Something(); System.out.println(s.doSomething() returns + doSomething(); public String doSomething() return Do something .; 看上去很完美。答案: 错。看上去在main里call doSomething没有什么问题,毕竟两个methods都在同一个
7、class里。但仔细看,main是static的。static method不能直接call non-static methods。可改成System.out.println(s.doSomething() returns + s.doSomething();。同理,static method不能访问non-static instant variable。9.此处,Something类的文件名叫OtherThing.javaclass Something private static void main(String something_to_do) System.out.println(Do
8、 something .); 这个好像很明显。答案: 正确。从来没有人说过Java的Class名字必须和其文件名相同。但public class的名字必须和文件名相同。10.interface A int x = 0; class B int x =1; class C extends B implements A public void pX() System.out.println(x); public static void main(String args) new C().pX(); 答案:错误。在编译时会发生错误(错误描述不同的JVM有不同的信息,意思就是未明确的x调用,两个x都匹
9、配(就象在同时import java.util和java.sql两个包时直接声明Date一样)。对于父类的变量,可以用super.x来明确,而接口的属性默认隐含为 public static final.所以可以通过A.x来明确。11.interface Playable void play();interface Bounceable void play(); interface Rollable extends Playable, Bounceable Ball ball = new Ball(PingPang); class Ball implements Rollable privat
10、e String name; public String getName() return name; public Ball(String name) this.name = name; public void play() ball = new Ball(Football);System.out.println(ball.getName(); 这个错误不容易发现。答案: 错。interface Rollable extends Playable, Bounceable没有问题。interface可继承多个interfaces,所以这里没错。问题出在interface Rollable里的B
11、all ball = new Ball(PingPang);。任何在interface里声明的interface variable (接口变量,也可称成员变量),默认为public static final。也就是说Ball ball = new Ball(PingPang);实际上是public static final Ball ball = new Ball(PingPang);。在Ball类的Play()方法中,ball = new Ball(Football);改变了ball的reference,而这里的ball来自Rollable interface,Rollable interf
12、ace里的ball是public static final的,final的object是不能被改变reference的。因此编译器将在ball = new Ball(Football);这里显示有错。JAVA编程题1现在输入n个数字,以逗号,分开;然后可选择升或者降序排序;按提交键就在另一页面显示按什么排序,结果为,提供resetimport java.util.*;public class bycomma public static String splitStringByComma(String source) if(source=null|source.trim().equals() r
13、eturn null; StringTokenizer commaToker=new tringTokenizer(source,); String result = new StringcommaToker.countTokens(); int i=0; while(commaToker.hasMoreTokens() resulti = commaToker.nextToken();i+;return result;public static void main(String args) String s = splitStringByComma(5,8,7,4,3,9,1); int i
14、i = new ints.length; for(int i = 0;is.length;i+) iii =Integer.parseInt(si); Arrays.sort(ii); /asc for(int i=0;i=0;i-) System.out.println(iii); 2金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥1011)(一千零一拾一元整)输出。package test.format;import java.text.NumberFormat;import java.util.HashMap;public class SimpleMoneyFormat publi
15、c 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 fina
16、l 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
17、= 亿; 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(
18、);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, T
19、WO);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);chineseMoneyPat
20、tern.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() i
21、f (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)
22、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); privat
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java程序员面试宝典-Java代码查错 精品资料 Java 程序员 面试 宝典 代码 查错 精品 资料
限制150内