例3.6
JStudent3.java

public class JStudent3  
{
    String name;
    char sex;
    int stuID;

    public void setName(String stuName)
    {
        name = stuName;
    }

    public static void main(String args[])
    {
        JStudent3 s1 = new JStudent3();
        s1.name = "张三";
        System.out.println("变量name的值为:"+s1.name);
        s1.setName("李四");
        System.out.println("变量name的值现在为:"+s1.name);
    }
}

例3.7
JStudent4.java

public class JStudent4  
{
    String name;
    char sex;
    int stuID;

    public JStudent4()
    {
    }

    public JStudent4(String stuName, char sex, int stuID)
    {
        name = stuName;
        this.sex = sex;
        this.stuID = stuID;
    }

    public void setName(String stuName)
    {
        name = stuName;
    }

    public void setSex(char sex)
    {
        this.sex = sex;
    }

    public void setStuID(int stuID)
    {
        this.stuID = stuID;
    }

    public static void main(String args[])
    {
        JStudent4[] s = new JStudent4[30];
        for (int i = 0; i < s.length; i++)
        {
            s[i] = new JStudent4();
        }
    }
}

例3.8
JStudent4Test.java

public class JStudent4Test  
{
    public static void main(String args[])
    {
        JStudent4 s1 = new JStudent4();
        s1.setName("张三");
        s1.setSex('男');
        s1.setStuID(20130201);
        JStudent4 s2 = new JStudent4("李四", '男', 20130301);
        s2 = s1;
        s1 = null;
    }
}

例3.9
JPassTest.java

public class JPassTest  
{
    float ptValue;

    // 参数类型是基本数据类型
    public void changeInt(int value)
    {
        value = 55;
    }

    // 参数类型是引用数据类型
    public void changeStr(String value)
    {
        value = new String("world");
    }

    // 参数类型是引用数据类型
    public void changeObjValue(JPassTest ref)
    {
        ref.ptValue = 99.0f;
    }

    public static void main(String args[])
    {
        String str;
        int val;
        // 创建JPassTest类的对象
        JPassTest pt = new JPassTest();
        // 测试基本数据类型参数的传递
        val = 11;
        pt.changeInt(val);
        System.out.println("Int value is: " + val);
        // 测试引用数据类型参数的传递
        str = new String("Hello");
        pt.changeStr(str);
        System.out.println("Str value is: " + str);
        // 测试引用数据类型参数的传递
        pt.ptValue = 101.0f;
        pt.changeObjValue(pt);
        System.out.println("Pt value is: " + pt.ptValue);
    }
}

注:本博客内容节选自高飞编著的Java程序设计实用教程 ,详细内容请参阅书籍。