商超管理系统项目-成长经历

java编程项目遇到的易错知识点:

1.单引号与双引号的区别:
    • 单引号引的数据 是char类型的
    • 双引号引的数据 是String类型的
    • char定义时用单引号,只能有一个字母,数字。char c='c';
    • 而String用双引号,可以是一个,也可能是多个字母,汉字等。就是所谓的字符串。String s="adsaf";char只是一个基本类型,而String 可以是一个类,可以直接引用。
    • 比如char c='c';不能直接对c调用方法。
    • String s="abc";  这时可以调用s.charAt(0);等方法,因为String是类,这是就是对象的调用了
2.比较符号:
Equals比较的是值   ==比较的是地址   compareTo比较的是ASCII码值
         运行下面的程序结果一目了然:
Integer aInteger=new Integer(4);
Integer bInteger=new Integer("4");
System.out.println(aInteger==bInteger);
System.out.println(aInteger.equals(bInteger));
System.out.print(aInteger.compareTo(bInteger));

3.字符串连接:
</pre><pre name="code" class="java">		// 1
		   String a = "abc";  
		   String b ="def";
		   String c = a+b;
		
		// 2
		   StringBuffer a =new StringBuffer("acb");
		   StringBuffer b =new StringBuffer("123");
		   a.append(b);
		
		//3 第三种方法,必须是String 类型
		   String a = "abc";
		   String b ="def";
		   String c = a.concat(b);
</pre><pre name="code" class="html">4.判断字符串为空:
 
package test;

public class T
{

	/**
	 * 判断字符串是空的方法
	 * 以及效率
	 * 运行一次主函数 比较多次
	 * 
	 * 结论:运行一次,第一次比较时 效率由大到小 :function1() < function1() < function1();
	 * 		运行一次,从第二次开始。三个函数效率基本接近,效率排序不唯一!(估计与缓存有关?)
	 * 
	 * @param args
	 */
	
	static String s = "";
	static long n = 1000000000;
	
	public static void main(String[] args)
	{
		//多打印几次,比较结果
		for (int i = 0; i < 10; i++)
		{
			
			System.out.println("--------"+i);
			function1();
			function2();
			function3();
		}
	}
		//函数1
		private static void function1() 
		{
			   long startTime = System.currentTimeMillis(); //开始时间
	
			   for (long i = 0; i< n; i++)
				{
				     if(s == null || s.equals(""));
				}
			   long endTime = System.currentTimeMillis();//结束时间
			   //打印时间差
			   System.out.println("function 1 use time: "+ (endTime - startTime) +"ms");
		}

		//函数2
		private static void function2()
		{
		   long startTime = System.currentTimeMillis();

		   for(long i = 0; i< n; i++)
		   {
		    <span style="white-space:pre">	</span>if(s == null || s.length() <= 0);
		   }
		   
		   long endTime = System.currentTimeMillis();

		   System.out.println("function 2 use time: "+ (endTime - startTime) +"ms");
		}

		
		//函数3
		private static void function3() 
		{
		   long startTime = System.currentTimeMillis();

		   for(long i = 0; i< n; i++)
		   {   
			 s.isEmpty();
		   }
		   
		   long endTime = System.currentTimeMillis();

		   System.out.println("function 3 use time: "+ (endTime - startTime) +"ms");
		}
	
}
5.性能优化:

5.1 字符串的比较:  
String choice = "abc";
 "0".equals(choice)   这种写法第一保证效率,第二防止空指针异常! {相对于这种写法:choice.equals("0") }

5.2 遍历:

for (int i = 0,length = goodsList.size(); i < length; i++)  
for (int i = 0; i <goodsList.size(); i++)  

①相对于②效率更高。原因:②每遍历一次都要计算 goodsList.size()的大小,而①只需要计算一次,放在栈中即可!

6.小技巧:

没学习正则表达式前,这是一个不错的防止用户输入有误而导致程序挂掉的选择。
<strong>	</strong>boolean bool = true;
	 do
	{	
		 System.out.println("\n重新输入或按 0 返回上一级菜单.");
		 String choice = ScannerChoice.ScannerChoString();
			
			if ("0".equals(choice) || "1".equals(choice))
				{
					bool = false;
					int info = Integer.parseInt(choice); //转成int类型
					switch (info)
					{
					case 0:
						mianPage();
						break;
					case 1:
						GoodsPage.addGoodsPage();
						break;
					default:
						break;
					}
				}
			System.err.println("\t!输入有误!");
	}while(bool);


In Java System.out.println() will print to the standard out of the system you are using. On the other hand, System.err.println() will print to the standard error.

If you are using a simple Java console application, both outputs will be the same (the command line or console) but you can reconfigure the streams so that for example, System.out still prints to the console but System.err writes to a file.

8.java判断list为空

if(null == list || list.size() ==0 ){
}

list.isEmpty()和list.size()==0 没有区别

isEmpty()判断有没有元素
而size()返回有几个元素
如果判断一个集合有无元素 
建议用isEmpty()方法.这清晰,简明

list!=null跟!list.isEmpty()有什么区别?

这就相当与,你要喝水,
前面就是判断是不是连水杯都没有,
后面就是判断水杯里面没有水,
连盛水的东西都没有,
这个水从何而来?
所以一般的判断是
if(list!=null && !list.isEmpty()){
这个里面取list中的值
}else{
做其他处理
}
9.关键字instanceof
uinstanceof是Java的一个二元操作符。它的作用是测试它左边的对象是否是它右边的类的实例,返回boolean类型的数据。可以用在 继承中的子类的实例是否为父类的实现
Animal an = new Animal();
Sheep2 sheep = new Sheep2();
if(an instanceof Animal)
{
	System.out.println("an is Animal's instance");
}
if(sheep instanceof Sheep2)
{
	System.out.println("sheep is Sheep2's instance");
}
if(sheep instanceof Animal)
{
	System.out.println("sheep is Animal's instance");
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值