java常范的几种错误

发布时间:2020-06-16 09:48:50 作者:Leah
来源:亿速云 阅读:109

这篇文章将为大家详细讲解有关java常范的几种错误,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

1、空指针错误

在java数组的使用中,有时候需要对字符串数组中的元素进行对比。那么当元素不为null时,程序会正常运行;然而,一旦对比的元素为null,那么程序就会出现空指针错误。

解决方法:加入保护,当元素不为null时在进行判断。

public static void main(Sring[] args){
  String [] sums = "adfafA";
  for(int i=0;i<sums.length;i++){
    if(sums[]!=null && sums[i].equals(sr)){
      System.out.println("找到元素"+sums[i]);
      break;
    }
  }
}

2、数据的精度范围与类型转换

在Java中有4种基础类型数据:

整型:  -1 2 3 4 ……

浮点型:float 1.3f ;  double 3.2   3.5d (未标明类型,默认最高精度)

字符型:采用Unicode编码,2字节

布尔型:true、false

四种基础类型数据的精度范围各不相同,逐级提升。

如果在使用数据类型时,不注意精度范围,就会出现数据溢出,从而发生计算错误。

四种基础类型中,整型,浮点型和字符型数据是可以相互转化的。

转换规则如下:

①隐形转换:精度小的数据值,赋值给精度大的值,可以自动转换。(低级向高级)

②强制转换:范围大的数据值,(高级到低级)

③运算自动提升规则:运算中,自动转换为精度高的数据类型。

3、三种循环的使用

①for 循环:是一种有限次数的循环,在循环前,规定好循环次数。

for(表达式;表达式;表达式){
        循环体
 }

break语句:执行后,立即跳出循环,不在执行后面的语句,且结束所有循环。

continue 语句:执行后,立即跳出当前单词循环,重新判断条件是否继续循。

②While循环语句:适合未知次数的循环。

while(条件表达式){
  语句
 };

③do-While循环:也是未知次数的循环。单至少要先执行一次do,在判断。如果while成立,继续循环,反之,结束循环。

do {
 语句
 }while(条件表达式);

4、多次拷贝字符串

测试所不能发现的一个错误是生成不可变(immutable)对象的多份拷贝。不可变对象是不可改变的,因此不需要拷贝它。最常用的不可变对象是String。

如果你必须改变一个String对象的内容,你应该使用StringBuffer。下面的代码会正常工作:

String s = new String ("Text here");

但是,这段代码性能差,而且没有必要这么复杂。你还可以用以下的方式来重写上面的代码:

String temp = "Text here";
String s = new String (temp);

但是这段代码包含额外的String,并非完全必要。更好的代码为:

String s = "Text here";

5、没有克隆(clone)返回的对象

封装(encapsulation)是面向对象编程的重要概念。不幸的是,Java为不小心打破封装提供了方便——Java允许返回私有数据的引用(reference)。下面的代码揭示了这一点:

import java.awt.Dimension;
  /***Example class.The x and y values should never*be negative.*/
  public class Example{
  private Dimension d = new Dimension (0, 0);
  public Example (){ }
  /*** Set height and width. Both height and width must be nonnegative * or an exception is thrown.*/
  public synchronized void setValues (int height,int width) throws IllegalArgumentException{
  if (height <0 || width <0)
  throw new IllegalArgumentException();
  d.height = height;
  d.width = width;
  }
  public synchronized Dimension getValues(){
  // Ooops! Breaks encapsulation
  return d;
  }
  }

Example类保证了它所存储的height和width值永远非负数,试图使用setValues()方法来设置负值会触发异常。不幸的是,由于getValues()返回d的引用,而不是d的拷贝,你可以编写如下的破坏性代码:

  Example ex = new Example();
  Dimension d = ex.getValues();
  d.height = -5;
  d.width = -10;

现在,Example对象拥有负值了!如果getValues() 的调用者永远也不设置返回的Dimension对象的width 和height值,那么仅凭测试是不可能检测到这类的错误。

不幸的是,随着时间的推移,客户代码可能会改变返回的Dimension对象的值,这个时候,追寻错误的根源是件枯燥且费时的事情,尤其是在多线程环境中。

更好的方式是让getValues()返回拷贝:

  public synchronized Dimension getValues(){
  return new Dimension (d.x, d.y);
  }

6、拷贝错误的数据

有时候程序员知道必须返回一个拷贝,但是却不小心拷贝了错误的数据。由于仅仅做了部分的数据拷贝工作,下面的代码与程序员的意图有偏差:

import java.awt.Dimension;
  /*** Example class. The height and width values should never * be
  negative. */
  public class Example{
  static final public int TOTAL_VALUES = 10;
  private Dimension[] d = new Dimension[TOTAL_VALUES];
  public Example (){ }
  /*** Set height and width. Both height and width must be nonnegative * or an exception will be thrown. */
  public synchronized void setValues (int index, int height, int width) throws IllegalArgumentException{
  if (height <0 || width <0)
  throw new IllegalArgumentException();
  if (d[index] == null)
  d[index] = new Dimension();
  d[index].height = height;
  d[index].width = width;
  }
  public synchronized Dimension[] getValues()
  throws CloneNotSupportedException{
  return (Dimension[])d.clone();
  }
  }

这儿的问题在于getValues()方法仅仅克隆了数组,而没有克隆数组中包含的Dimension对象,因此,虽然调用者无法改变内部的数组使其元素指向不同的Dimension对象,但是调用者却可以改变内部的数组元素(也就是Dimension对象)的内容。方法getValues()的更好版本为:

 public synchronized Dimension[] getValues() throws CloneNotSupportedException{
  Dimension[] copy = (Dimension[])d.clone();
  for (int i = 0; i
  // NOTE: Dimension isn’t cloneable.
  if (d != null)
  copy[i] = new Dimension (d[i].height, d[i].width);
  }
  return copy;
  }

在克隆原子类型数据的多维数组的时候,也会犯类似的错误。原子类型包括int,float等。简单的克隆int型的一维数组是正确的,如下所示:

 public void store (int[] data) throws CloneNotSupportedException{
  this.data = (int[])data.clone();
  // OK
  }

拷贝int型的二维数组更复杂些。Java没有int型的二维数组,因此一个int型的二维数组实际上是一个这样的一维数组:它的类型为int[]。简单的克隆int[][]型的数组会犯与上面例子中getValues()方法第一版本同样的错误,因此应该避免这么做。下面的例子演示了在克隆int型二维数组时错误的和正确的做法:

public void wrongStore (int[][] data) throws CloneNotSupportedException{
  this.data = (int[][])data.clone(); // Not OK!
  }
  public void rightStore (int[][] data){
  // OK!
  this.data = (int[][])data.clone();
  for (int i = 0; i
  if (data != null)
  this.data[i] = (int[])data[i].clone();
  }
  }

关于java常范的几种错误就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. 中端存储里的“高端范”
  2. storcli/percli常⽤场景

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java ava

上一篇:Layui和Django实现后台布局

下一篇:java如何判断对象是否为null

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》