1. 数据类型
强类型语言,变量必须定义才能使用
public class Demo02 { public static void main(String[] args) { //八大基本数据类型 int num = 1; byte num2 = 127; short num3 = 30; long num4 = 30L; //Long类型需要在数字后面加个L //小数:浮点数 float num5 = 50.1F; double num6 = 3.141592623289793238462643; char name = 'A'; String namea = "雷恩加尔"; //布尔值 boolean flag = true; boolean fflaga = false; } }
2. 注释模式
//单行注释 /*多行注释*/ 文档注释
package Study; /** * @author Administrator * @version 1.0 * @since 1.8 * * */ public class Demo08 { String name; /** * * @param name * @return * @throws Exception */ public String test(String name) throws Exception{ return name; } }
3. 进制
//整数扩展 进制 二进制0b 十进制 八进制0 十六进制0x
4. 浮点数拓展
//面试题 //浮点数拓展 银行业务怎么表示?钱 float f = 0.1f //0.1 double d = 1.0/10 //float 有限 离散 舍务误差 大约 接近但是不等于 double //最好完全使用浮点数进行比较
5 . 字符拓展
char c1 = 'a'; char c2 = '中'; System.out.println(c1); System.out.println((int)c1);//强制转换 System.out.println(c2); System.out.println((int)c2);//强制转换 /* a 97 中 20013 */ //转义字符 \n \t \u 。。。 //初级 if (flag==true){} //高级 if (flag){} //面试体:所有字符的本质还是数字 //编码unicode 2字节 a = 97 A = 65 0-65536(2^16) //U0000 ~ UFFFF char c3 = ''\u0061' System.out.println(c3); -->a
6.运算中不同类型的数据先转化同一类型然后进行运算
数据类型——》byte 1个字节 short 2个字节 int 4个字节 long 8 个字节
浮点类型——》float 4个字节 double 8个字节
字符类型——》char 2个字节
boolean类型》 true 和 false 1个字节
public class Demo03 { public static void main(String[] args) { int i = 1; //强制转换 高——》低 byte b = (byte)i; //自动转换 低——》高 double c = i; /*注意点: * 1.不能对布尔类型进行转换 * 2.不能把对象类型转换为不相干的类型 * 3.把高容量转换为低容量时为强制转换 * 4.转换的时候可能存在内存溢出,或者精度问题!*/ System.out.println((int)23.7); //23 System.out.println((int)-45.89f);//-45 //大数操作容易产生内存溢出问题 //可使用下划线分割数字 int money = 10_0000_0000; int years = 20; int total = money * years; System.out.println(total); //-1474836480 内存溢出 long total2 = money*years;//-1474836480 默认是int,在转换之前已经存在问题了 long total3 = money*((long)years); System.out.println(total3);//20000000000 } }
7.变量
-
命名规则
Java中每一个变量都必须声明其类型
-
类变量
-
实例变量
public class Demo04 { //类变量 static static double salary = 2500; //实例变量:从属于对象 不自行初始化。这个类型的默认值为0 0.0 00000 //布尔默认值为false //除了基本类型其他全都是null String name; //null int age; //0 //main方法 public static void main(String[] args) { //局部变量:必须声明和初始化值 int i = 10; System.out.println(i); //变量类型 变量名字 = new Demo04 Demo04 demo04 = new Demo04(); System.out.println(demo04.age); System.out.println(demo04.name); //类变量 static System.out.println(salary); //2500.0 } //其他方法 public void other(){ } }
-
常量(全大写加下划线分割)
public class Demo05 { //常量 final double PI = 3.14; static final int SH_XX = 12; // == final static int sh = 12; public static void main(String[] args) { } }
8.运算符
public class Demo06 { //逻辑运算符 public static void main(String[] args) { // 与(and) 或(or) 非(!) 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)); //非运算取反 //短路运算 System.out.println("a && b "+(a&&b)); System.out.println("b && a "+(b&&a)); //当解释器运算到b为false时便会输出结果 false 不在运算 &&a 速度和资源的节约大于上式 } }
public class Demo07 { //位运算符 public static void main(String[] args) { /** * A = 0011 1100 * B = 0000 1101 * * A&B 0000 1100 A与B 都1为1 * A|B 0011 1101 A或B 有一为1 * A^B 0011 0010 A异或 相同为0 不同为1 * ~B 1111 0010 面试题 2*8 怎么样最快 2*8 = 16 2*2*2*2 >> << 位运算符 */ System.out.println(2<<3);//8 = 2**3 2往左移动3位 为8 System.out.println(2>>1);//1 2/2 } }
!!!!!!!!!!!!!!!!!! public class Demo07 { //三元运算符 条件 ? "结果一":“结果2”; 符合条件输出结果一,不满足条件输出结果二 public static void main(String[] args) { int score = 50; String type = score < 60 ? "不及格":"及格"; // 不及格 System.out.println(type);
9.包机制
本质:文件夹
一般利用公司域名倒置作为包名
package pk1[.pk2[.pk3]]
import导入包
javadoc -encoding UTF-8 Demo08.java