java加强课程的一些总结!2月18日!

本文深入探讨了Java API的概念及其使用方式,对比了Vector与ArrayList的特点,介绍了字符串不可变性的原理及几种字符转整型的方法,并提供了实用的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如何理解 JAVA API

     JAVA  API JDK提供给编程人员的接口,编程人员可以调用这些接口实现所需要的功能,就像WINDOWS的接口一样,只需操作系统所提供的函数,就可以实现预想的功能,而不必知道这个功能是如何实现的。

2  如果继续打开以前的一个工程,应该打开工程主目录下的那个文件:

     应该打开以jcw为后缀的那个文件。

3  String类型的对象一旦生成就不能修改,那么为什么还提供replace    toUpperCase方法呢?

    String类型的对象一旦生成就不能修改,如 String  str1= new String(“prince”),则在一块内存地址中就存放了prince这一内容,str1指向这个地址,无法对其修改,然后我们写上 String str2 =str1.replace(‘p’,’P’),此时就在另一块内存中存放了Prince这个内容,str2指向它,我们还可以写String str3=str1.toUpperCase,此时在另一块内存中存放了PRINCE的内容,str3指向它。也就是说,replacetoUpperCase两个方法返回的都是一个新的对象,而没有对原来的对象修改,因此提供这两种方法和“String类型的对象一旦生成就不能修改是不矛盾的。

4  JDKInteger的方法中,提供了那三中方法将一个字符型转化成整型?

      这三种方法分别是decode(String str)parseInt(String str)valueof(String str);

下面的程序输出的都是整数

   

public class Test {

        public static void main(String[] args) {

              // TODO: Add your code here

              String intStr= new String("125");

             

              System.out.println(Integer.decode(intStr));

              System.out.println(Integer.valueOf(intStr));

              System.out.println(Integer.parseInt(intStr));

         } 

}

5  VectorArrayList的区别?在什么情况下用Vector?什么情况下用ArrayList?

   Vector能够提供线程的同步访问,能保证多个线程对Vector的正确访问,而ArrayList不能保证这种同步的正确访问,所以在多线程操作时应该使用Vector,单线程用ArrayList就可以。另外,当元素的个数超过了本身的容量时,Vector会将其容量增加一倍,而ArrayList只增加50%

编写一个能用Hashtable类的代码,包含String nameint age 两个成员变量,并编写验证该关键字类是否正确的测试代码

//MyKey.java

public class MyKey {

public String name = null;

public int age = 0;  

public MyKey(String name,int age)

{

        this.name=name;

        this.age=age;

}

public boolean equals(Object obj)

{

        // TODO: 在这添加你的代码

        if(obj instanceof MyKey)

        {

               MyKey keyTemp=(MyKey)obj;

               if(name.equals(keyTemp.name)&& age==keyTemp.age)

               {

                      return true;

               } 

               else

               {

                      return false;

               }

        }

        else

        {

               return false;

        }

}

       public int hashCode()

{

        // TODO: 在这添加你的代码

       

        return name.hashCode()+age;

}

 

public String toString()

{

        // TODO: 在这添加你的代码

        return name+","+age;

}    

}

 

//TestHashtable.java

import java.util.*;

public class TestHashtable {

public static void main(String[] args)

{

        // TODO: 在这添加你的代码

       

        Hashtable test=new Hashtable();

       

        test.put(new MyKey("zhangsan",18),new Integer(1));

        test.put(new MyKey("lisi",19),new Integer(2));

        test.put(new MyKey("wangwu",20),new Integer(3));

       

        Enumeration e =test.keys();

       

        while(e.hasMoreElements())

        {

               MyKey key= (MyKey)e.nextElement();

               System.out.print(key.toString()+"=");

               System.out.println(test.get(key));

        }

       

        System.out.println(test.get(new MyKey("zhangsan",18)));

}    

}

7 编写打印出当前虚拟机的所有系统属性的程序,并在启动这个程序是,为java虚拟机增加一个系统属性

