Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java语言程序设计 第二部分 Java语言基础.

Similar presentations


Presentation on theme: "Java语言程序设计 第二部分 Java语言基础."— Presentation transcript:

1 Java语言程序设计 第二部分 Java语言基础

2 Java语言基础 第一讲 基本数据类型

3 标识符 标识符唯一地标识计算机中运行或存在的任何一个成分的名称。不过,通常所说的标识符是指用户自定义标识符,即用户为自己程序中的各种成分所定义的名称。 标识符可以由字母、数字、下划线“_”和美元符号“$”组成,但必须以字母、下划线或美元符号开头。 Java语言使用国际字符格式标准(Unicode),Unicode字符集采用 16位编码,其前256个字符与ASCII字符集完全一致。除了数字0-9、英文字母A-Z、a-z及+、-、*、/等ASCII字符之外, Unicode字符集还提供了其他语言文字,如拉丁语、希腊语、汉字等。

4 标识符 变量 字母小写,采用有意义的单词(循环变量i、j、k除外),由多个单词组成变量名每个单词的首字母大写(第一个单词除外) 方法 同变量,尽量用动词 常量 字母大写,单词间用下划线分隔 类和接口 首字母大写

5 关键字 关键字通常也称为保留字,是程序设计语言本身已经使用且被赋予特定意义的一些标识符。 abstract continue for new
switch boolean default goto* null synchronized break do if package this byte double implements private threadsafe byvalve * else import protected throw case extends instanceof public transient catch false int return true char final interface short try class finally long static void const * float native super while

6 数据类型

7 变量声明 变量类型 identifier [, identifier];
int ID; float price, wholesalePrice; char myChar; boolean isOpen; 变量类型 identifier = value [, identifier = value]; int ID = ; float price = 2.35F, wholesalePrice = 1.95F ; char myChar = ’t’; boolean isOpen = false;

8 常量声明 常量声明的形式与变量声明基本一样,只需用关键字final标识, final写在最前面。 常量标识符全部用大写字母表示。
final int MAX=10; final float PI=3.14f; 常量标识符全部用大写字母表示。

9 数据类型转换 数据类型转换是将一种类型的数据转变为另一种类型的数据。当表达式中的数据类型不一致时,就需要进行数据类型转换。类型转换的方法有两种:隐式类型转换和显式类型转换。 当一个容纳信息量小的类型转化为一个信息量大的类型时,数据本身的信息不会丢失,所以它是安全的,这时编译器会自动地完成类型转换工作,这种转换被称为隐式数据类型转换。如我们将一个int型的数据转换为double型时是不用强制声明的。

10 数据类型转换 数据类型转换是将一种类型的数据转变为另一种类型的数据。当表达式中的数据类型不一致时,就需要进行数据类型转换。类型转换的方法有两种:隐式类型转换和显式类型转换。 当把一个容量较大的数据类型向一个容量较小的数据类型转换时,可能面临信息丢失的危险,此时必须使用显式类型转换。显式类型转换的形式为: (类型)表达式 例如: int n; double x; n=x; //编译错误 n=(int)x; //正确

11 Java语言基础 第二讲 运算符与表达式

12 运算符与表达式 运算符代表着特定的运算指令,程序运行时将对运算符连接的操作数进行相应的运算
表达式是用运算符把操作数(变量、常量等)连接起来表达某种运算或含义的式子。

13 算术运算符

14 算术运算符

15 关系运算符

16 逻辑运算符 短路 if(test1(0) && test2(2) && test3(2))

