Fork me on GitHub

JAVA基础

2.Java基础

1.注释、标识符、关键字

53个关键字中包含两个保留字

注释就类似笔记,对代码进行解释。并不会被执行。书写注释是一个非常好的习惯。

注释

 // 单行注释
 /* */ 多行注释
 /** */ 文档注释

标识符、关键字

Java所有的组成部分都需要名字。类名、变量名以及方法名等都被称为标识符。

以下为关键字

abstractassertbooleanbreakbyte
case catch char class const
continue default do double else
enum extends final finally float
for goto if implements import
instanceof int interface long native
new package private protected public
return strictfp short static super
switch synchronized this throw throws
transient try void volatitle while

所有的标识符都应该以字母,美元符号¥,或者下划线_开头。

不能使用上述的关键字作为变量名或者方法名。

标识符区分大小写。

可以使用中文命名,但是不建议使用。拼音也尽量避免。

image-20200804120106869

 

命名规范

2.数据类型

java是一种强类型语言,就是变量严格符合规定,先定义再使用。

java的数据类型分为两大类

  • 基本类型(primitive type):int、byte、short、long、float、double、char、boolean

  • 引用类型(reference type)

  • image-20200622220356464

  •  

    基本数据类型

         //八大基本数据类型
         int num = 100;//最常用,占用4个字节
         byte num2 = 20;//占用1个字节
         short num3 = 30;//占用2个字节
         long num4 = 30L;//long类型在数字后面加L,用以区分,占用8个字节
 
         //小数、浮点数
         float num5 = 50.1F;//Float类型药在数字后面加F,占用4个字节
         double num6 = 3.14159265358979323;//占用8个字节
 
         //字符
         char name = 'A';//占用2个字节
         //字符串,String不是关键字,是类
         String name1 = "RyanPeng";
 
         //布尔值
         boolean flag = true;//占用一个字节

image-20200613185058356

32位与64位的区别

引用类型

类、接口、数组

 

3.数据类型扩展及面试题讲解

进制的转换用短除法,转成十进制类似10的次方的计算,要记住。

整数拓展

 //整数拓展: 进制      二进制0b  十进制    八进制       十六进制0x
         int i = 10;
         int i2 = 010; //八进制0
         int i3 = 0x10; //16进制0x   0~9 A~F 16
 10
 8
 16

浮点数拓展

         //浮点数拓展  银行怎么表示钱?
         float f = 0.1f; //0.1
         double d = 1.0/10; //0.1
         System.out.println(f==d);
 
         float d1 = 12314521324f;
         double d2 = d1 + 1;
         System.out.println(d1==d2);
 false
 true

为什么会造成上述情况

  • 因为float 是有限的,是离散的,有舍入误差,接近但是不等于

  • 最好完全避免使用浮点数进行比较

那么银行中怎么表示钱呢?

  • 使用 BigDecimal数学工具类

 

字符类拓展

         char c1 = 'a';
         char c2 = '中';
 
         System.out.println(c1);
         System.out.println(c2);
         System.out.println((int)c1);//强制转换
         System.out.println((int)c2);
 a
 
 97
 20013
  • 所有的字符本质还是数字

  • 编码是 Unicode 占用两个字节 0-65536,查表可以发现a=97,中=20013

 char c3 = '\u0061';//Unicode的标准写法
 System.out.println(c3);
 a

转义字符:

  • \t 制表符

  • \n 换行符

         String sa = new String("hello world");
         String sb = new String("hello world");
         System.out.println(sa==sb);
 
 
         String sc = "hello world";
         String sd = "hello world";
         System.out.println(sc==sd);
 false
 true

以上内容要通过对象和内存两方面分析

 

布尔值拓展

 boolean flag = ture;
 if(flag==true){}
 if(flag){}
 // less is more 代码要精简易读,上述两种表示方法同等

4.类型转换

java是一种强类型语言

运算中,不同类型的数据先转化为同一类型,再进行运算。(基本上是按照字节数来转换的)

