包装类2007.5.11
对方法的学习:主要学习该方法的返回值类型,方法参数类型,方法的作用
JAVA的标准库结构:
API: JAVA内置资源库:Application Programming Interface(字典)封装了一系列方法。
API中常用的包:(类和接口)
1. java.long包:(基础和默认的语言包)包含基本数据类型
2. java.util包:是java.long包的扩展和补充。大部分内容封装的是集合类。(数组集合)
3. java.io包:封装了一系列输入和输出相关的类和接口
4. java.net包:封装了网络编程方面的内容
5. java.text包:有关日期和格式化的(银行转帐,存款额 10.0--->$10.00,double类型不能完成)
6. java.awt包:关于图形用户界面
7. java.swing包:java.awt包的升级,扩展(即将学习)
8. java.applet包:小应用程序包
equals( )与equal(==)的区别
标识性接口:只有接口名称,没有接口内容 如 接口cloneable
标识性接口作用:定义标识性接口作用并不是为程序员看的,而是给jvm看的。
注:对象与对象多数是值与值的比较,很少有地址之间的比较。
equals( )与equal(==)的区别:
l equals是基本数据和基本数据类型之间的比较;equals( )方法是指对象值与对象值之间的
比较。
l Equals(==)是地址与地址之间的比较;equals( )是值与值的比较。
if(which.equals("men"))//equals代表==
return new Men( );//返回Men类型的无名对象
`
包装类:里面封装的是八种数据类型及八种数据类型所对应的类类型,将8种基本数据类型包装到类类型中,是为基本数据类型设计的,
作用:完成基本数据类型以及基本数据类型所对应的包装类类型和字符串类型之间的相互转换。
注:将来我们写程序的过程中都是基于图形用户界面的,有关于图形用户界面所出现的文字信息全部都是字符串的表示格式。
String 1 | String 2 | ||
= | 想得到的结果为3 | “1”+“2”=“12” | |
图形用户界面》》》》》通过包装包装类进行类类型与基本数据类型之间的转换 | |||
l 基本数据类型:[参与运算+ - * / ]整数数据类型byte,short int long;小数数据类型double, float
l 类类型:复合数据类型
JAVA中特殊的两个类:包装类和String类在实例化对象时都有一个确切的值与之对应,其它的类类型都没有确切的值与之对应。
例:A a=new A( );//a1没有声明对应值
Integer a1=new Integer(12); //a1=12
`a1.parseInt(“1237”)//对象调用类方法
`a1.toString( )//调用的是本类的方法返回值是“12”
`a.toString( )//返回对象的字符串的表示格式
`
JAVA中的基本数据类型所对应的数据类型包装类
基本数据类型 | 数据类型包装类 |
Byte | byte |
Short | short |
# Integer | int |
Long | long |
Float | float |
Double | double |
# Character | char |
Boolean | boolean |
`
数据类型包装类所共有的方法
1. public String toString( )//重构 | 包装类和字符串之间的转换 |
2. public type typeValue( )//八种数据类型 | 包装类型转换基本类型 |
3. public boolean equals(Object obj )//重构 | 用于包装类或字符串类之间值与值之间比较 |
1
A a=new A( );
`a.toString( )//返回对象的字符串的表示格式
2
class dfsd
{
public static void main(String args[ ])
{
Integer a1=new Integer(12);//a1=12
int a=a1.intValue( ); //包装类型Integer和基本类型int之间的转换
double b=a1.doubleValue( );
byte c=a1.byteValue( );
System.out.println (a);
System.out.println (b);
System.out.println (c)
}
}
````````````````````````````````````````````````````````````````
例:5.3 p131
class Exam
{
public static void main(String args[ ])
{
//构造方法
Integer a=new Integer(1);//声明包装类a
Boolean b=new Boolean(true); //声明包装类b
Float f=new Float(-2.4); //声明包装类f
Double d=new Double(1.4); //声明包装类d
Character ch=new Character('A'); //声明包装类ch
Character ch2=new Character('A'); //声明包装类ch2
int i=a.intValue( );
char c=ch.charValue( );//字符A
System.out.println(i);
System.out.println (b.booleanValue( ));
System.out.println (f.floatValue( ));
System.out.println (d.doubleValue( ));
System.out.println (c);
System.out.println (ch.equals(ch2));
System.out.println (d.toString( ));
}
}
Integer类:
二个构造方法:
1. public Integer(int value) 将基本数据类型转换成包装类。
例:Integer a1=new Integer(12)//a1=12
2. public Integer (String s) throws NumberFormatException//抛出一个数字格式化异常
例:Integer a1=new Integer(“1”);
Integer a1=new Integer(“1.1”) ;//抛出一个数字格式化异常
Integer a1=new Integer(“abc”);//不对,要传一个类似的整数数据类型的字符串
p134
Integer包装类的类变量
类变量 | 说明 |
public static final int MAX_VALUE | Int类型的最大值 |
public static final int MIN_VALUE | Int类型的最小值 |
例:
System.out.println (Integer.MAX_VALUE);//
System.out.println (Integer.MIN_VALUE);
```````````````````````````````````````````````````````````````````````
Integer类常用方法及说明
方法 | 说明 |
Pulbic static int paseInt(String s) Throws NumberFormatException | 将字符串转为十进制整数 String与int 之间(通过包装类来转换) |
Public static Integer valueOf(String s) Throws NumberFormatException | 将字符串转成包装类或日期时间类 |
例:Integer a1=new Integer (“123”);
int a=a1.parseInt(123);
**例:数据类型之间的转换方法:(非常重要)
class ajj
{
public static void main(String args[ ])
{
int b=3;
Integer b1=new Integer(b); System.out.println (b1); //int--Integer
System.out.println (b1.toString( ));//integer--string
System.out.println (Integer.parseInt("2314"));//string—解析int
System.out.println (Integer.valueOf("45645"));//string --integer
System.out.println (b1.intValue( ));//Integer--int
}
}
Character类
构造方法:public Character (char value)
将char转为包装类类型
例:character ch=new Character (‘A’);
Character类中的常用方法
public static boolean isLowerCase(char ch) | ch小写为true |
public static boolean isUpperCase(char ch) | ch大写为true |
pulbic static boolean IsDigit(char ch) | ch是数字,返回true |
pulbic static boolean isLetter(char ch) | ch是否字母 |
pulbic static char toLowerCase (char ch) | ch字母的小写 |
pulbic static char toUpperCase(char ch) | ch字母的大写 |
pulbic static boolean IsDigit(char ch) |
|
public static boolean isWhitespace(char ch) | 参数是否一个空格 是:true,(参数为char) |
public static int digit(char ch,int radix)
| ch字符对应进制的值 参数:char ch(A-F[a-f] 或0-9,16或10) |
Character isLowerCase(‘b’)//true
Character.isUpperCase(‘B’)]//true
例:Character . int digit( ), ch.charValue( ), Character.isDigit( )方法的用法
class CharatcterExam
{
public static void main(String args[ ])
{
Character ch=new Character('s');//构造方法
char c=ch.charValue( );//转为基本类型调用的是typeValue( )
//方法
if(Character.isUpperCase(c))
System.out.println ("The character"+c+"is upper case.");
else
System.out.println("The character"+c+"is lower case.");
boolean b=Character.isDigit(c);
int x=Character.digit('c',16);//十六进制中c为12
System.out.println ("b="+b);
System.out.println ("x="+x);
int r=Character.digit('2',10);//
System.out.println (r);
}
}
Java中的String str="abc"; String str=new String("abc");和String str = new String( );的区别以及==与equals( )的不同。
Java运行环境有一个字符串池,由String类维护。执行语句String str="abc"时,首先查看字符串池中是否存在字符串"abc",如果存在则直接将"abc"赋给str,如果不存在则先在字符串池中新建一个字符串"abc",然后再将其赋给str。执行语句String str=new String("abc")时,不管字符串池中是否存在字符串"abc",直接新建一个字符串"abc"(注意:新建的字符串"abc"不是在字符串池中),然后将其付给str。前一语句的效率高,后一语句的效率低,因为新建字符串占用内存空间。String str = new String( )创建了一个空字符串,与String str=new String("")相同。下面举个例子说明:
public class CompareString {
public static void main(String[ ] args) {
String a = new String( );
String aa = "";
String aaa = new String("");
String b = new String("asdf");
String c = new String("asdf");
String d = "asdf";
System.out.println(a == aa);
System.out.println(a == aaa);
System.out.println(a.intern( ) == aa.intern( ));
System.out.println(a.intern( ) == aaa.intern( ));
System.out.println(d == "asdf");
System.out.println(b == c);
System.out.println(b == d);
System.out.println(b.equals(c));
System.out.println(b.equals(d));
b = b.intern( );
System.out.println(b == c);
System.out.println(b == d);
c = c.intern( );
System.out.println(b == c);
}
}
以上程序的运行结果为:
从运行结果可以验证前面所述的内容。如果不懂String 类的intern( )方法的用法可以参考jdk自带的文档.
从CompareString类中我们也可以看出==与equals( )的不同之处:即==比较的是两个对象的引用(即内存地址)是否相等,而equals( )比较的是两个对象的值(即内存地址里存放的值)是否相等。当然equals( )在个别类中被重写了那就例外了。
问题:怎么理解:Object o="123";
我想了解一下,如果
Object o="123";
int[ ] a={1,2,3};
那么
boolean b=o.equals(a);
返回值是b=false.
我知道对于String之间的比较是比较内容,其他都是比较引用。
请问,这里是String与array之间的比较,
equals()到底是如何工作的?
class my
{
public static void main(String args[ ])
{
Object o="123";//将String无名对象传给O,O拥有Object类的属性,、String类型重构而来的方法。
int[ ] a={1,2,3};
boolean b=o.equals(a);//值比较,a为int,o为String类型
System.out.println (b);//false
}
}
class my
{
public static void main(String args[ ])throws Exception
{
System.out.println ("请输入一个字符:");
char c=(char)System.in.read( );
if(Character.isLowerCase(c))
System.out.println ("你输入的是:"+c+"对应的大写为"+Character.toUpperCase(c));
else if(Character.isUpperCase(c))
System.out.println ("你输入的是:"+c+"对应的小写为"+Character.toLowerCase(c));
else if(Character.isDigit(c))
{
System.exit(-1);
System.out.println ("你输入的是:"+c+"程序退出");
}
}
}