Java教程

Java高级开发

本文主要是介绍Java高级开发,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Java高级开发

1.API概述

1.1.知识概述

API(Application Programming Interface),应用程序编程接口。JavaAPI是提供给开发者的查询手册,是JDK中提供给我们使用类的说明文档。这些类将底层的代码进行了封装,我们不需要关心这些类是如何实现的,只需要学习这些类如何使用。所以我们可以通过查询API手册的方式,来学习Java类库中提供的类,并得知如何使用它们。

1.2.API手册使用步骤

1.打开帮助文档。

2.点击显示,点击索引,找打输入框。

3.要找那个类或接口?在输入框中输入然后回车。

4.看包。

5.看类的解释和说明。

6.学习构造方法。

7.使用成员变量和成员方法。

1.3.课堂提问

1.JDK目前最新的是哪个版本?

2.API手册1.8及1.6为什么使用JDK1.8,却仍然还使用1.6API手册?

因为JDK1.6中文手册是SUN公司官方翻译的中文简体手册,而JDK1.8手册是其他翻译软件的译本,不太标准。

1.4.JDK包结构

功能
java.langJava程序的基础类,如字符串、多线程等,该包中的类使用的频率非常高,所以此包下的类在使用过程中不需要导包。
java.util常用的工具类,如Scanner、集合、随机数生成器、日历、时钟。
java.ioJava对系统文件的操作类,读写操作。
java.netJava中的网络操作。
java.mathJava中数学运算操作。
java.securityJava中安全相关操作。
java.textJava中处理文字、日期、数字信息等格式。

1.5.文档注释

在这里插入图片描述
在这里插入图片描述

2.Scanner类

2.1.知识概述

一个可以解析基本数据类型和字符串的简单文本扫描器。

2.2.引用数据类型的使用步骤

1.导包

使用import关键字导包,引入要使用的类型,注意java.lang包下的类不需要导包。

格式:

import 包名.类名;

举例:

import java.util.Scanner;

2.创建对象

使用该类的构造方法创建该类的对象。

格式:

数据类型 对象名=new 数据类型();

举例:

Scanner scan=new Scanner(System.in);

3.使用属性或方法

使用该类的成员方法完成指定功能。

格式:

对象名.方法名();

举例:

int i=sacn.nextInt();//接收键盘输入的Int类型的整数。

使用Scanner完成键盘录入的代码:

package cn.tedu.Scanner;
import java.util.Scanner;
/**
 * 一.知识概述:
 *  一个可以解析基本数据类型和字符串的简单文本扫描器。
 * 二.引用数据类型的使用步骤
 * 1.导包
 *  使用import关键字导包,引入要使用的类型,注意java.lang包下的类不需要导包。
 *  格式:import 包名.类名;
 * 2.创建对象
 *  数据类型 对象名=new 数据类型();
 * 3.使用属性或方法
 *  对象名.方法名();
 *  
 * @author Administrator
 *
 */
public class ScannerDemo1 {

	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		System.out.println("请输入数字:");
		int num=scan.nextInt();
		System.out.println("扫描到的数字为:"+num);
		//关闭扫描器
		scan.close();
	}

}
package cn.tedu.Scanner;
import java.util.Scanner;
/**
 * 一.知识概述:
 *  一个可以解析基本数据类型和字符串的简单文本扫描器。
 * 二.引用数据类型的使用步骤
 * 1.导包
 *  使用import关键字导包,引入要使用的类型,注意java.lang包下的类不需要导包。
 *  格式:import 包名.类名;
 * 2.创建对象
 *  数据类型 对象名=new 数据类型();
 * 3.使用属性或方法
 *  对象名.方法名();
 * @author Administrator
 *
 */
public class ScannerDemo2 {

	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		System.out.println("请输入文字:");
		String num=scan.nextLine();
		System.out.println("扫描到的文字为:"+num);
		//关闭扫描器
		scan.close();
	}
}

2.3.面试重点

next()方法和nextLine()方法的区别?

