java基本语法

1.java基本语法

 

1.1注释

  1. 单行注释//

  2. 多行注释/* */

  3. 文档注释/** */

1.2标识符

1.2.1基本

1.关键字
在这里插入图片描述
 
2.所有标识符都以 字母,_ , $ 开头

标识符大小写敏感
 
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.2.2关于数据类型面试扩展

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!
 

1.3类型转换

高低顺序:

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.4变量

1.4.1变量作用域

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.4.2变量的命名规范

1.变量命名: 见名知意

2.类成员变量: 首字母小写,除了第一个单词后面单词首字母大写(驼峰原则)

3.常量名:全是大写字母或者下划线

4.类名:首字母大写,驼峰原则(新建文件命名)

 

1.5常量

一种特殊的变量,它的值被设定后便不可以改变!

final 常量名 = 常量值

常量名一般用大写字符

public class Helloworld {
    static final double PI = 3.14;            //常量
    public static void main(String[] args) {
        System.out.println(PI);
    }
}

 

1.6基本运算符

ctrl+D 复制当前行到下一行

当使用除法的时候会出现小数一定要注意数据类型,要把整数强制转化为小数

整数型进行运算的时候,当有long参与,结果为long型;无long参与,结果全为int。(double同理)

与或非 &&,||,!

 

1.6.1自增自减 ++ ——

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

 

1.6.2幂

 double pow = Math.pow(2,3);   //Math类
  System.out.println(pow);     //8.0

 

1.6.3逻辑运算符

&& || !

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

 

1.6.4位运算

& | ~ (非) ^

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      
System.out.println(8>>2);     //2       

例如:int i=15; i>>2的结果是3:
转为二进制的形式可能更好理解(移出的部分将被抛弃)
0000 1111(15)右移2位的结果是0000 0011(3).

带符号右移(>>):正数右移高位补0,负数右移高位补1
无符号右移(>>>):无论是正数还是负数,高位通通补0。
 

1.6.5三元运算符

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


  • 68
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 15
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值