Debuger

Great questions

These questions will solve most bugs:

what method shows the symptom ? what lines of code produces that symptom ?

what is the state of the receiver object in that code ? what were the param values passed in ?

if it’s an exception, what does the exception error message say – null pointer? array access? Somethimes the exception name can be very informative.

调优方法

Eclipse debugger , 好用

println()

注释掉部分代码

Truths of Debugging

  • 直觉很重要, 你可以测试你直觉的想法, 但当直觉与实际发生碰撞时, 实际发生获胜.
  • 简单的代码也会引起重大的bug, 不要忽视那些简单的代码, 往往是它们出现问题.
  • 注意你自己定义的变量, 程序中出现的bug, 往往是你定义的变量不是你想要的值.
  • 如果你的程序1分钟前还可以正常执行, 但现在不行了, 那么你上次改动过什么? 注意: 如果你每写50行code就测试一下, 那么当程序出问题时, 你就 知道是哪50行出现的问题.
  • 不要随便修改代码来追踪bug, 这样有可能带来新的bug.
  • 如果你发现一些错误跟你一直追踪的错误没有关系, 那么先来将这些错误搞定吧, 没准这些错误与你一直追踪的bug是有关系的, 只是你还没想到.

Java 通用性(Generics)

  • Using a generic class, like using ArrayList<String>
  • Writing generic code with a simple <T> or <?> type parameter
  • Writing generic code with a <T extends Foo> type parameter

Use Generic Class

  1. ArrayList<String> strings = new ArrayList<String>();
  2. strings.add("hi");
  3. strings.add("there");
  4. String s = strings.get(0);

循环使用

  1. List<String> strings = ...
  2. for (String s: strings) {
  3. System.out.println(s);
  4. }