17 public class ShortCircuit {
static boolean test1(int val) { System.out.println("test1(" + val + ")"); System.out.println("result: " + (val < 1)); return val < 1; } static boolean test2(int val) { System.out.println("test2(" + val + ")"); System.out.println("result: " + (val < 2)); return val < 2;

18 static boolean test3(int val) {
System.out.println("test3(" + val + ")"); System.out.println("result: " + (val < 3)); return val < 3; } public static void main(String[] args) { if(test1(0) && test2(2) && test3(2)) System.out.println("expression is true"); else System.out.println("expression is false");

19 运算符 位运算符 运算符的优先级 逻辑运算 & | ~ ^ 移位运算 << >> >>> 括号
& | ~ ^ 移位运算 << >> >>> 运算符的优先级 括号 算术运算符 关系运算符 逻辑运算符

20 运算符的优先级和结合性 优先级别 运算符 含义 要求运算 对象的个数 结合方向 1 ()、[]、. 自左至右 2
!、~、++、--、-、(类型)、sizeof (单目运算符) 自右至左 3 *、/、% (双目运算符) 4 +、- 5 <<、>> 6 <、<=、>、>= 7 = =、!= 8 & 9 ^ 10 | 11 &&、|| 12 ? : (三目运算符) 13 =、+=、-=、*=、/=、%=、>>=、<<=、&=、^=、|= 14 ,

21 Java语言基础 第三讲 流程控制语句-分支

22 流程控制 表达式语句 表达式; sum=sum+1; a>b; 块语句 { [变量声明或常量声明]; 语句序列; }

23 程序的3种基本结构

24 if语句

25 if语句

26 if语句

27 switch语句 public class SwitchDaysInMonth{
public static void main(String args[]){ int month = Integer.parseInt(args[0]); switch(month){ case 1: case 3: case 5: case 7: case 8: case 10: case 12: System.out.println("There are 31 days in that month."); break;

28 case 2: System.out.println("There are 28 days in that month."); break; case 4: case 6: case 9: case 11: System.out.println("There are 30 days in that month."); default: System.out.println("Invalid month."); }

29 分支结构示例 判断某年是平年还是闰年,如果某年能被4整除并且不能被100整除,或者能被400整除,那么这年是闰年。
将百分成绩转换为五等级成绩A、B、C、D、E

30 Java语言基础 第三讲 流程控制语句-循环

31 流程控制 循环语句 for语句 while语句 do … while语句

32 流程控制

33 流程控制

34 流程控制

35 流程控制

36 流程控制

37 流程控制 其他流程控制语句 goto break continue

38 流程控制 标号 LabeledFor.java LabeledWhile.java

39 循环结构示例 输入一个整数,求每个位上数字之和 求 内的所有素数

40 Java语言基础 第四讲 数组

41 数组 数组(array)是具有相同数据类型的有限个同名变量的有序集合。一个数组包含若干个 同名变量,每个变量被称为一个数组元素,数组的元素个数称为数组的长度。 数组元素在数组中的位置称为数组的下标。通过数组名加下标的形式可以引用数组中的 指定元素。数组下标的个数称为数组的维数,有一个下标是一维数组,有二个下标就是二维 数组,其余类推。 通常不对数组变量本身进行运算或操作,而是对数组变量中所包含的数组元素进行运算和操作。对数组元素所能进行的运算和操作,取决于数组元素所属的数据类型。

42 一维数组 声明一维数组变量 使用new为数组分配空间 数据类型[] 数组 或 数据类型 数组[] int a[ ]; 或 int[ ] a;
数据类型[] 数组 或 数据类型 数组[] int a[ ]; 或 int[ ] a; double[ ] arrray1; double[ ] array2; double[ ] arrray1,array2; 使用new为数组分配空间 数组 = new 数据类型[长度] a = new int[5]; int a[ ] = new int[5];

43 数组的存储 1 A[0] 2 A[1] 3 A[2] 引用地址 4 A[3] 一维数组A 5 A[4]

44 数组 数组在堆中创建,在java中没有静态数 import java.util.*; public class ArrayNew {
static Random rand = new Random(); static int pRand(int mod) { return Math.abs(rand.nextInt()) % mod + 1; } public static void main(String[] args) { int[] a; a = new int[pRand(20)]; prt("length of a = " + a.length); for(int i = 0; i < a.length; i++) prt("a[" + i + "] = " + a[i]); System.out.println(a); static void prt(String s) { System.out.println(s); 数组在堆中创建,在java中没有静态数

45 数组 数组可以互相赋值 int[] a,b; a=new int[5]; b=new int[10]; a=b;

46 数组

47 数组 数组边界 a.length 运行时错误 数组初始化 数组拷贝 Array.copy

48 数组示例 查找数组中的最大值 找出最大的数及其下标 对数组排序

49 二维数组 声明二维数组 二维数组元素表示格式 int mat[ ][ ] = new int [3][4];
二维数组[下标1][下标2] mat[i][j] 表示第i行第j列的数组元素

50 public class InitArray
{ public static void main( String args[] ) int array1[][] = { { 1, 2, 3 }, { 4, 5, 6 } }; int array2[][] = { { 1, 2 }, { 3 }, { 4, 5, 6 } }; System.out.println( "Values in array1 by row are" ); outputArray( array1 ); System.out.println( "\nValues in array2 by row are" ); outputArray( array2 ); // displays array2 by row } public static void outputArray( int array[][] ) for ( int row = 0; row < array.length; row++ ) for ( int column = 0; column < array[ row ].length; column++ ) System.out.printf( "%d ", array[ row ][ column ] ); System.out.println(); // start new line of output

51 多维数组的存储 A[0][0] A[0][1] A[0][2] A[0] A[1][0] A[1] 引用地址 A[1][1] A[2]

52 Java语言基础 第五讲 字符串

53 字符串 字符串概述 字符串说明 不可变字符串String 可变字符串StringBuffer 不使用new关键字
String s=“abc”; 使用new关键字 String s=new String(“abc”);

54 字符串 字符串池 abc s1 s2 s3 s4 abc abc String s1=“abc”; String s2=“abc”;
String s3=new String(“abc”); String s4=new String(“abc”); s1 s2 s3 s4 abc abc

55 main方法的命令行参数 java Introductions ljs teacher public class Introductions
{ public static void main (String args[]) System.out.println("My name is " + args[0] + “ and I am a " + args[1]); }

56 字符串处理 字符串长度 字符串连接 取子串 转换成字符数组 str.length() +、str.concat()
str.subString() 转换成字符数组 str.toCharArray()

57 字符串处理 获得指定位置的字符 大小写转换 字符替换 去字符串左右空格 str.charAt()
str.toUpperCase()、str.toLowerCase() 字符替换 str.replace() 去字符串左右空格 str.trim()

58 字符串处理 字符串查找 字符串的分割 字符串格式化 字符串的比较 str.indexOf() str.lastIndexOf()
str.split() 字符串格式化 str.format() 字符串的比较 str.equals() str.compareTo() str.compareToIgnoreCase()

59 字符串转换 字符串转换成基本数据类型 基本数据类型转换成字符串 对象类型转换为字符串 基本类的parseXXX() valueOf()
String.valueOf() 对象类型转换为字符串 toString()

60 StringBuffer类 构造 基本操作 修改操作 选择使用String和StringBuffer
new StringBuffer(…); 基本操作 length、charAt、subString 修改操作 追加append 插入insert 替换replace 删除delete 选择使用String和StringBuffer

61 字符串示例 统计字符串中字符出现的次数。 从字符串中提取出数字,并将其按出现的顺序转换成一个整数。 反转输出字符串。


Download ppt "Java语言程序设计 第二部分 Java语言基础."

Similar presentations


Ads by Google