import java.util.*;

public class TestSysPro {

public static void main(String[] args) {

        // TODO: Add your code here

                      Properties sp= System.getProperties();

       

        Enumeration e=sp.propertyNames();

       

        while(e.hasMoreElements())

        {

               String str=(String)e.nextElement();

               System.out.println(str+"="+sp.getProperty(str));

        }

        try

        {

               Process p=Runtime.getRuntime().exec("notepad.exe TestSysPro.java");

            Thread.sleep(5000);

               p.destroy();

        }

        catch(Exception ec)

        {

           System.out.println(ec.toString());  

        }

       

}    

}

cmd窗口中键入 java -DXIANG=PRINCE TONG=PRINCESS TestSysPro

  此处增加了两个系统属性 XIANG TONG 值分别是 PRINCE PRINCESS

8 为什么Runtime类被设计成不能在程序中直接创建它的实例程序对象?Java设计之又是通过什么样的方式来保证在程序中又能有一个Runtime实例对象呢?

   一个Runtime对象对应一个虚拟机,一个程序起来以后,就启动了一个虚拟机,如果可以用new创建多个Runtime对象,那么这多个Runtime对应那个虚拟机呢?而我们的运行程序只对应一个虚拟机。设计者是通过私有的构造函数的来保证在程序中只有一个Runtime实例化对象

 以下几段转自  http://blog.sina.com.cn/s/blog_3fc21997010006o3.html

   Runtime不能够用new来创建实例,是因为Runtime作为一个类不提供公有的构造函数,而是采用私有的构造函数强化其不可在用户程序中实例化的能力。Runtime是和java当前的应用程序相关联的,应该只有一个实例,这个实例是在应用程序运行初自动创建的。要使用这个实例,可以用Runtime.getRuntime()方法来获得这个实例的引用。

   
做一个测试:
    Runtime rt1 = Runtime.getRuntime();
    Runtime rt2 = Runtime.getRuntime();
    System.out.println((rt1 == rt2));
   
返回的是true,这说明用Runtime.getRuntime()返回的是同一个实例的引用。

9 修改前面讲解的TimerTimerTask类的例子程序代码,让该程序启动Windows自带的计算器程序后立即结束

在此程序中是让计算器启动1秒后结束

import java.util.*;

public class TestCalendar {

public static void main(String[] args) {

        // TODO: Add your code here

        Calendar cal = Calendar.getInstance();

       

        System.out.println(cal.get(Calendar.YEAR)+" "+cal.get(Calendar.MONTH)+" "+cal.get(Calendar.DAY_OF_MONTH)+" "+cal.get(Calendar.HOUR)+":"+cal.get(Calendar.MINUTE)+":"+cal.get(Calendar.SECOND));

    cal.add(Calendar.DAY_OF_MONTH,365);

    System.out.println(cal.get(Calendar.YEAR)+" "+cal.get(Calendar.MONTH)+" "+cal.get(Calendar.DAY_OF_MONTH)+" "+cal.get(Calendar.HOUR)+":"+cal.get(Calendar.MINUTE)+":"+cal.get(Calendar.SECOND));

 

    class MyTimerTask extends TimerTask{

          private Timer tm=null;

          public MyTimerTask(Timer tm)

          {

                 this.tm=tm;

          }

          public void run()

          {

                  Process p=null;

                 try

                 {

                     p=Runtime.getRuntime().exec("calc.exe ");

                

                 }

                 catch(Exception e)

                 {

                        e.printStackTrace();

                 }

                 tm.cancel();

                 try

                 {

                        Thread.sleep(1000);  //启动1秒后就结束

                        p.destroy();

                 }

                 catch(Exception e)

                 {

                        System.out.println(e.toString());

                 }

                

          }

         

     }

    

     Timer tm=new Timer();

     

     tm.schedule(new MyTimerTask(tm),3000);

    

}    

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值