Scanner中next()和nextLine() 方法都可以扫描控制台的字符串,区别在于next()方法遇见第一个有效字符(非空格或换行符)时开始扫描,当遇见第一个分隔符或结束符,结束扫描。nextLine()方法获取一行内容并作为字符串返回,遇见换行符时结束。

3.Random类

3.1.知识概述

此类主要生成随机数。

3.2.Random类的使用步骤

1.查看类(导包)

java.util.Random:该类需要import导包

2.查看构造方法(创建对象)

public Random():创建一个新的随机数生成器

3.查看成员(使用成员方法或变量)

  • public int nextInt():返回一个随机数,随机范围为-2147483648到2147483647均匀分配。
  • public int nextInt(int n):返回一个随机数,随机范围为0(包括)到n(不包括)之间均匀分配。
  • public long nextLong():返回一个随机数,随机范围为long的取值范围之间均匀分配。
  • public double nextDouble():返回一个随机数,随机范围为double取值范围之间均匀分配。
  • public boolean nextBoolean():返回一个随机数,随机范围为boolean均匀分配(true、false)。

3.3.课堂练习

使用Random生成3个随机数,范围在0-50之间。

package cn.tedu.Random;
//1.导包
import java.util.Random;
/**
 * 课堂练习:
 * 生成3个随机数,范围在0-50之间。
 * @author Administrator
 */
public class RandomDemo2 {

	public static void main(String[] args) {
		//2.创建对象
		Random ran=new Random();
		//3.使用循环生成随机数
		for(int i=0;i<3;i++){
			int num=ran.nextInt(50);
			System.out.println(num);
		}
	}
}

3.4.课后作业

使用Random生成3个随机数,并全部输出打印到控制台,并取出3个随机数中的最大值。

package cn.tedu.API;

/**
 * 使用Random生成3个随机数,并全部输出打印到控制台,并取出3个随机数中的最大值。
 */

import java.util.Random;

public class HomeWork {
    public static void main(String[] args) {
        Random random = new Random();
        int a=random.nextInt(100);
        int b=random.nextInt(100);
        int c=random.nextInt(100);
        System.out.println("随机数一:"+a+"随机数二:"+b+"随机数三:"+c);
        int temp=a>b?a:b;
        int max=temp>c?temp:c;
        System.out.println("最大值为:"+max);
    }
}

4.Math类

4.1.知识概述

java.lang.Math类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。

类似这样的工具类,其所有方法均分静态方法,并且不会创建对象,调用起来非常简单。

4.2.基本运算的方法

public static int abs(int a):返回int类型a的绝对值。
  //返回int类型a的绝对值。
   int abs1 = Math.abs(5);
   System.out.println(abs1);//5
   int abs2 = Math.abs(-5);
   System.out.println(abs2);//5
public static double ceil(double a):返回大于等于参数a的最小整数。
   //返回大于等于参数a的最小整数。
    double ceil1 = Math.ceil(4.1);
    System.out.println(ceil1);//5.0
    double ceil2 = Math.ceil(-3.3);
    System.out.println(ceil2);//-3.0
public static double floor(double a):返回小于等于参数的最大整数。
   //返回小于等于参数的最大整数。
   double floor1 = Math.floor(3.3);
   System.out.println(floor1);//3.0
   double floor2 = Math.floor(-3.3);
   System.out.println(floor2);//-4.0
public static long round(double a):返回最接近参数a的long值。(类似于四舍五入)
    //返回最接近参数a的long值。(类似于四舍五入)
    long round1 = Math.round(5.5);
    System.out.println(round1);
    long round2 = Math.round(-5.5);
    System.out.println(round2);
public static double random():返回0(包括)到1(不包括)之间的随机小数。
//返回0(包括)到1(不包括)之间的随机小数。
double random = Math.random();
System.out.println(random);

完整代码:

package cn.tedu.Math;
/**
 * Math类概述:
 * java.lang.Math类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
 * 类似这样的工具类,其所有方法均分静态方法,并且不会创建对象,调用起来非常简单。
 * Math类的基本运算方法:
 * public static int abs(int a):返回int类型a的绝对值。
 * public static double ceil(double a):返回大于等于参数a的最小整数。
 * public static double floor(double a):返回小于等于参数的最大整数。
 * public static long round(double a):返回最接近参数a的long值。(类似于四舍五入)
 * public static double random():返回0(包括)到1(不包括)之间的随机小数。
 * @author MaYF
 *
 */
