第二章:基础语法
1.标识符
Java对各种变量、方法和类等要素命名时使用的字符序列称为标识符。
凡事自己可以起名字的地方都叫标识符,都遵守标识符的规则
Java标识符命名规则:
标识符由字母、下划线“-”、美元符“$”或数字组成。
标识符应以字母、下换线、美元符开头。
Java标识符大小写敏感,长度无限制。
Java标识符选取应注意“因名见意”且不能与java语言的关键字重名。
2.关键字认识
Java中一些赋以特定的含义,用做专门用途的字符串称为关键字(keyword)。
所有java关键字都是小写。
goto和const虽然从未使用但也作为java关键字保留下来。
abstract 、default 、if 、private 、this、
boolean 、do 、implement 、protected 、throw
break 、double 、import 、public 、throws
byte 、 else 、instanceof 、return 、transient
case 、 extends 、int 、short 、try
catch 、final 、interface 、static 、void
char 、 finally 、long 、strictfp 、volatile
class 、float 、 native 、super 、while
const 、for 、new 、switch 、null、
continue 、goto 、package 、synchronized
3.Java变量
Java变量是程序中最基本的存储单元,其要素包括变量名、变量类型和作用域。
Java程序中每一变量都属于特定的数据类型,在使用前必须对其声明,声明格式为:
type varName[=value][{,varName}[=vale]]
例如:
int i = 100;
float f = 12.3f;
double d1, d2, d3=0.123;
String s = “hello”;
从本质上讲变量其实是内存中的一小块区域,使用变量名来访问这块区域,因此,每一个变量使用前必须要先申请(声明),然后必须进行赋值(填充内容),才能使用
程序执行的过程:
1) Load到内存区
2) 找到main方法开始执行
3) 执行过程中内存管理:(new出来的东西:heap 堆存储)(局部变量:stack 栈存储)(静态变量,字符串常量:data segement 数据区)(存放代码: code segment 代码区)
4.Java变量的分类
按被声明的位置划分为:
局部变量:方法或语句块内部定义的变量
成员变量:方法外部、类的内部定义的变量
注意:类外部(与类对应的大括号外)不能有变量声明
按所属的数据类型划分:
基本数据类型变量
引用数据类型变量
例子:
Java变量的分类
public class HelloWorld {
int j=9;
public static void main(String [] args)
{
System.out.println("helloworld!");
}
}
其中:int j=9; 为成员变量
String [] args 、 int i=8; 为局部变量
引申例子:
static int j=9;
public static void main(String [] args)
{
int i=8;
System.out.println("helloworld!");
System.out.println ( j+i);
}
}
成员变量加了static 在main方法中才能用j。
5.数据类型划分
数据类型中包括基本数据类型、引用数据类型。
基本数据类型中包括数值型{整数型(byte, short, int, long)、浮点型(float, double)}、字符型(char)、布尔型(boolean)
引用数据类型中包括类(class)、接口(interface)、数组。
Java各整数类型有固定的表数范围和字段长度,其不受具体操作系统的影响,以保证java程序的可移植性。
Java语言的整型常量默认为int型,声明long型常量可以后加“l”或“L”
与整数类型类似,java浮点类型有固定的表数范围和字段长度,不受平台影响。
Java浮点型常量默认为double型,如要声明一个常量为float型,则需在数字后面加f或F。
例子:
public static void main(String[] args) {
boolean b = true;
int x, y = 9;
double d = 3.1415;
char c1, c2;
c1 = '\u534e';
c2 = 'c';
x = 12;
System.out.println("b=" + b);
System.out.println
("x=" + x + ",y=" + y);
System.out.println("d=" + d);
System.out.println("c1=" + c1);
System.out.println("c2=" + c2);
}
}
基本数据类型转换:
容量小的类型自动转换为容量大的数据类型,顺序如下:
byte,short, char-> int ->long -> float ->double
byte,short, char之间不会互相转换,他们三者在计算时首先转换为int类型
容量大的转换为容量小的数据类型时,要加强制转换符,但可能造成精度降低或溢出,使用时要格外注意。
例子:
public class TestConvert {
public static void main(String arg[]) {
int i1 = 123;
int i2 = 456;
double d1 = (i1+i2)*1.2;//系统将转换为double型运算
float f1 = (float)((i1+i2)*1.2);//需要加强制转换符
byte b1 = 67;
byte b2 = 89;
byte b3 = (byte)(b1+b2);//系统将转换为int型运算,需
//要强制转换符
System.out.println(b3);
double d2 = 1e200;
float f2 = (float)d2;//会产生溢出
System.out.println(f2);
float f3 = 1.23f;//必须加f
long l1 = 123;
long l2 = 30000000000L;//必须加l
float f = l1+l2+f3;//系统将转换为float型计算
long l = (long)f;//强制转换会舍去小数部分(不是四舍五入)
}
}
public class TestConvert2 {
public static void main(String[] args) {
int i=1,j=12;
float f1=(float)0.1; //0.1f
float f2=123;
long l1 = 12345678,l2=8888888888L;
double d1 = 2e20,d2=124;
byte b1 = 1,b2 = 2,b3 = 127;
j = j+10;
i = i/10;
i = (int)(i*0.1);
char c1='a',c2=125;
byte b = (byte)(b1-b2);
char c = (char)(c1+c2-1);
float f3 = f1+f2;
float f4 = (float)(f1+f2*0.1);
double d = d1*i+j;
float f = (float)(d1*5+d2);
}
}
6.运算符
Java运算符如下
算数运算符:+,-,*,/,%,++,--
关系运算符:>, <, >=, <=, = =, !=
逻辑元算符:!, &,|,^,&&,||
赋值运算符:=
扩展赋值运算符:+=,-=,*=,/=
字符串连接运算符:+
7.表达式
表达式是符合一定语法规则的运算符合操作数的序列
a
5.0+a
(a-b)*c-4
i<30 && i%10!=0
表达式的类型和值:
对表达式中操作数进行运算得到的结果称为表达式的值
表达式值的数据类型即为表达式的类型。
表达式的运算顺序:应按照运算符的优先级从高到低的顺序进行
三目条件运算符:
x ? y : z
其中x为boolean类型的表达式,先计算x的值,若为ture,则整个三目运算符的结果为表达式y的值,否则整个表达式为z的值
8.条件语句,循环语句
条件语句根据不同条件执行不同语句。
if
if .. else
if .. else if
if .. else if…else if.. else
switch
循环语句重复执行某些动作
for
while
do .. while
例子:
public class TestIF {
public static void main(String[] args) {
int i = 20;
if(i < 20) {
System.out.println("<20");
System.out.println("<20");
} else if (i < 40) {
System.out.println("<40");
} else if (i < 60) {
System.out.println("<60");
} else {
System.out.println(">=60");
}
System.out.println(">=60");
}
}
用for循环计算1+3+5+7+……+99的值,并输出计算结果。
public class OddSum {
public static void main(String[] args) {
long result = 0;
for(int i=1; i<=99; i+=2) {
result += i;
}
System.out.println("result=" + result); //result=2500
}
}
例子使用while、do .. while:
public static void main(String[] args) {
int i = 0;
while(i < 10) {
System.out.print(i);
i++;
}
i = 0;
do {
i++;
System.out.print(" ");
System.out.print(i);
} while(i < 10); //while后面有分号;
}
//0123456789 1 2 3 4 5 6 7 8 9 10
}
break语句用于终止某个语句块的执行。用在循环语句中,可以强行退出循环,
continue语句用在循环语句体中,用于终止某次循环过程,跳过循环体中continue语句下面未执行的循环,开始下一次循环过程。
输出1~100内前5个被3整除的数
public class TestZhengChu {
public static void main(String []args){
int mun=0, i=1;
while (i<=100){
if (i%3==0){
System.out.print(“ ”+i);
mun++;
}
if (mun==5){
break;
}
i++;
}
}
}
// 3 6 9 12 15
输出101~200内的质数
public class ZhiShu {
public static void main(String [] args){
int i,j;
for ( i=101;i<=200;i+=2){
boolean m = true;
for ( j=2;j<i;j++ ){
if (i%j==0){
m = false;
break;
}
}
if (!m){
continue;//跳出当次循环
}
System.out.print(" "+i);
}
}
}
// 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199
switch语句
例子:
public class TestSwitch {
public static void main(String[] args) {
int i = 8;
switch(i) {
case 8 :
case 3 :
case 2 :
System.out.println("C");
break;
case 9 :
System.out.println("D");
break;
default:
System.out.println("error");
}
}
} //c
switch语句中注意:小心case穿透,推荐使用break语句,多个case可以合并到一起,default可以省略,但不推荐省略,switch语句中只能探测int类型值。
9.程序方法
Java的方法类似于其他语言的函数,是一段用来完成特定功能的代码片段,声明格式:
[修饰符1 修饰符2 …] 返回值类型方法名(形式参数列表){
Java语句:… …
}
形式参数:在方法被调用时用于接收外界输入的数据
实参:调用方法时实际传给方法的数据
返回值:方法在执行完毕后返还给调用它的环境的数据
返回值类型:事先约定的返回值的数据类型,如无返回值,必须给出返回值类型void。
return语句终止方法的运行并指定要返回的数据
java中进行函数调用中传递参数时,遵循值传递的原则:
基本数据类型传递的是该数据值本身,引用数据类型传递的是对对象的引用,而不是对象本身。
public class TestMethod {
public static void main(String[] args) {
m();
m2(2);
m3(3, 4);
m4(4, 6);
int i = m4(4, 6);
System.out.println(i);
}
public static void m() {
//return;
}
public static void m2(int i) {
if(i > 3)
return;
System.out.println(i);
}
public static void m3(int i, int j) {
System.out.println(i + j);
}
public static int m4(int i, int j) {
return i > j ? i : j;
}
}
// ok
//hello
//2
//7
//6