image-20200621211753892

 

         int i = 127;
         byte b = (byte)i;//要非常注意内存溢出的情况
         //强制转换
         System.out.println(i);
         System.out.println(b);
 127
 127

强制转换:高-->低

自动转换:低-->高

注意点

  • 不能对布尔值进行转换

  • 不能把对象类型转换为不相干的类型

  • 在把高容量转换到低容量的时候,强制转换

  • 转换的时候内存可能溢出,或者精度问题


 

         int money = 10_0000_0000;
         int years = 20;
         System.out.println(money);
         int total = money*years;
         long total2 = money*years;
         System.out.println(total);
         System.out.println(total2);
         long total3 = money*((long)years);
         System.out.println(total3);

 

 1000000000
 -1474836480
 -1474836480
 20000000000

5.变量、常量、作用域

变量

  • 每个变量都必须声明类型

  • 类型可以是基本类型,也可以是引用类型

  • 变量名必须是合法的标识符

         String name = "TongTong";
         char x = 'X';
         double pi = 3.14;

作用域

  • 类变量

  • 实例变量

  • 局部变量

 public class Demo04 {
 
     //类变量 static
     static  double salary = 5000;
 
     //属性:变量
 
     //实例变量:从属于对象;如果不进行初始化,这个类型的默认值为0,0.0
     //布尔值默认是false
     //除了基本类型,其余的默认值都是null
     String name;
     int age;
     //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);
 
    }
 
     //其他方法
     public void add(){
 
    }
 
 }
 

结果如下:

image-20200616204201672

内存: 局部变量存放在栈内存

成员变量存放在堆内存

声明的位置:

成员变量(属性)在类里声明

局部变量:方法内、形参、代码块内

 

常量:一旦定义不会改变。

 public class Demo05 {
     //修饰符不存在先后顺序
     static final double PI = 3.14;
 
     public static void main(String[] args) {
         System.out.println(PI);
    }
 }
 

结果就是 3.14

命名规范

image-20200616204457111

 

6.基本运算符

image-20200616204600442

 public class Calculate{
  public static void main(String[] args){
  int a = 5;
  System.out.println(((a++)+1+(++a)));//13 此时 a=7
  }
 }
 //自己分析
 public class Calculate{
  public static void main(String[] args){
  int a = 5;
  System.out.println(((a++)+(a--))*(++a));//66
  }
 }
 //自己分析
 public class Calculate{
  public static void main(String[] args){
  int a = 5;
  System.out.println(((a++)+(a--))*(++a));//7
  }
 }
 //自己分析

 

 public class Demo01 {
     public static void main(String[] args) {
         //二元运算符
         //idea操作 Ctrl + D 复制当前行到下一行
         int a = 10;
         int b = 20;
         int c = 25;
         int d = 25;
 
         System.out.println(a+b);
         System.out.println(a-b);
         System.out.println(a*b);
         System.out.println(a/b);
 
    }
 }

image-20200616205313627

所以一定要对数据的范围处理,a/b=0.5,整型四舍五入=0,应该写成 a/(double)b

 

 package operator;
 
 public class Demo02 {
     public static void main(String[] args) {
         long a = 123156412315646L;
         int b = 123;
         short c = 10;
         byte d = 8;
 
         System.out.println(a+b+c+d);//返回long
         System.out.println(b+c+d);//返回int
         System.out.println(c+d);//返回int
    }
 }
 

 

关系运算符

 public class Demo03 {
     public static void main(String[] args) {
         //关系运算符返回结果:正确,错误,布尔值
         int a = 10;
         int b = 20;
         int c = 21;
 
         System.out.println(a>b);
         System.out.println(a<b);
         System.out.println(a==b);
         System.out.println(a!=b);
         System.out.println(c%a);
    }
 }

image-20200616211932547

     public static void main(String[] args) {
         // ++ -- 自增,自减,一元运算符
         int a = 3;
         int b = a++;//执行完这行代码,先给b赋值,a再自增
         int c = ++a;//先自增,再赋值
 
         System.out.println(a);
         System.out.println(b);
         System.out.println(c);
    }

image-20200616213847404

