JAVA基础理论

时间:2021-08-23 19:35:52   收藏:0   阅读:13

JAVA基础理论

注释、标识符、关键字

1. Java的三种注释

2. 标识符、关键字

标识符 + 名字 = 赋予的值

12345
abstract assert boolean break byte
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 volatile while

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


数据类型

1. 强类型语言

安全性高 速度较慢

2. 弱类型语言

 

3. Java数据类型

基本数据类型

数值类型

1. 整数类型
2. 浮点类型
3. 字符类型

引用数据类型

// 不是基本数据类型的都是 引用数据类型


类型转换

低 ====================================高

byte , short , char => int => long => float => double

强制转换 (类型)变量名 高 -- 低

自动转换 低 -- 高

public class Demo04 {
   public static void main(String[] args) {
       int a = 128;
       double b = a;
       System.out.println(a);
       System.out.println(b);
       /*
        不能对布尔值进行转换
        不能把对象转换为不相干的类型
        把高容量的转换为低容量的时候,强制转换反之自动转换
        转换的时候可能存在内存溢出,或者精度问题
        */
       System.out.println("===========================");
       System.out.println((int)23.75); // 23
       System.out.println((int)-28.45); // -28
       
       System.out.println("=======================");
       char q = ‘A‘;
       int w = q + 1;
       System.out.println((int)q);
       System.out.println(w);
       System.out.println((char)w);
?
       System.out.println("=======================");
       //跨度比较大的时候 注意溢出问题
       //数字之间可以用下划线
       int money = 1_000_000_000;
       int years = 20;
       System.out.println(money * (long)years);
  }
}

变量

 

变量的作用域

public class Demo06 {
   //类变量 static
   static double salary =2500;
   public static void main(String[] args) {
    //类变量 static
       System.out.println(salary);
  }
}
public class Demo06 {
String name;
   int age;
   public static void main(String[] args) {
   //变量类型 变量名称 = new Demo06()
   Demo06 demo06 = new Demo06();
   System.out.println(demo06.name);
   System.out.println(demo06.age);
  }
}
public class Demo06 {
public static void main(String[] args) {
    //局部变量 必须声明初始化值
    int i = 10;
    System.out.println(i);
}
}

常量

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

变量的命名规范

运算符

名称运算符
算术运算符 +,-,*,/,%,++,--
赋值运算符 =
关系运算符 >,<,<==,>==,==,!=,instanceof
逻辑运算符 &&,||,!
位运算符 &,|,^,~,>>,<<,>>>
条件运算符 ?,:
扩展赋值运算符 +=,-=,*=,/=

算数运算符

public class Demo02 {
   public static void main(String[] args) {
   //++ --     自增,自减,一元运算符
   int a = 3;
   int b = a++;//执行完这行代码后先给b赋值再自增
   //a = a + 1
   int c = ++a;//执行完这行代码后先给c赋值再自增
   //a = a + 1
   System.out.println(a);
   System.out.println(b);
   System.out.println(c);
   //幂运算 2^3   2*2*2 = 8 很多运算会是使用工具类
   double pow = Math.pow(2,4);
       System.out.println(pow);
  }
}

逻辑运算符

public class Demo03 {
       public static void main(String[] args) {
            // 与 (and) 或 (or) 非(取反)
           boolean a = true;
           boolean b = false;
           System.out.println("a&&b:"+(a&&b)); //逻辑与运算 两者都为真,结果才为true
           System.out.println("a||b:"+(a||b));//逻辑或运算 两者有一个为真,则结果为true
           System.out.println("!(a&&b):"+!(a&&b));//如果为真则为假,如果为假则为真
           //短路运算测试
           int c = 5;
           boolean d = (c<4)&&(c++<4);//先后运算 前者为false 后者不进行运算
           System.out.println(d);
           System.out.println(c);
      }
}

位运算符

package operator;
?
public class Demo04 {
   public static void main(String[] args) {
       //位运算符
       /*
       A = 1100 1100
       B = 0000 1101
       
       A&B 0000 1101
       A|B 1100 1101
       A^B 1100 0001
       ~B 1111 0010
       
       效率巨高
       2*8 2*2*2*2
       << *2
       >> /2
       0000 0000 0
       0000 0001 1
       0000 0010 2
       0000 0011 3
       0000 0100 4
       0000 1000 8
       0001 0000 16
        */
       System.out.println(2<<3);//16
  }
}

三元运算符

public class Demo06 {
   public static void main(String[] args) {
       // x ? y : z
       //如果x==true,则结果为y,否则结果为z
?
       int score = 80;
       String type = score <60 ?"不及格":"及格";
       // if
       System.out.println(type);
  }
}

运算优先级

字符连接符

public class Demo05 {
   public static void main(String[] args) {
       int a = 10;
       int b = 20;
?
       a += b;//a = a+b
       a -= b;//a = a-b
       System.out.println(a);
?
       //字符串连接符 + string
       System.out.println(""+a+b);//""号后面的字符串连接
       System.out.println(a+b+"");//“”前面的正常运算
?
       String s1 = "Hello";
       String s2 = "world";
       String s = s1 + " " + s2 + "!";
       System.out.println(s);//""加在中间连接字符串
  }
}

包机制

package pkg1[.pkg[.pkg3...]];

JavaDoc

将java编译成文档 javadoc -encoding UTF-8 charset UTF-8 doc.java

 

原文:https://www.cnblogs.com/Qiu101Shan/p/15177053.html

评论(0)
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!