三、注释、标识符、数据类型与类型转换

一、注释

书写注释是一个好习惯

public class Comment {
    public static void main(String[] args) {
        //单行注释
        //输出Hello World!

        /*
        多行注释
        输出Hello World!
         */

        /**
         * 文档注释
         */

        System.out.println("Hello World!");
    }
}

二、标识符

2.1、关键字


访问控制
private    protected    public

类,方法和变量修饰符
abstract    class    extends    final    implements    interface    native    new
static    strictfp    synchronized    transient    volatile
    
程序控制
break    continue    return    do    while    if    else    for    instanceof    switch
case    default
    
异常处理
try    cathc    throw    throws

包相关
import    package
    
基本类型
boolean    byte    char    double    float    int    long    short    null    true    false

变量引用
super    this    void
    
保留字
goto    const    

2.2、标识符的命名规范

   1、标识符用作给变量、方法和类命名。

   2、以字母、下划线“_”和“$”符开头,后面可跟字母、下划线“_”和“$”符或数字。

   3、大小写敏感。

   4、应该使用有意义的名称,达到见名知意的目的,并且长度无限制。

   5、尽量不要使用中文。

   6、不可以是true和false。true和false虽然不是关键字,但是有特殊用途。

   7、避免与java关键字与java类库的类名重名。

三、数据类型

3.1、基本数据类型

基本数据类型有四类八种,分别为:

整型:byte,short,int,long;

浮点型:float,double;

逻辑型:boolean;

字符型:char。

3.2、引用数据类型

引用数据类型有类、接口和数组。

3.3、数据类型拓展

public class Radix {
    public static void main(String[] args) {
        int i = 10;//十进制
        int i1 = 010;//八进制
        int i2 = 0x10;//十六进制

        System.out.println(i);
        System.out.println(i1);
        System.out.println(i2);

        System.out.println("-----------------------------------");
        //类型不同
        float f = 0.1f;
        double d = 1.0/10;

        System.out.println(f==d);

        System.out.println("-----------------------------------");
        //float 有限  离散  舍入误差  大约  接近但不等于
        //最好不要使用浮点数进行比较
        float a = 16948131f;
        float a1 = a+1;

        System.out.println(a==a1);

        System.out.println("------------------------------------");
        //字符类型强制转换
        char c = '龙';

        System.out.println((int) c);
        
        System.out.println("------------------------------------");
        //转义字符
        /*
        1./t 制表符
        2./n 换行
         */
        System.out.println("------------------------------------");
        //布尔值扩展
        boolean flag = true;
        
        if (flag ==true){}
        if (flag){}
    }
}

四、类型转换

public class Conversion {
    public static void main(String[] args) {
        /*
        低-----------------------------------高
        byte,short,char,int ,long,float,double

        由高到低:强制转换    (类型)变量名
        由低到高:自动转换

        强制转换(注意点)
        1.不能对boolean值转换
        2.不能把对象类型转换为不相干的类型
        3.在把高容量转换到低容量的时候,强制转换
        4.转换的时候可能存在内存溢出,或者精度问题
         */

        //溢出问题
        int money = 1000000000;
        int year = 20;
        int total = money*year;
        System.out.println(total);//溢出
        long total1 = money*((long)year);
        System.out.println(total1);

        System.out.println("------------------");
        //精度问题
        System.out.println((int) 23.7);
        System.out.println((int) -45.89f);

        System.out.println("------------------");

        char c = 'a';
        int d = c+1;
        System.out.println(d);
        System.out.println((char) d);
    }
}

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

&小小白&

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值