例子:

  1. public static void dempList() {
  2. List<String> a = new ArrayList<String>();
  3. a.add("Don't");
  4. a.add("blame");
  5. a.add("me");
  6.  
  7. for (String str: a) {
  8. System.out.println(str);
  9. }
  10.  
  11. Iterator<String> it = a.iterator();
  12. while (it.hasNext()) {
  13. String string = it.next();
  14. System.out.println(String);
  15. }
  16. List<Integer> ints = new ArrayList<Integer>();
  17. for (int i = 0; i<10; i++) {
  18. ints.add(new Integer(i * i));
  19. }
  20. int sum = ints.get(0).intValue() + ints.get(1).intValue();
  21.  
  22. sum = ints.get(0) + ints.get(1);
  23.  
  24. // Generic Map Example Code
  25. public static void demoMap() {
  26. HashMap<Integer, String>map = new HashMap<Integer, String>();
  27. map.put(new Integer(1), "one");
  28. map.put(new Integer(2), "two");
  29. map.put(3, "three"); // 自动包装
  30. map.put(4, "four");
  31.  
  32. String s = nap.get(new Integer(3));
  33. s = map.get(3) // 自动包装
  34.  
  35. HashMap<String, List<Integer>> counts = new HashMap<String, List<Integer>>();
  36. List<Integer> evens = new ArrayList<Integer>();
  37. evens.add(2);
  38. evens.add(4);
  39. evens.add(6);
  40. counts.put("even", evens);
  41.  
  42. List<Integer> evens2 = counts.get("evens");

Define a Generic<T> Class/Method

you can define your own class as a generic class. the class definithion code is parameterized by a type, typically called<T>. This is more or less what ArrayList does. At the very start of the class, the parameter is added like this: public calss Foo<T>

这个 T 有以下限制: (T 的本质就好比是一个普通的类型)

- declare variables, parameters, and return types of the type T

- use = on T pointers

- call methods that work on all Objects, like .equals()

记住: where you see “T”, it is just replaced by “Object” to produce the code for runtime. So the ArrayList<String>code and the ArrayList<Integer>code … those two are actually just the ArrayList<Object>code at runtime.

例子:

  1. public class Pair<T> {
  2. private T a;
  3. private T b;
  4. private List<T> unused;
  5.  
  6. public Pair(T a, T b) {
  7. this. a = a;
  8. this.b = b;
  9. }
  10. public T getA() {
  11. return a;
  12. }
  13. public T getB() {
  14. return b;
  15. }
  16. public void swap() {
  17. T temp = a;
  18. a = b;
  19. b = temp;
  20. }
  21.  
  22. public boolean isSame() {
  23. return a.equals(b);
  24. }
  25.  
  26. public boolean contains(T elem) {
  27. return (a.equals(elem) || b.equals(elem));
  28. }
  29. public static void main(String[] args) {
  30.  
  31. // integer 类型的可以
  32. Pair<Integer> ipair = new Pair<Integer>(1, 2);
  33. Integer a = ipair.getA();
  34. int b = ipair.getB(); // 没包装
  35.  
  36. // String 类型的也可以
  37. Pair<String> spair = new Pair<String>("hi", "there");
  38. String s = spair.getA();
  39. }

Generic <T> Method

与整个类都使用通用来说, 可以针对某个方法来使用通用, 语法: public <T> void foo(List<T> list)

例如:

  1. public static <T> void removeAdjacent(Collection<T> coll) {
  2. Iterator<T> it = coll.iterator();
  3. T last = null;
  4. while (it.hasNext()) {
  5. T curr = it.next();
  6. if (curr == las) it.remove();
  7. last = curr;
  8. }
  9. }

<T> Method – use a <T> type on the method to identify what type of element is in the collection. The <T> goes just before the return type. T can be used to decalre variables, return types, etc. This is ok, but slightly heavyweight, since in this case we actually don’t care what type of thing is in there. This removes elements that are == to an adjacent element.

?/T with “extends” Generics

extends 可以限制 T, 例如 with a <T extends Number> – for any T value , we can assume it is a Number subclass, so .intValue()

例如:

  1. public class PairNumber <T extends Number> {
  2. private T a;
  3. private T b;
  4.  
  5. public PairNumber( T a, T b) {
  6. this.a = a;
  7. this.b = b;
  8. }
  9. public int sum() {
  10. return (a.intValue() + b.intValue());
  11. }

?/T Extends Method

  1. public static int sumAll(Collection<? extends Number> nums) {
  2. int sum = 0;
  3. for (Number num : nums) {
  4. sum += num.intValue(0;
  5. }
  6. return sum;
  7. }

cs108 03 ( 调试, java通用性)的更多相关文章

  1. Eclipse快速入门:远程调试Java应用

    Eclipse快速入门:远程调试Java应用 2012年03月27日00:00 it168网站原创 作者:皮丽华 编辑:皮丽华 我要评论(0) 标签: Eclipse , Java , Java框架, ...

  2. 远程debug调试java代码

    远程debug调试java代码 日常环境和预发环境遇到问题时,可以用远程调试的方法本地打断点,在本地调试.生产环境由于网络隔离和系统稳定性考虑,不能进行远程代码调试. 整体过程是通过修改远程服务JAV ...

  3. [原创] 如何用Eclispe调试java -jar xxx.jar 方式执行的jar包

    有时候,我们经常会需要调试 java -jar xxx.jar方式运行的代码,而不是必须在Eclipse中用Debug或者Run的方式运行.比如我们拿到的SourceCode不完整.Java提供了一种 ...

  4. paip. java resin 远程 调试 java resin remote debug

    paip. java resin 远程 调试 java resin remote debug 作者Attilax  艾龙,  EMAIL:1466519819@qq.com 来源:attilax的专栏 ...

  5. 使用JDB调试Java程序

    Java程序中有逻辑错误,就需要使用JDB来进行调试了.调试程序在IDE中很方便了,比如这篇博客介绍了在Intellj IDEA中调试Java程序的方法. 我们课程内容推荐在Linux环境下学习,有同 ...

  6. 如何在Eclipse中Debug调试Java代码

    背景 有的时候你想debug调试Java的源代码,就想试图在Java源代码中设置断点,在Eclipse中常常会出现Unable to insert breakpoint Absent Line Num ...

  7. 像调试java一样来调试Redis lua

    高并发的系统中,redis的使用是非常频繁的,而lua脚本则更是锦上添花.因为lua脚本本身执行的时候是一个事务性的操作,不会掺杂其他外部的命令,所以很多关键的系统节点都会用redis+lua来实现一 ...

  8. 【转】如何用Eclispe调试java -jar xxx.jar 方式执行的jar包

    原文地址:https://www.cnblogs.com/zzpbuaa/p/5443269.html 有时候,我们经常会需要调试 java -jar xxx.jar方式运行的代码,而不是必须在Ecl ...

  9. Eclipse调试Java的10个技巧【转】

    clipse调试Java的10个技巧 先提三点 不要使用System.out.println作为调试工具 启用所有组件的详细的日志记录级别 使用一个日志分析器来阅读日志 1.条件断点 想象一下我们平时 ...

随机推荐

  1. 使用js的indexOf,lastIndexOf,slice三函数轻易得到url的服务器,路径和页名

    js的indexOf,lastIndexOf,slice能帮我们在js字符串处理时少走一些弯路. 程序如下: var url="http://www.cnblogs.com/xiandeda ...

  2. (剑指Offer)面试题56:链表中环的入口结点

    题目: 一个链表中包含环,请找出该链表的环的入口结点. 思路: 1.哈希表 遍历整个链表,并将链表结点存入哈希表中(这里我们使用容器set),如果遍历到某个链表结点已经在set中,那么该点即为环的入口 ...

  3. Python 面向对象编程 继承 和多态

    Python 面向对象编程 继承 和多态 一:多继承性 对于java我们熟悉的是一个类只能继承一个父类:但是对于C++ 一个子类可以有多个父亲,同样对于 Python一个类也可以有多个父亲 格式: c ...

  4. jQuery特性

      CreateTime--2017年7月17日11:45:27Author:Marydon jQuery特性 1.jQuery取值 使用jQuery进行页面取值时,获取到的值的类型有两种:strin ...

  5. 机器学习数学基础- gradient descent算法(上)

    为什么要了解点数学基础 学习大数据分布式计算时多少会涉及到机器学习的算法,所以理解一些机器学习基础,有助于理解大数据分布式计算系统(比如spark)的设计.机器学习中一个常见的就是gradient d ...

  6. spring spel表达式语言

    一.通过bean的id对bean进行引用 1.对其他bean的引用 <property name="dept" value="#{dept}"/> ...

  7. Android开发之Shortcuts, LiveFolder, Widget

    2013-07-05 桌面组件包括:快捷方式(Shortcuts),实时文件夹(Live Folder),桌面插件(Widget).   快捷方式用于启动应用程序的某个组件,例如Activity, S ...

  8. 使用万网+阿里云服务器(Apache)配置二级域名

    1. 在万网域名解析中添加二级域名解析配置    在下面的主机记录(RR)的栏位添加二级域名名称后点确定按钮即可.     注:需要等一段时间才能生效,请留意万网域名解析列表的生效状态栏位. 2. 在 ...

  9. PHP安全细节(转)

    一个常见的PHP安全细则 发布时间:2012-01-09 10:18:50   来源:51cto   评论:0 点击:9 次 [字号:大 中 小] PHP本身再老版本有一些问题,比如在 php4.3. ...

  10. Mybatis自动生成的配置实例

    一.目录 按照图片准备下面的东西吧,基础jar,数据链接库的jar. 二.generatorConfig.xml <?xml version="1.0" encoding=& ...