基本和JavaScript类似。有前端基础的同学很快就能上手写东西了。
基本数据类型:8种
- 数字类型:byte(8位 -128~127)、short(16位 -32768~32767)、int(32位...)、long(64位...)、float(单精度32位)、double (双精度64位)
- 布尔型:boolean
- 字符类型:char
int a, b, c; // 声明三个int型整数:a、 b、c
int d = 3, e = 4, f = 5; // 声明三个整数并赋予初值
byte z = 22; // 声明并初始化 z
String s = "runoob"; // 声明并初始化字符串 s
double pi = 3.14159; // 声明了双精度浮点型变量 pi
char x = 'x'; // 声明变量 x 的值是字符 'x'。
引用类型:
- 数组:int[] array = {1,2,3,4,5,6} array[0]
- 对象:Site site = new Site("Runoob") site.method()
常量:(用final 修饰符)
final double PI = 3.1415927;
常用运算符
- 算术:+ 加 -减 *乘 /除 %取余 ++自增 --自减
- 关系:==相等 !=不等 >大于 >=大于等于 <小于 <=小于等于
- 逻辑:&&与 ||或 !非
- 赋值:= 把右边的值赋值给左边
循环(break跳出循环体、continue跳到下一次循环的迭代)
- while
int x = 10;
while( x < 20 ) {
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}
- do while
int x = 10;
do{
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}while( x < 20 );
- for
for(int x = 10; x < 20; x = x+1) {
System.out.print("value of x : " + x );
System.out.print("\n");
}
条件语句
- if
int x = 10;
if( x < 20 ){
System.out.print("这是 if 语句");
}
- if else
int x = 30;
if( x < 20 ){
System.out.print("这是 if 语句");
}else{
System.out.print("这是 else 语句");
}
- if...else if...else
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 语句");
}
网友评论