基本数据类型的类封装、数组作为方法的参数以及从方法中返回数组、对象数组、文档生成器

上一篇文章:Java中的包、import语句、访问权限

基本数据类型的类封装

Java的基本数据类型:boolean、byte、short、char、int、long、float、double

Java 也提供了与基本数据类型相关的类,实现了对基本数据类型的封装。这些类在java.lang包中,该包是Java的核心,不需要手动import导入,系统会自动导入。

基本数据类型与其类封装的对应:

基本数据类型对应的类封装
booleanBoolean
byteByte
shortShort
charCharacter
intInteger
longLong
floatFloat
doubleDouble
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 指定文件路径 源文件名的格式,将制作的类文档信息保存到指定的文件路径中。

下一篇文章

Java中的继承(一)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jackey_Song_Odd

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值