基础语法点
变量 实质是内存里的一小块区域,通过变量名来访问这一区域的值;
变量使用前要先声明,再赋值,才能使用。
局部变量与成员变量
public class hello {
//int j;成员变量:类内部,函数外部
public static void main(String args[]){
//int i;局部变量:方法内部变量和方法参数
System.out.println("Hello world!");
}
}
凡是在大括号里声明的变量,出了大括号就不会有人认识它。
public class TestVar {
static int j =0;//
public void m(){
int i = 0;
System.out.println(i);
}
public static void main(String[] args){
int i = 0;
System.out.println(i);
System.out.println(j);
boolean c = false;
if(c){
int d = 0;
System.out.println("c is true");
}
//出了d所在的if这个大括号,d值无法使用
//System.out.println(d);
}
}
四种基本数据类型
//四种基本数据类型
public class TestVar2 {
public static void main(String[] args){
int x,y=9;//此时x未赋初值,默认为0
x = 12;
double c = 2.1234;
char ch = '\u534e';//在计算机内部的表示————0101 0011 0100 1110
char ch2 = 'a';
boolean b = true;
System.out.println(x);
System.out.println(y);
System.out.println(c);
System.out.println(ch);
System.out.println(ch2);
System.out.println(b);
}
}
类型转换例子
public class TestConvert {
public static void main(String[] args){
int l1 = 12;
int l2 = 13;
double d = (l1+l2)*1.2;//此时系统将转换为double类型
float f = (float)d;//转换为float需要强制转换符
byte a = 123;
byte b = 6;
byte c = (byte)(a+b);//需要强制转换符(int->byte)
//a+b运算时系统转换为int,溢出时去掉后面三个字节
System.out.println(c);
float f1 = 1.23f;//定义后加f
long m = 8888888888888L;//long类型使用时后面必须加L来识别
System.out.println(m);
float s = f1 + m;//系统自动转换为float
long L2 = (long)s;
double d2 = 1e200;
float f4 = (float)d2;//溢出 浮点数类型特殊
System.out.println(f4);
}
}
//类型转换例子
public class TestConvert2 {
public static void main(String[] args){
int i = 1,j=1;//j赋初值后使用
float f1 = 0.1f;
//float f1 = float(f1) double->>float
float f2 = 123;
long l1 = 12345678,l2 = 8888888888L;
double d1 = 2e20;
double d2 = 124;
byte b1 =1,b2=2,b3=127;
j = j+10;
i = i/10; //int /int -->int (0.1)==0
//System.out.println(i);
i = (int) (i*0.1);
System.out.println(i);
char c1 = 'a',c2 = 125;
byte b = (byte)(b1-b2);//int->>byte
char c = (char)(c1+c2-1);//int ->>char
float f3 =f1+f2;
float f4 = (float)(f1 + f2*0.1);//double->float
double d = d1*i+j;
float f = (float)(d1*5+d2);
}
}
自加自减运算符
public class TestAdd {
public static void main(String[] args){
int a = 10;
int b = 12;
int c = (a++);
//先使用a的值,再++
System.out.print("a="+a);//11
System.out.println("c="+c);//10
int d = (a--);
System.out.print("a="+a);//10
System.out.println("d="+d);//11
int e = (++a);//a自增,同时赋给e
System.out.print("a="+a);//11
System.out.println("e="+e);//11
int f = (--a);
System.out.print("a="+a);//10
System.out.println("f="+f);//10
}
}
+运算符两侧只要有一个是字符串类型,系统自动将两侧都转换为字符串类型进行连接;当进行打印时,无论任何类型都自动转换为字符串类型,再打印
int c = 12;
System.out.println(“c=”+c);
//result = 1!+2!+...+10!
public class TestFor {
public static void main(String[] args){
long result = 0;
long f = 1;
for(int i =1;i<=10;i++){
f *= i;
result+=f;
}
System.out.println("result="+result);
}
}
while循环例子
public class TestWhile {
public static void main(String[] args){
//while 0~9
int i = 0;
while(i<10){
System.out.println(i);
i++;
}
//do while 0~9
do{
System.out.println(i);
i++;
}while(i<10);
//do while 1~10
do{
i++;
System.out.println(i);
}while(i<10);
}
}