Java系列文章
第1章 绪论
第2章 结构化程序设计
第4章 数组、字符串、向量
第6章 异常处理、递归
1、Java标识符和关键字的概念。
标识符
标识符可以用来表识文件名、变量名、类名和类里的方法名
组成 | |
---|---|
java字母 + java数字 |
注意
1.首字符必须是Java字母
2.所有标识符是区分大小写的
3.除去关键字、false、true和null |
Java字母
A~Z, a~z,下划线“_ ”,美元符号“$”,等
Java数字
0~9,等
标识符
2、Java的数据类型包括哪些?每一种数据类型占用的位数是多少?定义方式是怎样?
每一种数据类型占用的位数(即内存空间)是固定的
不依赖于具体的计算机。
boolean
8位,true、false
char
16位,0~65535
byte
8位,-128~+127
short
16位,-32768~+32767
int
32位
long
64位
float
32位
double
64位
3、Java的算术运算符、关系运算符、布尔逻辑运算符、赋值运算符。
运算符与其他语言类似
我认为特殊的一点在于**,短路规则**
example:
public class logic {
public static void main(String[] args) {
int month = 8;
int day =1;
//条件或,采用短路规则,只判断第一个表达式
if((month==8)||(++day<15))
System.out.println("Month"+month+", day="+day);
//逻辑或,不采用短路规则,两个表达式都计算了
if((month==8)|(++day<15))
System.out.println("Month"+month+", day="+day);
输出结果
4、if、if /else、switch语句的用法。
大致分为两种
1.if…else
if(布尔表达式){
//如果布尔表达式的值为true
}else{
//如果布尔表达式的值为false
}
public class Test {
public static void main(String args[]){
int x = 30;
if( x < 20 ){
System.out.print("这是 if 语句");
}else{
System.out.print("这是 else 语句");
}
}
}
2.if…else if…else
if(布尔表达式 1){
//如果布尔表达式 1的值为true执行代码
}else if(布尔表达式 2){
//如果布尔表达式 2的值为true执行代码
}else if(布尔表达式 3){
//如果布尔表达式 3的值为true执行代码
}else {
//如果以上布尔表达式都不为true执行代码
}
public class Test {
public static void main(String args[]){
int x = 30;
if( x == 10 ){
System.out.print("Value of X is 10");
}else if( x == 20 ){
System.out.print("Value of X is 20");
}else if( x == 30 ){
System.out.print("Value of X is 30");
}else{
System.out.print("这是 else 语句");
}
}
}
5、for、while、do-while语句的用法。
for
for(初始化; 布尔表达式; 更新) {
//代码语句
}
public class Main {
public static void main(String[] args) {
int[] intary = { 1,2,3,4};
forDisplay(intary);
foreachDisplay(intary);
}
public static void forDisplay(int[] a){
System.out.println("使用 for 循环数组");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
}
while
while( 布尔表达式 ) {
//循环内容
}
public class Test {
public static void main(String[] args) {
int x = 10;
while( x < 20 ) {
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}
}
do-while
do {
//代码语句
}while(布尔表达式);
public class Test {
public static void main(String[] args){
int x = 10;
do{
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}while( x < 20 );
}
}
6、break语句、continue语句的用法。
break
public class Main {
public static void main(String[] args) {
int[] intary = { 99,12,22,34,45,67,5678,8990 };
int no = 5678;
int i = 0;
boolean found = false;
for ( ; i < intary.length; i++) {
if (intary[i] == no) {
found = true;
break;
}
}
if (found) {
System.out.println(no + " 元素的索引位置在: " + i);
}
else {
System.out.println(no + " 元素不在数组中");
}
}
}
continue
public class Main {
public static void main(String[] args) {
StringBuffer searchstr = new StringBuffer("hello how are you. ");
int length = searchstr.length();
int count = 0;
for (int i = 0; i < length; i++) {
if (searchstr.charAt(i) != 'h')
continue;
count++;
searchstr.setCharAt(i, 'h');
}
System.out.println("发现 " + count
+ " 个 h 字符");
System.out.println(searchstr);
}
}