public class MathDemo1 {

	public static void main(String[] args) {
		// 返回int类型a的绝对值。
		int abs1 = Math.abs(5);
		System.out.println(abs1);// 5
		int abs2 = Math.abs(-5);
		System.out.println(abs2);// 5
		System.out.println("---------------");
		// 返回大于等于参数a的最小整数。
		double ceil1 = Math.ceil(4.1);
		System.out.println(ceil1);// 5.0
		double ceil2 = Math.ceil(-3.3);
		System.out.println(ceil2);// -3.0
		System.out.println("---------------");
		// 返回小于等于参数的最大整数。
		double floor1 = Math.floor(3.3);
		System.out.println(floor1);// 3.0
		double floor2 = Math.floor(-3.3);
		System.out.println(floor2);// -4.0
		System.out.println("---------------");
		// 返回最接近参数a的long值。(类似于四舍五入)
		long round1 = Math.round(5.5);
		System.out.println(round1);
		long round2 = Math.round(-5.5);
		System.out.println(round2);
		System.out.println("---------------");
		// 返回0(包括)到1(不包括)之间的随机小数。
		double random = Math.random();
		System.out.println(random);
	}
}

4.3.课堂练习

请使用Math相关的API方法,计算在-10.8到5.9之间,绝对值大于6或者小于2.1的整数有多少个?

package cn.tedu.homework;
/**
 * 请使用Math 相关的API,计算在 -10.8 到5.9 之间,绝对值大于6 或者小于2.1 的整数有多少个?
 * @author MaYF
 */
public class ABSCount {

	public static void main(String[] args) {
		// 定义最小值
		double min =-10.8;
		// 定义最大值
		double max = 5.9;
		// 定义变量计数
		int count = 0;
		// 范围内循环 
		for (double i = Math.ceil(min); i <= max; i++) {
			// 获取绝对值并判断 
			if (Math.abs(i) > 6 || Math.abs(i) < 2.1) {
				// 计数
				count++;
			}
		}
		System.out.println("个数为: " + count + " 个");
		}
}

5.String类

5.1.知识概述

java.lang.String类代表字符串。Java程序中所有的字符串文字(例如“abc”)都可以被看做是实现此类的实例。

String类中包括用于检测各个字符串的方法。如用于比较字符串,搜索字符串,提取子字符串以及创建具有转换为大写或小写的所有字符的字符串的副本。

5.2.字符串特点

1.字符串是不变的:字符串的值在创建之后不能更改(final修饰)。

package cn.tedu.String;
/**
 * String类
 * 特点:
 * 1.String不可变
 * @author Administrator
 *
 */
public class StringDemo1 {

	public static void main(String[] args) {
		String s="abc";
		System.out.println(s);
		String s1=s+"d";
		//s+="d";
		System.out.println(s);
		System.out.println(s1);

	}
}

2.字符串共享: 因为String对象是不可变的,所以它们是可以被共享的。

3.底层存储:“abc”等效于char[] data={‘a’,‘b’,‘c’};字符数组 字符—》字符串

字符串String中使用char数组存储,char的底层byte[]字节数组。

在这里插入图片描述

5.3.使用步骤

1.查看类

java.lang.String:此类不需要导包。

2.查看构造方法

public String():创建新的String对象,以空字符序列创建。

public String(char[] value):通过当前参数中字符数组value来创建新的字符串对象。

public String(byte[] bytes):通过使用平台默认的字符集解码参数中的字节来创建字符串对象。

package cn.tedu.String;
/**
 * String类使用构造方法创建
 * public String():创建新的String对象,以空字符序列创建。
 * 
 * public String(char[] value):
 * 通过当前参数中字符数组value来创建新的字符串对象。
 * 
 * public String(byte[] bytes):
 * 通过使用平台默认的字符集解码参数中的字节来创建字符串对象。
 * @author Administrator
 *
 */
