Java基础知识总结.docx
《Java基础知识总结.docx》由会员分享,可在线阅读,更多相关《Java基础知识总结.docx(47页珍藏版)》请在冰豆网上搜索。
Java基础知识总结
Java基础知识总结
JVM
W3CJAVA教程
JAVA考古学
StringBuffer和StringBuilder中的两个函数:
Java代码
1.//int indexOf(String str) :
返回当前StringBuffer对象中,第一个满足str子串的位置。
2.//int indexOf(String str, int fromIndex) :
从fromIndex开始查找,返回第一个满足str子串的位置。
3.
4.StringBuffer sb = new StringBuffer("This is a StringBuffer!
");
5.System.out.println("sb.indexOf(\"is\") = " + sb.indexOf("is")); //2
6.System.out.println("sb.indexOf(\"is\", 4) = " + sb.indexOf("is", 4)); //5
7.System.out.println("sb.indexOf(\"is\", 4) = " + sb.indexOf("is", 7)); // -1
8.
9.//StringBuffer insert(int offset, String str)
10.//在当前StringBuffer对象中插入一个元素,在索引号offset处插入相应的值。
11.
12.StringBuffer sf = new StringBuffer("..{..}) public class ESBYSYSInquiryMachineInfoSrvRequest {");
13.int classIdx = sf.indexOf("public class ");
14.if(classIdx > 0){
15. sf.insert(sf.indexOf("{", classIdx), " implements java.io.Serializable");
16.}
17.System.out.println(sf.toString());
18.//..{..}) public class ESBYSYSInquiryMachineInfoSrvRequest implements java.io.Serializable{
2.保留2位小数:
Java代码
1.import java.text.DecimalFormat;
2.DecimalFormat df=new DecimalFormat("0.00");
3.Double x = 83.3333333333;
4.x=Double.parseDouble(df.format(x));
3.groupby和orderby
Java代码
1.ORDER BY 用于对数据按指定的列和方法排序。
2.
3.select * from syscolumns order by id asc, colorder desc;
4.指示查询出的结果 按 id 正序排列, colOrder 逆序排列。
5.
6.GROUP BY 用于汇总统计。
HAVING 用途和 WHERE类似,但用于对 GROUP BY 的结果进行过滤。
7.select id, count(id) from syscolumns group by id;
8.这条语句以 id 进行汇总,统计出每个不同的 id 出现的个数。
9.
10.select id, count(id) from syscolumns group by id having count
(1) > 10;
11.这条语句以 id 进行汇总,统计出每个不同的 id 出现的个数,但 having 后的条件指定了只显示 count(id) 大于 10 的数据。
。
12.
13.先Group by ,后 Order by
4.日期
获取当前时间:
Java代码
1.1.
2.SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd" + " " + "hh:
mm:
ss");
3.String datetime = tempDate.format(new java.util.Date());
4.
5.2.
6.Calendar now=Calendar.getInstance();
7.String time=now.get(Calendar.YEAR)+"-"+(now.get(Calendar.MONTH)+1)+"-"+now.get(Calendar.DAY_OF_MONTH)+" "+now.get(Calendar.HOUR_OF_DAY)+":
"+now.get(Calendar.MINUTE)+":
"+now.get(Calendar.SECOND);
8.
9.
10.3.Date curDate= new Date(System.currentTimeMillis());
1.SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:
mm:
ss" ); //24小时制
2.SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:
mm:
ss");//12小时制
a.获取年、月、日:
Java代码
1.String year=String.valueOf(c.get(Calendar.YEAR));
2.String month=String.valueOf(c.get(Calendar.MONTH)+1);
3.String day=String.valueOf(c.get(Calendar.DAY_OF_MONTH));
b.Calendar和Date的转化
(1)Calendar转化为Date
Java代码
1.Calendar cal=Calendar.getInstance();
2.Date date=cal.getTime();
(2)Date转化为Calendar
Java代码
1.Date date=new Date();
2.Calendar cal=Calendar.getInstance();
3.cal.setTime(date);
c.计算一年中的第几星期
(1)计算某一天是一年中的第几星期
Java代码
1.Calendar cal=Calendar.getInstance();
2.cal.set(Calendar.YEAR, 2006);
3.cal.set(Calendar.MONTH,1);
4.cal.set(Calendar.DAY_OF_MONTH, 3);
5.int weekno=cal.get(Calendar.WEEK_OF_YEAR);
(2)计算一年中的第几星期是几号
Java代码
1.SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
2.Calendar cal=Calendar.getInstance();
3.cal.set(Calendar.YEAR, 2006);
4.cal.set(Calendar.WEEK_OF_YEAR, 1);
5.cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
6.System.out.println(df.format(cal.getTime()));
7.输出:
8.2006-01-02
d:
1天内(注意为add ):
Java代码
1.Calendar c=Calendar.getInstance();
2.c.add(Calendar.DAY_OF_MONTH, -1); //得到前一天的时间
3.startDateStr=sf.format(c.getTime());
4.
5.endDateStr=sf.format(java.util.Calendar.getInstance().getTime()); 推荐使用这种方法获取当前时间,不推荐使用new Date()
3天内:
Java代码
1.Calendar c=Calendar.getInstance();
2.c.add(Calendar.DAY_OF_MONTH, -3); //得到前3天的时间
3.startDateStr=sf.format(c.getTime());
近1月:
Java代码
1.Calendar c=Calendar.getInstance();
2.c.add(Calendar.MONTH, -1);
3.startDateStr=sf.format(c.getTime());
到当前时间的前几年的时间:
Java代码
1.Calendar c = Calendar.getInstance();
2. c.add(Calendar.YEAR, -5);//得到前5年
3. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:
mm:
ss");
4. String mDateTime=formatter.format(c.getTime());
5. String strStart=mDateTime.substring(0, 16);//2002-10-24 09:
30
本月(注意为set ):
Java代码
1.Calendar c=Calendar.getInstance();
2.c.set(Calendar.DATE,1); //把日期设为当月第一天
3.startDateStr=sf.format(c.getTime());
上月:
Java代码
1.Calendar c2=Calendar.getInstance();
2. c2.add(Calendar.MONTH,-1);
3. c2.set(Calendar.DATE,1);
4. startDateStr=sf.format(c2.getTime());
5.文本域input的不可编辑属性disabled和 readonly 区别
都可以实现都可以实现input的不可编辑,但有区别...
disable属性--表示已经失效的输入域
readonly属性--表示只读(只能看到,不能修改)的输入域(框)
Java代码
1.
2.
3.
Java代码
1. /**是否闰年
2. */
3.public boolean isLeapYear(int year) {
4. return (year % 4 == 0 && year % 100 !
= 0) || (year % 400 == 0); }
具体日期操作:
可将选中的代码拖动到QQ聊天窗口中复制
6.File协议
表示本地文件传输协议,File协议主要用于访问本地计算机中的文件 ,就如同在Windows资源管理器中打开文件一样。
应用:
要使用File协议,基本的格式如下:
file:
///文件路径,比如要打开F:
盘flash文件夹中的1.swf文件,那么可以在资源管理器或IE地址栏中键入:
file:
///f:
/flash/1.swf并回车。
3个斜杠代表本地
8.substring(int beginIndex,int endIndex)
该子字符串从指定的beginIndex处开始,直到索引endIndex-1处的字符。
因此,该子字符串的长度为endIndex-beginIndex。
示例:
"hamburger".substring(4,8)returns"urge"
"smiles".substring(1,5)returns"mile "
参数:
beginIndex-起始索引(包括)。
endIndex- 结束索引(不包括) 。
9.什么时候用Thread.getContextClassLoader()
需要动态加载很多类和资源的时候 .
通常当你需要动态加载资源的时候 , 你至少有三个 ClassLoader 可以选择 :
✧∙∙∙∙∙∙∙系统类加载器或叫作应用类加载器 (systemclassloaderorapplicationclassloader)
✧∙∙∙∙∙∙∙当前类加载器
✧∙∙∙∙∙∙∙当前线程类加载器
10.instanceof运算符
指出对象是否是特定类的一个实例。
Java代码
1.String s = "I AM an Object!
";
2.boolean isObject = s instanceof Object;
3.
4.public double calculate(Bill bill) {
5. if (bill instanceof PhoneBill) {
6. //计算电话账单
7. }
8.}
11、java环境配置(都放下面):
CLASSPATH .;%JAVA_HOME%\lib
JAVA_HOME C:
\ProgramFiles\Java\jdk1.6.0_22
Path .;%JAVA_HOME%\bin
13.JDK1.5的新特性
“JDK1.5”(开发代号猛虎)的一个重要主题就是通过新增一些特性来简化开发 ,这些特性包括泛型,for-each循环,自动装包/拆包,枚举,可变参数, 静态导入 。
使用这些特性有助于我们编写更加清晰,精悍,安全的代码。
自动装包/拆包
自动装包/拆包大大方便了基本类型数据和它们包装类地使用。
自动装包:
基本类型自动转为包装类.(int >> Integer)
自动拆包:
包装类自动转为基本类型.(Integer >> int)
在JDK1.5之前,我们总是对集合不能存放基本类型而耿耿于怀,现在自动转换机制解决了我们的问题。
在实际开发过程中,我们经常会遇到需要使用对象,而不是内置数据类型的情形。
为了解决这个问题,Java语言为每一个内置数据类型提供了对应的包装类。
所有的包装类(Integer、Long、Byte、Double、Float、Short)都是抽象类Number的子类。
这里Integer先自动转换为int进行加法运算,然后int再次转换为Integer.
Java代码
1.int a = 3;
2.Collection c = new ArrayList();
3.c.add(a);//自动转换成Integer.
4.
5.Integer b = new Integer
(2);
6.c.add(b + 2);
枚举:
Java代码
1.public enum Color
2.{
3. Red,
4. White,
5. Blue
6.}
然后可以这样来使用Color myColor = Color.Red.
枚举类型还提供了两个有用的静态方法values()和valueOf(). 我们可以很方便地使用它们,例如
Java代码
1.for (Color c :
Color.values())
2. System.out.println(c);
带构造函数的枚举:
Java代码
1.public enum Color {
2.
3. RED("红色"),BLUE("蓝色"),GREEN("绿色");
4.
5. private final String name;
6.
7. public String getName() {
8. return name;
9. }
10. private Color(String name){
11. this.name=name;
12. }
13.}
Java代码
1.System.out.println("Color.RED.name():
"+Color.RED.name()); //RED
2.System.out.println("Color.RED.toString():
"+Color.RED.toString()); //RED
3.System.out.println(Color.RED.getName()); //红色
Color.RED.name()
Color.RED.toString();得到的都是RED
1、Color枚举类是特殊的class,其枚举值(RED,BLUE...)是Color的类对象(类实例):
Colorc=Color.RED ;
而且这些枚举值都是publicstaticfinal的,也就是我们经常所定义的常量,因此枚举类中的枚举值最好全部大写 。
2、即然枚举类是class,当然在枚举类型中有构造器,方法和数据域。
但是,枚举类的构造器有很大的不同:
(1)构造函数只是在构造枚举值的时候被调用。
(2) 枚举构造函数只能私有private ,绝对不允许有public构造器。
这样可以保证外部代码无法新构造枚举类的实例。
因为我们知道枚举值是publicstaticfinal的常量而已但枚举类的方法和数据域可以允许外部访问。
Java代码
1./**
2. *
3. * 服务器类型
4. *
5. */
6.public enum ServerType {
7.
8. JBoss("server/default/deploy","client,common"), WebLogic("",""), GlassFish("","");
9.
10. private String deployPath;
11.
12. private String libClassPath;
13.
14. private ServerType(String deployPath, String libClassPath){
15. this.deployPath = deployPath;
16. this.libClassPath = libClassPath;
17. }
18.
19. public String getDeployPath(){
20. return this.deployPath;
21. }
22.
23. public String getLibClassPath(){
24. return this.libClassPath;
25. }
26.}
ServerTypeserverType=ServerType.JBoss;
StringdeployPath=serverType.getDeployPath();
StringlibClassPath=serverType.getLibClassPath();
可变参数:
当不能确定一个方法的入口参数的个数时,以往版本的Java中,通常的做法是将多个参数放在一个数组或者对象集合中作为参数来传递,1.5版本以前的写法是:
Java代码
1.int sum(Integer[] numbers)
2.{
3. int nSum = 0;
4. for(int i:
numbers)
5. nSum += i;
6. return nSum;
7.}
在别处调用该方法
sum(newInteger[]{12,13,20});
而在1.5版本中可以写为:
Java代码
1.public static int sum(Integer... number){
2. int nSum=0;
3. for(int i :
number){
4. nSum+=i;
5. }
6. return nSum;
7.}
在别处调用该方法
Java代码
1.System.out.println("sum():
"+sum(12,13,14));
2.System.out.println("sum():
"+sum(12,13));
静态导入:
要使用静态成员(方法和变量)我们必须给出提供