所以a的值也改变了

 //幂运算
         double pow = Math.pow(3, 2);
         System.out.println(pow);//结果为9.0

逻辑运算符

     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));
    }

image-20200616214604083

         //短路运算
         int c = 5;
         boolean d = (c<4)&&(c++<4);
         System.out.println(c);
         System.out.println(d);

image-20200616214750122

并没有执行自增操作,因为第一个条件c<4已经不满足了,所以后面的代码页不会执行。

位运算(转换成8位二进制进行计算,规则如下)

大哥,我求求你把原码,反码,补码都牢牢记在心里面

正数的原码和补码相同。

负数的补码等于反码+1。

负数的第一位为符号位,存储在计算机中用的是补码,先把原码转化为反码,再把反码转化为补码。转换的过程中,符号位不变。

已知一个数的补码,求原码的操作分两种情况: (1)如果补码的符号位为“0”,表示是一个正数,所以补码就是该数的原码。 (2)如果补码的符号位为“1”,表示是一个负数,求原码的操作可以是:符号位为1,其余各位取反,然后再整个数加1。

按位与运算符(&)0&0=0 0&1=0 1&0=0 1&1=1
按位或运算符(| 0|0=0 0|1=1 1|0=1 1|1=1
异或运算符(^) 0^0=0 0^1=1 1^0=1 1^1=0
取反运算符 (~) ~1=0 ~0=1
左移运算符(<<) 设 a=1010 1110,a = a<< 2 将a的二进制位左移2位、右补0,即得a=1011 1000。
右移运算符(>>) a=a>>2 将a的二进制位右移2位,左补0 或者 左补1得看被移数是正还是负。
复合赋值运算符 &=例:a&=b 相当于a=a&b >>=例:a>>=b 相当于a=a>>b 等
右移运算符(>>>) 与>>相同,但是>>>不计算符号
     public static void main(String[] args) {
         /*
         A = 0011 1100
         B = 0000 1101
 
         A&B = 0000 1100
         A|B = 0011 1101
         A^B = 0011 0001
         ~B = 1111 0010
          */
 
         /*
         0000 0000 0
         0000 0001 1
         0000 0010 2
         0000 0011 3
 
         << *2 左移相当于*2
         >> /2 右移相当于/2
          */
         System.out.println(2<<3);//结果为16
    }
 }

三元运算符

 public class Demo07 {
     public static void main(String[] args) {
         int a = 10;
         int b = 20;
 
         //字符串连接符 +
         System.out.println(a+b);
         System.out.println(""+a+b);
         System.out.println(a+b+"");
    }
 }

结果

 30 //输出值为 (a+b)的值
 1020   //""在前面是一个字符串,将a和b拼接起来
 30 //""在后面,先计算

 

x ? y : z

 public class Demo08 {
     public static void main(String[] args) {
         // x ? y : z
         //如果 x==true,则结果为y,否则为z
 
         int score = 80;
         String type = score <60 ?"不及格":"及格";
         System.out.println(type);//输出结果为 : 及格
    }
 }
         int a = -15;        
  System.out.println(a>>2);//-4
  System.out.println(a>>>2);//1073741820

多打括号是一个好的习惯。

7.包机制

包的本质就是文件夹,一般用公司的域名倒置作为文件名。

有包的类 package必须放在最上面。

如果需要引用某一个包的成员,需要用import语句明确引入该包。

e-g import com.kuangshen.base.* //*是通配符,表示所有的文件。

8.JavaDoc生成文档

image-20200617222759261

即在类名 方法名前面 /**+Enter

 

生成文档 javadoc 参数 java文件

image-20200617224139596

之后在打开的资源的文件管理器前面加上 cmd 打开cmd窗口,之后运行如下代码。

image-20200617223650351

之后会在文件夹下生成一些文件,点开index.html即可查看文档。

利用IDEA生成

参考

 

[以上内容均学自B站狂神](https://space.bilibili.com/95256449)

posted @ 2020-09-11 19:18  一直在努力的小白  阅读(115)  评论(0)    收藏  举报