201871010105- Cao in "object-oriented programming (Java)" the second week of learning summary
project | content |
This work belongs courses | https://www.cnblogs.com/nwnu-daizh/ |
Where the job requires | https://www.cnblogs.com/nwnu-daizh/p/11435127.html |
Target operational requirements | 1. adapt the teacher teaching, learning can be completed this week, according to independent study theoretical knowledge requirements; 2. grasp the Java Application Program Structure; 3. master and variable data types Java language; 4. learn to use various types of expressions configured Java operator; The master Java Application input-output technology; 6. grasp the Java process control technology (branch, loop); (Key) 7. grasp Math class, String class usage. (difficulty) |
first part:
1, the identifier:
C available identifier number, uppercase and lowercase letters, underline, not begin with a number;
Java available identifiers besides the 3 C's, one more with a dollar sign ($), the same can not start with a number.
2, Keywords:
C keywords have: auto break case char const continue default do double else and so on;
There are Java keywords: abstract boolean break byte case catch char class continue and so on;
3, the data type:
Data type in C are:
1) Basic Type: Integer (basic integer int, short int short [int] Long and long [int] signed and type [signed], unsigned unsigned), char [signed / unsigned] char , float (single-precision float, double, double, and long double long double), enumerated type
2) structure types: an array type, type of structure, union type
3) pointer type
4) Air Type
Note that the various types of ships Number of Bytes:
int: 2 bytes
short: 2 bytes
long: 4 bytes
char: 1 byte
float: 4 bytes
double: 8 bytes
Java data types:
1) Basic type: char (char), number (integer (byte byte, short integer short, int int, long integer long), float (single-precision float, double double )), Boolean (boolean (true or false))
2) complex type: class, interface, array
Note that the various types of storage Number of Bytes:
byte: 1 byte
short: 2 bytes
int: 4 bytes
long: 8 bytes
char: 2 bytes (Unicode encoded)
float: 4 bytes
double: 8 bytes
4, constants and variables
1) Constant
Same integer constants defined in Java and C, in addition to long data in its outer end by l or L, other types of values are displayed directly. C, thereafter adding the unsigned constants u or U. For different binary, decimal digit can not directly show the highest 0, octal begin with 0, hexadecimal begin with 0x or 0X.
For floating-point type, C and Java are only using decimal notation. And using decimal exponent, e exponent notation separated by a decimal or between and E index. Note that the Java single precision required to add f or F, after double D to add or d to show distinction.
Character constants are in single quotation marks or escape a single character string representation. Special attention, C represents only the ASCII code for character of Unicode characters from 0 to 255, Java byte in the storage unit 2 may represent special characters, indication when unicode encoding \ u plus four hexadecimal string.
Boolean type only in Java only, need special attention.
Java中常量用关键字final修饰,一经赋值不可更改;C中表示不可更改的关键字是const,其修饰的变量(注意是变量,不是常量)必须在定义时赋初值,另外用#define定义的宏常量没有类型。
2)变量
Java和C中变量的定义基本相同,即:
数据类型变量名[ = 变量初值];
变量可赋初值也可不赋,但Java中长整型和浮点型数后要加相应识别标志(如l、f)。
特别注意:由于编译器的不同,C申明变量要放在可执行语句之前,否则可能发生编译错误。
5、逻辑运算符和位运算符
C和Java中都有的逻辑运算符&&、||、!三种,且意义相同,区别在于C中运算结果为0和非0,Java中只能是ture或false。Java中还存在&、|、^(异或),&和&&、|和||的区别在于前者是非捷径运算符而后者是捷径运算符,即&前后都做判断,&&前为假不做后面的判断,|前后都做判断,||前为真不做后面的判断。^表示两者相同为假。
C和Java中都有的位运算符是:&、|、^、~(取反)、<<(左移)、>>(右移),意义基本相同。负数的右移运算在C中视不同系统有所不同(可能是算术右移也可能是逻辑右移),而Java中>>表示的是算术右移,即最高位填充符号位。Java中逻辑右移(无符号右移)运算符是>>>,采用补码右移,高位添0。
6、数组
C中数组的定义如下:
类型说明符数组名[常量表达式];
定义可与初始化同时进行,如:int a[10] = {0,1,2,3,4,5,6,7,8,9};中括号内的常量可以省略。
Java中数组定义有两种方式:
数据类型数组名[];或 数据类型 []数组名;
定义和初始化可同时进行,如:int []a = {0,1,2,3,4,5,6,7,8,9};
注意Java中数组如果在定义时没有进行初始化,在进行初始化的时候需要先分配内存,即:
数组名 = new 数据类型[常量表达式];
也可在定义同时进行内存分配:
数据类型数组名[] = new 数据类型[常量表达式];
C和Java都不支持变长数组,引用的时候都是 数组名[下标]。区别是:Java的下标范围为0~数组长度-1,不在该范围会抛出数组下标越界异常,而C有效范围也是0~数组长度-1,但下标超出此界不会报错。
多维数组中,数组元素都是按行排列的。
还有一点要注意:C中定义数组不进行初始化则数组元素值是不可预知的,而Java中分配内存而不进行初始化数组中是有默认值的。
7、类、域、方法和全局变量、函数
1)类是C中没有的,Java中类定义如下:
[修饰符] class 类名[extends 父类名][implements 接口名]
其中修饰符可以为以下一个或多个访问修饰符:
abstract:抽象类。
final:最终类。
public:公共类。
2)域(成员变量)和全局变量类比:
Java中域的定义如下:
[修饰符] 类型 成员变量名;
修饰符可选以下一个或多个关键字:
public:公共成员。
protected:本类或同一个包的其他类以及其它包该类的子类可访问。
private:私有成员。
final:常量,确定后不能改变。
static:静态变量。
transient:临时变量。
volatile:备份变量。
各类型成员变量默认初始化为:
整型变量:0
浮点型变量:0.0
布尔型变量:false
字符型变量:空格
类变量:null
C中全局变量定义同一般变量:
[存储类别] 数据类型 变量表列;
其中存储类别可选:
auto:自动变量,当不申明存储类别时隐式默认该值。
static:静态变量。
register:寄存器变量。
extern:外部变量。
3)方法和函数类比:
Java中方法的定义如下:
[修饰符] 返回类型 方法名([参数表列])
修饰符可选以下一个或多个:
public:公共方法。
protected:本类或同一个包的其他类以及其它包该类的子类可访问。
private:私有方法。
abstract:抽象方法,只有方法头没有方法体。
static:静态方法。
存储类别可选:
extern:外部函数。
static:静态函数。
第二部分:实验部分
实验名称:实验二 Java基本程序设计(1)
1. 实验目的:
(1)进一步熟悉命令行和IDE两种方式下java程序开发的基本步骤;
(2)掌握Eclipse集成开发环境下导入Java源程序的过程;
(3)掌握Java语言构造基本程序的数据类型、变量、运算符、各类表达式、输入输出、流程控制的基本语法;
(4)掌握Math类、String类、StringBuilder类的用法。
3. 实验步骤与内容:
实验1 程序互评
通过程序互评我发现同学们都从在或多或少的问题,而且大多同学都是只写了主要程序没有框架,但主框架也是很重要,此次实验也提醒我们写程序的时候要仔细、严谨。
实验2 :
编写包含以下代码片段的java应用程序,输出字符串类对象s3的值。
代码如下:
package sshiyabyi1;
public class helloworld {
public static void main(String[] args) {
String s1="Hello!";
String s2="World";
String s3=s1+s2;
System.out.println(s3);
s3=s1.concat(s2);
}
}
运行如下:
实验3 :
更改实验2中s1、s2、s3为StringBuilder类对象,观察程序运行结果并与实验2结果进行对比,理解String类对象与StringBuilder类对象的区别。
代码如下:
package sshiyabyi1;
public class helloworld {
public static void main(String[] args) {
StringBuilder s1=new StringBuilder("Hello!");
StringBuilder s2=new StringBuilder("World");
StringBuilder builder=new StringBuilder();
builder.append(s1);
builder.append(s2);
System.out.println(builder);
}
}
运行如下:
实验4 :
在命令行方式下调试运行下列程序,理解java应用程序命令行参数的用法。
代码如下:
package project1;
public class Message {
public static void main(String[] args) {
if (args[0].equals("-h")) System.out.print( "Hello");
else if(args[0].equals( "-g")) System.out.print("goodbye,");
for(int i=1;i<args.length;i++)
System.out.print(" "+args[i]);
System.out.println("!");
}
}
运行如下:
实验5 java程序导入
Eclipse环境下导入第3章示例程序InputTest.java步骤:
(1)新建java project如下图:
2) 选择File->import->File ystem->Next,打开文件导入窗口如下图,点击上方Browse选择导入源程序并选中,点击下方Browse选择源程序导入位置为新项目InputTest/src位置后,点击finish完成导入。
(3) 打开InputTest项目src文件夹的缺省包,双击InputTest.java在IDE源程序编辑区打开文件。
(4) 右键单击InputTest.java文件名打开快捷菜单,选择Run as->java application运行此程序,结合程序运行结果,理解代码中Scanner类对象用法,掌握java控制台输入方法。
实验6 文件读写程序测试
按照实验5操作步骤,导入WriteReadFileTest.java示例程序,运行程序并理解程序代码,观察项目文件夹下文件myfile.txt的内容,掌握文件的输入输出操作。
2)myfile.txt内容如下
实验7
按照实验5的操作步骤,导入第3章3-3——3-4示例程序,掌握两个程序循环控制结构的用途
3-3实例
3-4实例
实验8:按照实验5的操作步骤,导入第3章3-5示例程序,理解抽奖概率计算算法
该程序用来计算该公式n*(n-1)*(n-2)*...*(n-k+1)/(1*2*3...*k)。
四.实验总结
本周的内容主要是C语言与Java语言异同的对比,也是我们学习Java的基础内容,由于在学习C语言的时候没有学懂,所以学习Java的时候感觉有点吃力,
但是通过学习老师的课件和Mooc平台上的视频,理解了这一部分的内容,在课本中有很多的实例,通过每一部分的实例分析与对比,了解了各个不同变量的作
用,尤其是在实验七中的两个不同循环语句的应用下运行出不同的结果,让我更清楚程序语言严谨的重要性。