1.Eclipse关联jdk源代码的方法
1).右键项目,依次选择 构建路径->配置构建路径->Java构建路径->库->JRE系统库->rt.jar->源代码链接。编辑“源代码链接”,设置“外部文件”的路径为JDK安装路径下的src.zip。如图
2).String类的equals()方法及其实现方法,如图
3).equals方法和==的区别
==用来比较地址,例如java
public static void main(String[] args) { String str1 = "hello"; String str2 = new String("hello"); System.out.println("str1 == str2 --> " + (str1 == str2)); }
equals方法能够直接比较内容,例如git
public static void main(String[] args) { String str1 = "hello"; String str2 = new String("hello"); System.out.println("str1 equals str2 --> " + (str1.equals(str2))); }
2.构造方法:就是他的名称与类名同样的方法
构造方法的重载:方法名同样,但参数个数或参数类型不一样
下面的程序是否能够经过编译?为何?数组
public class Test { public static void main(String args[]) { Foo obj = new Foo(); } } class Foo{ int value; public Foo(int intValue){ value = intValue; } }
不能编译,没有定义无参的构造函数,不能调用。改为app
public class Test { public static void main(String args[]) { Foo obj = new Foo(); } } class Foo{ int value; public Foo(){ } public Foo(int intValue){ value = intValue; } }
3.运行下列程序,结果是什么?查阅资料,分析为何。函数
public class Test { public static void main(String args[]) { double a = 0.1; double b = 0.1; double c = 0.1; if((a + b + c) == 0.3){ System.out.println("等于0.3"); }else { System.out.println("不等于0.3"); } } }
结果为不等于0.3,精度损失。修改以下性能
import java.math.BigDecimal; public class Test { public static void main(String args[]) { double a = 0.1; double b = 0.1; double c = 0.1; double n=MyMath.add(a, b); if(MyMath.add(n,c) == 0.3){ System.out.println("等于0.3"); }else { System.out.println("不等于0.3"); } } } class MyMath{ public static double add(double d1,double d2){ BigDecimal b1=new BigDecimal(d1); BigDecimal b2=new BigDecimal(d2); return b1.add(b2).doubleValue(); } }
4.运行下列程序,结果是什么?分析缘由,应如何修改.学习
public class Test { public static void main(String[] args) { MyClass[] arr=new MyClass[3]; arr[1].value=100; } } class MyClass{ public int value=1; }
结果测试
缘由:数组越界了,没有实例化。
修改以下.net
public class Test { public static void main(String[] args) { MyClass[] arr=new MyClass[3]; for(int i=0;i<3;i++){ arr[i]=new MyClass(); } arr[1].value=100; } } class MyClass{ public int value=1; }
5.在一个10000次的循环中,须要进行字符串的链接操做,那么,应该使用String类仍是StringBuffer类,为何?性能有差别吗?可否写出测试代码证实你的结论。(可查阅资料)
用StringBuffer类
缘由:String类要一个一个的连接,而StringBuffer类能够直接调用append()方法。
性能:StringBuffer类比String类效率高
例如设计
public class Test { public static void main(String[] args){ String str = "Hello"; String b=fun(str); System.out.println(b); } public static String fun(String s){ for(int i=0;i<10000;i++){ a=s+" World"; } return a; } } public class Test { public static void main(String[] args){ StringBuffer buf = new StringBuffer("Hello"); fun(buf); System.out.println(buf); } public static void fun(StringBuffer s){ for(int i=0;i<10000;i++){ s.append(" World"); } } }
1.评分系统
实验问题分析:
问题1:数组越界了
实验问题分析:
问题1:判断是否以com|cn|net|gov|edu|org结尾用的是if嵌套
缘由:应该是或者关系
3.字符串统计
4.身份证识别
缘由:由于你的return值是在循环的if条件里面,也就是if条件成立的时候返回了String
对象,可是若是if条件不成立的话,就没有返回值了。
解决方案:加上return null;
代码行数(新增/累积) | 学习时间(新增/累积) | 本周学习内容 | |
---|---|---|---|
目标 | 5000行 | 300小时 | |
第2-4周 | 100/100 | 20/20 | 学习了数组和方法 |
第5周 | 200/300 | 30/50 | 学习了String类和StringBuffer类 |
第6周 |