上一篇文章:Java中的包、import语句、访问权限
基本数据类型的类封装
Java的基本数据类型:boolean、byte、short、char、int、long、float、double
Java 也提供了与基本数据类型相关的类,实现了对基本数据类型的封装。这些类在java.lang
包中,该包是Java的核心,不需要手动import
导入,系统会自动导入。
基本数据类型与其类封装的对应:
基本数据类型 | 对应的类封装 |
---|---|
boolean | Boolean |
byte | Byte |
short | Short |
char | Character |
int | Integer |
long | Long |
float | Float |
double | Double |
class Example {
public static void main(String args[]){
Boolean a = new Boolean(true); //创建了一个Boolean的对象a,并使用Boolean()构造方法初始化对象
byte by = 127; //声明了一个byte型的变量by
Byte b = new Byte(by); //创建了一个Byte的对象b,并使用构造方法Byte()初始化该对象
short sh = 6;
Short c = new Short(sh);
Character d = new Character('a');
Integer e = new Integer(999);
Long f = new Long(6666L);
Float g = new Float(3.14f);
Double h = new Double(3.1415926);
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
System.out.println(f);
System.out.println(g);
System.out.println(h);
System.out.println("也可以使用xxx.Value()的方法返回对象的数据:");
System.out.println(a.booleanValue());
System.out.println(b.byteValue());
System.out.println(c.shortValue());
System.out.println(d.charValue());
System.out.println(e.intValue());
System.out.println(f.longValue());
System.out.println(g.floatValue());
System.out.println(h.doubleValue());
System.out.println("注意区分变量和对象的区别: 变量只能参与运算,而对象可以调用方法。");
System.out.println(e*10);
System.out.println(e == 999);
}
}
注意区分变量和对象的区别: 变量只能参与运算,而对象可以调用方法。
在 Character 类中还有一些类方法,可以通过类名直接调用:
Character.isLowerCase()
判断是否为小写字母Character.isUpperCase()
判断是否为大写字母Character.toUpperCase()
将小写字母转换为大写字母Character.toLowerCase()
将大写字母转换为小写字母
public class Example {
public static void main(String args[]){
char a[] = {'j','A','c','K','E','y','S','o','N','g'};
for(int i = 0; i <a.length; i++){
if(Character.isLowerCase(a[i]))
a[i] = Character.toUpperCase(a[i]);
else if(Character.isUpperCase(a[i]))
a[i] = Character.toLowerCase(a[i]);
}
for(int i = 0; i < a.length; i++)
System.out.print(" "+a[i]);
System.out.println(); //换行
System.out.println(Character.isLowerCase(a[0]));
}
}
数组作为方法的参数
将数组传递给方法时,是将数组的引用传递给方法。就像是C语言中将数组的指针传递给函数一样:
public class Example {
public static void printNums(int[] nums){
for(int i = 0; i < nums.length; i++)
System.out.print(nums[i]+" ");
}
public static void upChar(char[] ch){
for(int i = 0; i < ch.length; i++)
ch[i] = Character.toUpperCase(ch[i]);
}
public static void main(String args[]){
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
char b[] = {'j','a','c','k','e','y','s','o','n','g'};
printNums(a);
upChar(b);
System.out.println();//换行
for(int i = 0; i < b.length; i++)
System.out.print(b[i]);
}
}
从方法中返回数组
返回数组的方法定义格式为 修饰符 返回值类型[] 方法名(参数表)
,返回的数组名其实是个引用。
//使用方法将传入的数组倒置后返回
public class Example {
public static int[] reverse(int[] list){ //定义返回数组的方法
int c, j;
c = list.length;
j = c - 1;
int[] result = new int[c]; //声明一个与数组list等长度的数组
for(int i = 0; i < list.length; i++){
result[j] = list[i];
j--;
}
return result; //将倒置后的数组返回
}
public static void main(String args[]){
int[] a = {1, 2, 3, 4, 5};
int[] b = reverse(a);
for(int i = 0; i < b.length; i++){
System.out.print(" "+b[i]);
}
}
}
对象数组
创建多个同一类的对象,将其串在一起,组成对象数组。
class People {
String name;
void speak(){
System.out.println("我的名字是:"+name);
}
}
public class Example {
public static void main(String args[]){
People [] mans = new People[4]; //声明长度为4的People类数组
//以上只是声明了数组mans有4个元素,但其都是空对象,需要使用构造方法对其初始化才能使用
for(int i = 0; i < 4; i++)
System.out.println(mans[i]);
System.out.println("未使用构造方法为声明的对象初始化之前,其都是空对象");
for(int i = 0; i < 4; i++)
mans[i] = new People();//使用构造方法People()初始化对象,才能使用
mans[0].name = "张三";
mans[1].name = "李四";
mans[2].name = "王二";
mans[3].name = "Jackey";
for(int i = 0; i < 4; i++)
mans[i].speak();
}
}
文档生成器
JDK提供的javadoc.exe
可以制作源文件类结构的HTML格式的文档,打开文档可以查看类的详细信息,就相当于UML图,但是要比UML图更加详细。
命令行下进入源文件所在目录:使用命令javadoc 源文件名
的格式来制作类的文档信息;使用命令javadoc 指定文件路径 源文件名
的格式,将制作的类文档信息保存到指定的文件路径中。