public class StringDemo2 {

	public static void main(String[] args) {
		//方式一:
		String str1=new String();
		System.out.println(str1);
		//方式二:
		char[] ch={'a','b','c'};
		String str2=new String(ch);
		System.out.println(str2);
		//方式三:
		byte[] by={97,98,99};
		String str3=new String(by);
		System.out.println(str3);
		//方式四:
		String str4="abc";
		System.out.println(str4);
	}
}

5.4.常用方法

public boolean equals(Object anObject):将当前字符串与指定字符串anObject进行比较是否相等。

public boolean equalsIgnoreCase(String anotherString):将当前字符串与指定字符串anObject进行比较是否相等,忽略大小写。

public boolean startsWith(String prefix):测试当前字符串是否以指定的前缀开始。

public boolean endsWith(String suffix):测试当前字符串是否以指定的后缀结束。

package cn.tedu.String;
/**
 * String类的常用方法:
 * public boolean equals(Object anObject): 将当前字符串与指定字符串anObject进行比较是否相等。
 * 
 * public boolean equalsIgnoreCase(String anotherString):将当前字符串与指定字符串anObject进行比较是否相等,忽略大小写。
 * 
 * public boolean startsWith(String prefix):测试当前字符串是否以指定的前缀开始。
 * 
 * public boolean endsWith(String suffix):测试当前字符串是否以指定的后缀结束。
 * @author Administrator
 */
public class StringDemo3 {

	public static void main(String[] args) {
		//创建字符串对象
		String s1="hello";
		String s2="hello";
		String s3="HELLO";
		char[] ch={'h','e','l','l','o'};
		String s4=new String(ch);
		String s5=null;
		System.out.println(s1.equals(s2));//true
		System.out.println(s1.equals(s3));//false
		System.out.println(s1.equals(s4));//true
		System.out.println(s1.equals(s5));//false
        //任何一个空对象都不能调用属性和方法否则都会报以下异常
        //java.lang.NullPointerException空指针异常,应该避免。
		//System.out.println(s5.equals(s1));
		System.out.println("------------------");
		System.out.println(s1.equalsIgnoreCase(s2));//true
		System.out.println(s1.equalsIgnoreCase(s3));//true
		System.out.println("------------------");
		String str="Thinking in Java";
		System.out.println(str.startsWith("T"));//true
		System.out.println(str.endsWith("Java"));//true
		System.out.println(str.startsWith("thinking"));//false	
	}
}

5.5.获取功能的方法

public int length():返回此字符串的长度。

public String concat(String str):将指定字符串str连接到当前字符串的末尾。

public char charAt(int index):返回当前字符串中指定索引index处的char字符。

public int indexOf(String str):返回指定字符串str第一次出现在当前字符串的索引位置下标,没有则返回-1。

public String substring(int beginIndex):返回一个子字符串,从当前字符串的beginIdex下标位置开始截取到当前字符串的末尾。

public String substring(int beginIndex,int endIndex):返回一个子字符串,从当前字符串的beginIndex下标位置开始截取到当前字符串的endIndex下标位置结束,包含beginIndex,不包含endIndex。

package cn.tedu.String;
/**
 * 获取功能的方法:
 * public int length():
 * 返回此字符串的长度。
 * public String concat(String str):
 * 将指定字符串str连接到当前字符串的末尾。
 * public char charAt(int index):
 * 返回当前字符串中指定索引index处的char字符。
 * public int indexOf(String str):
 * 返回指定字符串str第一次出现在
 * 当前字符串的索引位置下标,没有则返回-1。
 * public String substring(int beginIndex):
 * 返回一个子字符串,从当前字符串的beginIdex
 * 下标位置开始截取到当前字符串的末尾。
 * public String substring(int beginIndex,int endIndex):
 * 返回一个子字符串,从当前字符串的beginIndex下标位置开始
 * 截取到当前字符串的endIndex下标位置结束,
 * 包含beginIndex,不包含endIndex。
 * 
 * @author Administrator
 *
 */
