单行注释//
多行注释/* */
文档注释/** */
标识符大小写敏感
3.整数型 byte short int long
浮点型 float double
字符型 char (字符代表的是一个字,两个及多个就不对)
字符串 string (它不是关键字,它是一个类)
布尔值 boolean
long num = 90L;
float num1 = 5.1F;
char name = 'a';
string name1 = "容辞";
boolean flag = true;
4.1bit(1位)
1Byte(一个字节)
8b = 1B
1024B = 1KB
1024KB = 1M
1024M = 1G
1024G = 1TB
5.转义字符
\t 制表符 \n 换行
1.int
int i = 10;
int i1 = 010;(八进制0)
int i2 = 0b10;(二进制0b)
int i3 = 0x10;(十六进制0x)
2.float/double
float f = 0.1f;
double d = 1.0/10;
System.out.println(f == d); //false
float f1 = 2333333333f;
float f2 = f1+1;
System.out.println(f1 == f2); //true
float 有限 大约 舍入误差
银行业务不能用浮点型表示!可以用类BigDecimal
结论:少用浮点型进行比较
3.char
char c1 = 'a';
char c2 = '中';
System.out.println(c1);
System.out.println((int)c1); //强制转换类型
System.out.println(c2);
System.out.println((int)c2); //强制转换类型
char c3 = '\u0061'; //unicode
System.out.println(c3);
所有字符型本质上都是数字
这里有个unicode编码 u0000—uFFFF
4.boolean
boolean flag = true;
if (flag == true){
} //新手
if (flag){
} //老手
//less is more!
高低顺序:
char,short,byte—>int—>long—>float—>double
int i = 128;
byte b = (byte)i; //强制转换--->从高到低
System.out.println(i);
System.out.println(b); //输出b=-128--->内存溢出
int i = 128;
double b = i; //自动转换--->从低到高
System.out.println(i);
System.out.println(b);
System.out.println((int)23.77); //23
System.out.println((int)-45.5789f); //-45
char c = 'a';
int t = c+1;
System.out.println(t); //98
System.out.println((char)t); //b
注意事项:
布尔型不能进行转换
转换的时候可能存在内存溢出或精度问题
int money = 10_0000_0000; //可在数字中加入下划线
System.out.println(i); //1000000000
int year = 20;
int total = money*year;
long total1 = money*(long)year;
System.out.println(total); //溢出
System.out.println((long)total); //溢出
System.out.println(total1); //20000000000
1.实例变量:它在类内方法外
实例变量如果不自行初始化,它的默认值为0 0.0 u0000
布尔值默认false
除了基本类型外其余默认为null
2.类变量(static)
3.局部变量:局部变量必须声明和初始化值
public class Helloworld {
String name; //实例变量:从属于对象
int age;
static double salary = 25000; //类变量
public static void main(String[] args) //mian方法
int a = 1; //局部变量
System.out.println(a);
//变量类型+变量名字 = new Helloworld()
Helloworld Helloworld = new Helloworld(); //实例变量
System.out.println(Helloworld.name);
System.out.println(salary); //类变量
}
}
1.变量命名: 见名知意
2.类成员变量: 首字母小写,除了第一个单词后面单词首字母大写(驼峰原则)
3.常量名:全是大写字母或者下划线
4.类名:首字母大写,驼峰原则(新建文件命名)
一种特殊的变量,它的值被设定后便不可以改变!
final 常量名 = 常量值
常量名一般用大写字符
public class Helloworld {
static final double PI = 3.14; //常量
public static void main(String[] args) {
System.out.println(PI);
}
}
ctrl+D 复制当前行到下一行
当使用除法的时候会出现小数一定要注意数据类型,要把整数强制转化为小数
整数型进行运算的时候,当有long参与,结果为long型;无long参与,结果全为int。(double同理)
与或非 &&,||,!
int a =10;
int b = a++; //先给b赋值,然后a在自增
(隐藏了a=a+1)
System.out.println(a); //a=11
(隐藏了a=a+1)
int c = ++a; //a先自增,再给c赋值
System.out.println(a); //a=12
System.out.println(b); //b=10
System.out.println(c); //c=12
double pow = Math.pow(2,3); //Math类
System.out.println(pow); //8.0
&& || !
boolean a = true;
boolean b = false;
System.out.println("a&&b:"+(a&&b));
System.out.println("a||b:"+(a||b));
System.out.println("!(a&&b):"+(!(a&&b)));
短路原则:当执行与运算时,如果前面的已经为flase那么根本没有执行与运算。
int c = 5;
boolean d = (c<=4)&&(c++<=4);
System.out.println(d); //flase
System.out.println(c); //c=5
& | ~ (非) ^
int a = 0b1000;
int b = 0b1101;
System.out.println(a&b); //与运算 1000 & 1101 = 1000
System.out.println(a|b); //或运算 1101
System.out.println(a^b); //异或运算 0101 相同就取0,不同就取1
<< >>
System.out.println(2<<3); //16 << 代表乘2
System.out.println(8>>2); //2 >> 代表除2
a+=b a=a+b
x ? y : z 如果x==true,则执行y;否则执行z
int score = 80;
String type = score<60?"不及格":"及格";
System.out.println(type); //及格
字符串连接符 +
int a = 10;
int b = 20;
System.out.println(""+a+b); //1020
System.out.println(a+b+""); //30