用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字

柯侧耳倾听者    -  云代码空间

—— 翱翔在Java世界的海洋之上

Java基础类库1

2017-12-22|795阅||

摘要:   Java基础类库1(2学时) 一、实验目的与要求 1. 使用Scanner获取键盘输入 2. System类 3. Runtime类 4. Object类 二、实验原理 Oracle为Java提供了丰富的基础类库,Java 8提供了

   Java基础类库12学时)

一、实验目的与要求

1. 使用Scanner获取键盘输入

2. System

3. Runtime

4. Object

二、实验原理

OracleJava提供了丰富的基础类库,Java 8提供了4000多个基础类(包括下一章将要介绍的集合框架),通过这些基础类库可以提高开发效率,降低开发难度。对于合格的Java程序员而言,至少要熟悉Java SE 70%以上的类(当然本书并不是让读者去背诵Java API文档),但在反复查阅API文档的过程中,会记住大部分类的功能、方法,因此程序员一定要多练,多敲代码。

三、预习与准备

1. 使用Scanner获取键盘输入

2. System

3. Runtime

4. Object

、实验内容

1. 使用Scanner获取键盘输入

2. 使用System类运行外部程序

3. 使用System类获取字符串哈希值

4. 使用Runtime类获取虚拟机的内存信息

5. 使用Object类实现“克隆”功能

 

 实验过程

1. 使用Scanner获取键盘输入

import java.util.Scanner;

public class ScannerKeyBoardTest{

    public static void main(String[] args){

        Scanner sc=new Scanner(System.in);

        while (sc.hasNext()) {

            System.out.println("键盘输入的内容是:"+sc.next());

        }

        sc.close();

    }

}

2. 使用System类运行外部程序

public class ExecTest{

    public static void main(String[] args) throws Exception {

        Runtime rt=Runtime.getRuntime();

        rt.exec("calc");

    }

}

3. 使用System类获取字符串哈希值

public class IdentityHashCodeTest{

    public static void main(String[] args){

        String s1=new String("Hello");

        String s2=new String("Hello");

        System.out.println(s1.hashCode()+"----"+s2.hashCode());

        System.out.println(System.identityHashCode(s1)

+"----"+System.identityHashCode(s2));

        String s3="Java";

        String s4="Java";

        System.out.println(System.identityHashCode(s3)

+"----"+System.identityHashCode(s4));

    }

}

4. 使用Runtime类获取虚拟机的内存信息

public class RuntimeTest{

    public static void main(String[] args){

        Runtime rt=Runtime.getRuntime();

        System.out.println("处理器数量:"+rt.availableProcessors());

        System.out.println("空闲内存数:"+rt.freeMemory());

        System.out.println("总内存数:"+rt.totalMemory());

        System.out.println("可用最大内存数:"+rt.maxMemory());

    }

}

5. 使用Object类实现“克隆”功能

class Address {

    String detail;

    public Address(String detail) {

        this.detail=detail;

    }

}

 

class User implements Cloneable {

    int age;

    Address address;

    public User(int age) {

        this.age=age;

        address=new Address("广州天河");

    }

    public User clone() throws CloneNotSupportedException{

        return (User)super.clone();

    }

}

 

public class CloneTest{

    public static void main(String[] args) throws CloneNotSupportedException{

        User u1=new User(29);

        User u2=u1.clone();

        System.out.println(u1==u2);

        System.out.println(u1.address==u2.address);

    }

}

六、实验效果

1. 使用Scanner获取键盘输入

 

 

2. 使用System类获取字符

 

 

3. 使用Object类实现“克隆”功能

 

、实验总结与体会

在这次的学习当中,我掌握了java基础类一些常用的的方法,也学会了很多的知识,所以在今后的学习中,要更加努力掌握更多的知识,更好的学好Java这门课程,多于老师和同学交流。

顶 0踩 0收藏
文章评论
    发表评论

    个人资料

    • 昵称: 柯侧耳倾听者
    • 等级: 初级设计师
    • 积分: 2220
    • 代码: 64 个
    • 文章: 64 篇
    • 随想: 5 条
    • 访问: 43 次
    • 关注

    标签

    最新提问

      站长推荐