public class StringDemo5 {

	public static void main(String[] args) {
		//创建字符串对象
		String s="helloworld";
		int length=s.length();
		System.out.println("字符串的长度为:"+length);//字符串的长度为:10
		System.out.println("------------------");
		String s1=s.concat("**hello tedu");
		System.out.println(s1);//helloworld**hello tedu
		System.out.println("------------------");
		System.out.println(s.charAt(0));//h
		System.out.println(s.charAt(9));//d
		System.out.println("------------------");
		System.out.println(s.indexOf("l"));//2
		System.out.println(s.indexOf("owo"));//4
		System.out.println(s.indexOf("ak"));//-1
		System.out.println("------------------");
		System.out.println(s.substring(1));//elloworld
		System.out.println(s.substring(5));//world
		System.out.println("------------------");
		System.out.println(s.substring(4, 7));//owo
		System.out.println(s.substring(5,s.length()));//world		
	}
}

5.6.课堂练习

定义一个方法,将int数组{1,2,3}取出来按照指定格式拼接成一个字符串。

格式:[word1#word2#word3]

结果:[1#2#3]

package cn.tedu.String;
/**
 * 课堂练习:
 *  定义一个方法,将int数组{1,2,3}取出来按照指定格式
 *  拼接成一个字符串。格式:[word1#word2#word3]
 *  [1#2#3]
 * @author Administrator
 *
 */
public class StringDemo6 {

	public static void main(String[] args) {
		//创建int数组
		int[] arr={1,2,3};
		//调用拼接方法
		String s=arrayToString(arr);
		System.out.println("拼接后的字符串为:"+s);
	}
	/**
	 * 字符串拼接的方法
	 * @param arr要做拼接的数组
	 * @return 拼接后的字符串
	 */
	public static String arrayToString(int[] arr){
		String s="[";
		//遍历数组
		for(int i=0;i<arr.length;i++){
			if(i==arr.length-1){
			//到数组的最后一个元素时,
			//就在原有基础上拼接当前元素和]
				s=s.concat(arr[i]+"]");//[1#2#3]
			}else{
		//只要没有到最后一个数组元素,
		//就在原有基础上拼接当前数组元素和#
				s=s.concat(arr[i]+"#");//[1#2#
			}
		}
		return s;
	}
}

5.7.转换功能的方法

public char[] toCharArray():将当前字符串转换为字符数组。

public byte[] getBytes():将当前字符串采用平台默认的字符集编码转换为新的字节数组。

public String replace(CharSequence target, CharSequence replacement):将与target匹配的字符串采用replacement字符串来替换。

package cn.tedu.String;
/**
 * 转换功能的方法:
 * public char[] toCharArray():将当前字符串转换为字符数组。
 * public byte[] getBytes():将当前字符串采用平台默认的字符集编码转换为新的字节数组。
 * public String replace(CharSequence target, CharSequence replacement):将与target匹配的字符串采用replacement字符串来替换。
 * 
 * 
 * @author Administrator
 *
 */
public class StringDemo7 {

	public static void main(String[] args) {
		//创建字符串对象
		String s="abcde";
		//将字符串转换为字符数组
		char[] chs=s.toCharArray();
		//遍历数组
		for (int i = 0; i < chs.length; i++) {
			System.out.println(chs[i]);
		}
		System.out.println("------------------");
		//将字符串转换为字节数组
		byte[] bytes=s.getBytes();
		//遍历数组
		for (int i = 0; i < bytes.length; i++) {
			System.out.println(bytes[i]);
		}
		System.out.println("------------------");
		String str="ittarena ittedu";
		String str1=str.replace("it", "IT");
		System.out.println(str1);
	}
}

小贴士:CharSequence 是一个接口,也是一种引用类型。作为参数类型,可以把String对象传递到方法中。将所有出现的原字符串替换成为新的字符串,返回替换之后的结果新字符串。

5.8.课后作业

统计字符个数

键盘录入一个字符,统计字符串中大写字母、小写字母及数字字符个数。

这篇关于Java高级开发的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!