JAVA学习笔记(1)

来源:互联网 发布:java 监听器原理 编辑:程序博客网 时间:2024/05/18 09:03

目录(?)[+]

基本数据类型

基本数据类型-定点型

package com.itany.basejava.day02.demo01;/* * 基本数据类型-定点型 */public class Test{    public static void main(String[] args)    {        //1-整型常量值默认的类型是int;如果声明的变量的类型为byte,short,char时,后面为这三个类型的变量        //赋值时,如果常量值在这三个变量表示的范围内,则int类型的常量会自动转换成byte,short,char,并赋值。        byte b = 10;        short s = 20;//      byte bb = 128;//报错,128超过了byte表示的范围,而此时,无法将int类型的128常量自动转换成byte类型//      short ss = 65535;//Type mismatch: cannot convert from int to short        //2-如果使用整型常量为long类型的变量赋值,则整型常量会自动转换成long类型。        long ll = 200;//此时,200常量为int类型,该int常量会自动向上转换成long类型,再赋值        //在整型常量后加上L或l,则该常量表示long类型的常量,用8个字节存储。        long l = 40L;        //3-如果常量的值超过了int类型表示的范围,则会报错。//      int xx = 11111111111111111;        long xx = 11111111111111L;        int i = 30;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

基本数据类型-char型

/* * 基本数据类型-char */public class Test{    public static void main(String[] args)    {        char c1 = 'a';//      char c2 = '';//要求字符常量必须是''中有一个字符//      char c3 = 'abc';        char c4 = '中';        //字符在内存中,也是以数值进行存储的。        //整型常量97属于char表示的范围内,则97会自动转换成char类型,赋值。        char c5 = 97;        System.out.println(c5);        //char类型既然是以数值存储,则可以参与数值运算。如果参与元算,则使用的是字符对应的数值        int x = c1 + 1;        System.out.println(x);        //转义字符---- \字符        //特殊的字符->普通字符        //' "   \           System.out.println('\'');        System.out.println('\\');        System.out.println("\"");        //普通字符->特殊含义字符        System.out.println("xxxxxxx");        System.out.println("aaaaaa\nbbbbbbbbbb");        System.out.println("xxxxxx\tyyyyyyyy");        //\r 回车,\n 换行, \t 制表符    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

基本数据类型-浮点型,布尔型

/* * 基本数据类型-浮点型,布尔型 */public class Test{    public static void main(String[] args)    {        //浮点型变量的定义        double d = 10.0;//      float ff = 10.0;//Type mismatch: cannot convert from double to float        float f = 10.0F;//10.0f或10.0F表示该常量为float类型。        //浮点型表示形式        double dd1 = 10.0;        double dd2 = 1.0e2;//科学计数法        double dd3 = 1.0E23;//1.0*10的23次方        double dd4 = 1.0e-23;        double dd5 = .12e-10;        double dd6 = dd1;        //java中无法表示1/10,如果需要具体表示一个小数,则使用BigDecimal类        System.out.println(1.0 - 0.9);        //boolean,默认为false        boolean flag = true;        flag = false;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

特殊的引用类型

特殊的引用类型-String

/* * 特殊的引用类型-String */public class Test{    public static void main(String[] args)    {        String s1 = "";        String s2 = "a";        String s3 = "abcdef";        int i = 10;        String s = "abc";        i = 100;//重新为i所指向的内存空间赋值        s = "efg";        System.out.println(i);//100        System.out.println(s);//efg    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

类型转换规则

类型转换规则:基本数据类型

/* * 类型转换规则:基本数据类型 */public class Test{    public static void main(String[] args)    {        //1-boolean不能与任何基本数据类型转换        //2-byte,short,char一般不互相转换。如果参与其他运算,会先自动转换成int类型,再运算.        byte b1 = 10;        short s1 = 20;        char c1 = 'a';        byte b2 = 10;        byte b3 = 20;//      byte b4 = 30;//30为常量,在byte类型表示的范围内,所以可以int自动转换成byte        //b2和b3会先自动转换成int类型,然后相加。此时,即使结果在byte表示的范围内,也无法自动转换成byte类型        //因为此时是变量相加,结果不确定。//      byte b4 = b2 + b3;//      int sum1 = b1 + 1;        int sum = b1 + s1 + c1;        //3-任何基本数据类型,都可以与String相加.基本类型会先转换成String,与String类型相加.        String str1 = "aaa";        String str2 = "bbb";        String str3 = "ccc";        //字符串通过+,进行拼接。        String str = str1 + str2 + str3;        System.out.println(str);//aaabbbccc//      System.out.println(str - str1);        int i2 = 1000;        long l2 = 11111L;        float f2 = 10.0F;        double d2 = 20.123;        boolean bool = true;        char c2 = 'a';        System.out.println("10" + i2);//"10" + "1000" -> "101000"        System.out.println(str1 + i2);//        System.out.println(str1 + l2);        System.out.println(str1 + f2);        System.out.println(str1 + d2);//"aaa" + "20.123"->        System.out.println(str1 + bool);//"aaa" + "true"->"aaatrue"        System.out.println(str1 + c2);//"aaa" + "a"->aaaa    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

基本数据类型转换

/* * 基本数据类型转换 */public class Test{    public static void main(String[] args)    {        //4-类型自动转换(隐式转换):字节数小的可以自动转换成字节数大。        byte b1 = 10;        short s1 = b1;        int i1 = s1;        long l1 = i1;        float f1 = l1;        double d1 = l1;//      5-类型强制转换(显式转换),如果字节数大的转换成小的,必须强制转换。强制转换,有可能发生数据溢出,造成错误。//      type1  t = (type1)值;        byte b2 = (byte)i1;        int i2 = (int)l1;        //注意是否会造成数据溢出。        int i3 = 200;        byte b3 = (byte)i3;//      如果是浮点型的数据转换成整型,则会出现截尾操作。        float f2 = 10.123F;        double d2 = 1000.6783;        int ii1 = (int)f2;        int ii2 = (int)d2;        System.out.println(ii1);        System.out.println(ii2);//      6-多种数据类型混合运算时,小类型自动转换成大类型。        byte x1 = 10;        short x2 = 20;        int x3 = 30;        long x4 = 40;        float x5 = 10.0F;        double x6 = 20.5;        char x7 = 'a';        System.out.println(x1 + x2 + x3 + x7 + x4 + x5 + x6);    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
原创粉丝点击