Java标识符,关键字,数据类型,引用类型

1.标识符和关键字

java中的标识符指的是方法,类名和属性的名称,要求是:标示符由字母、数字、_、$、¥组成,其中不能以数字开头,不能是Java中的关键字,注意在定义的时候不要用数字开头。同时也不要使用关键字作为标识符如if。
关键字(或者保留字)是对编译器有特殊意义的固定单词如下所示
数据类型:boolean、int、long、short、byte、float、double、char、class、interface。
流程控制:if、else、do、while、for、switch、case、default、break、continue、return、try、catch、finally。
修饰符:public、protected、private、final、void、static、strict、abstract、transient、synchronized、volatile、native。
动作:package、import、throw、throws、extends、implements、this、supper、instanceof、new。
保留字:true、false、null、goto、const。

2.数据类型与引用类型

Java 语言支持的数据类型分为两种:基本数据类型(Primitive Type)和引用数据类型(Reference Type)。其中基本数据类型包括 boolean(布尔型)、float(单精度浮点型)、char(字符型)、byte(字节型)、short(短整型)、int(整型)、long(长整型)和 double (双精度浮点型)共 8 种,如以下所示的代码在进行变量的声明的过程中需要进行数据类型的声明。

public class Demo01 {
    public static void main(String[] args) {
        //基本数据类型
        //变量必须声明其类型
        //整数
        int num=166;//数据类型 变量名=值;
        byte num2=127;
        short num3=30;
        long num4=30L;
        //小数
        float num5=50.13f;
        double num6=3.4242;
        char name='A';
        String a="hello";//String不是关键字
        //布尔值
        boolean flag=true;
        System.out.println(a);
        System.out.println(num);

    }
}

注1:对于String属于Java的引用数据类型,对于引用类型包括数组,类和接口可以直接通过关键字new来创建对象,也可以通过字符串直接赋值

        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。对于第一个引用变量是普通变量,定义时在栈中分配内存,引用变量在程序运行到作用域外释放。

3.数据类型的转换与数据溢出

数据类型范围小与数据类型范围大的数据进行计算时,将自动转化为大范围的数据类型,高等级类型转发为低等级类型时候会进行强制类型转换如:(int) long。
byte,short,char < int < long < float < double
注:

public class Demo04 {
    public static void main(String[] args) {
        int i=128;
        byte b=(byte)i;//强制类型转换
        System.out.println(i);
        System.out.println(b);//内存溢出
        System.out.println("==============");
        char c='a';
        int d=c+1;
        System.out.println(d);
        System.out.println((char) d);//高等级向低等级转换的时候强制类型转换

    }
}

结果为:
128
-128
98
b

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值