Java笔记二——认识对象

两种类型系统
基本类型
类类型

2.1 定义类

类(Class)->对象(Object)
抽象 –> 实例

class Clothes {
    String color;
    char size;
}

Clothes c1;  //声明参考名称、参考变量或者叫参考
new Clothes();  //新建一个对象
Clothes c1 = new Clothes();  //将c1名称参考至新建对象

Field.java

package openhome.cc;

class Clothes {
    String color;
    char size;
    //构造函数
    Clothes(String color, char size){
        this.color = color;
        this.size = size;
    }
}


public class Field {
    public static void main(String[] args)
    {
        Clothes sun = new Clothes("red",'S');
        System.out.printf("sun ( %s %s )%n", sun.color, sun.size);

    }
}

2.2标准类

java.util.Scanner

import java.util.Scanner;
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
//看标准输入中,有没有输入下一个字符串(以空格或换行符分隔),若有尝试将其剖析为int类型
//同理还有nextLong(), nextFloat(), nextDouble(), nextBoolean()

java.math.BigDecimal类得到想要的精确度

import java.math.BigDecimal;
BigDecimal rand1 = new BigDecimal("1.0");
BigDecimal rand2 = new BigDecimal("0.8");
BigDecimal result = rand1.subtract(rand2);

2.3 对象指定与相等

== 是否绑定在同一对象上
equal 是否所绑定的对象的内含值相同

BigDecimal rand1 = new BigDecimal("1.0");
BigDecimal rand2 = new BigDecimal("1.0");
System.out.printf(a == b);  //显示false
System.out.printf(a.equal(b));  //显示true

2.4基本类型打包器(Wrap)

基本类型:效率高
类类型:可以携带更多信息
基本类型打包器都归类于 java.lang

int data = 10;
Integer wrapper = new Integer(data);
System.out.print(data/3);   //基本类型操作
System.out.print(wrapper.doubleValue()/3);  //操作打包器方法

自动装箱,拆箱

Integer data1 = 10;   //自动装箱
int i = 10;
Integer warpper1 = i;  //自动装箱
Number number = 3.14f; //一般用Number类来自动装箱
Integer wrapper2 =10;  //自动装箱
int foo = wrapper2;    //自动拆箱

2.5 数组对象

数组在java中就是对象,数组一旦建立,长度就固定

2.5.1 一维数组

int[] scores = {88, 95, 63, 98, 85, 92};   //声明+初始化
// int[]进行申明,在内存中分配长度为6int连续空间,各个空间都给索引,索引从0开始,由于长度为6,最后一个所以为5

int[] ages = new int[6];   //声明,后面可以赋值
//int[] ages = new int[] {14,14,13,15,15,14};//声明后直接赋值
int height = new int(183);

for(int i; i < scores.length; i++)   //score是对象,length是属性,不像c++是函数
{
    System.out.printf("学生分数:%d %n",scores[i]);
}
增强版for
for(int score : scores) {
        System.out.printf("学生分数:%d %n",score);
    }

2.5.2 二维数组

int[][] cords= {
                {1,2,3},
                {4,5,6}
        };
int[][] cords = new int[2][3];

2.5.3数组复制

int[] scores1 = {88, 95, 63, 98, 85, 92};
int[] scores2 = score1; 
//并非数组复制

并非数组复制,需要用for循环,或Arrays.copyof(array,length),如下所示

import java.util.Arrays;
int[] scores1 = {88, 95, 63, 98, 85, 92};
int[] scores2 = Arrays.copyOf(scores1,scores1.length);
for(int score:scores2) {
    System.out.printf("%3d", score);
}

2.6 浅层赋值和深层赋值

class Clothes {
    String color;
    char size;
    Clothes(String color, char size){
        this.color = color;
        this.size = size;
    }
}

Clothes[] c1 = {new Clothes("red",'S'), new Clothes("blue", 'M')};
Clothes[] c2 = new Clothes[c1.length];
for(int i=0; i < c1.length; i++)
{
    //c2[i] = c1[i];  //浅层赋值,参考索引
    Clothes c = new Clothes(c1[i].color, c1[i].size); 
    //深拷贝,需要自己来赋值元素
    c2[i] = c;   
}

2.6 字符串

char[] name= {'j', 'u', 's', 't', 'i', 'n'};
String name2 = new String(name);
String name3 = new String(name);
System.out.println(name2 == name3);  //false,对象不同

String name1 = "justin";
String name4 = "justin";
System.out.println(name1 == name4);  //true
//以""包括的字符串,只要内容相同,无论在程序中出现几次,JVM都只建立一个String实例

String name5 = name1;
String name6 = name4;
System.out.println(name5 == name6);  //true

字符串对象一旦建立,就无法更改对象中的任何内容
但可以使用+

String str1 = "hello";
String str2 = str1 + "world";

2.7 Java API

http://www.oracle.com/technetwork/cn/java/javase/downloads/index.html
点击右上方的 Java SE

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值