Zero-based learning java ------ day2 ------ keywords, identifiers, constants, hexadecimal key conversion, java data types, type conversion of mandatory format

Today content requirements:

1. Understand the concept and characteristics of keywords, understand reserved words

2. The master identifier meaning, features, characters can use and precautions

3. Know the conversion constant between the concepts, decimal, hexadecimal, with an operational manner to understand the sign identification method

4. grasp the concept and definition of process variables

The master java data types and precautions

6. The master form casts of

 

1. Keyword

The concept: to be given specific meaning of the word java language

Features: All the letters are lowercase, in some advanced development tools, there will be a special color display

Reserved words: the current version is not the key, but it may be upgraded to a keyword in subsequent releases, such as goto; const

Note: In some other materials, may put the keyword as a reservation word

 

2. Identifier

2.1 Overview

The concept: to classes, interfaces, methods, variables from the name of the character sequence

Constituent elements:

    Case letters of the alphabet; number; '$' , '_'

Precautions ( mandatory, otherwise the compiler does not pass ):

               You can not start with a number; is not java keywords (including reserved words); strict case-insensitive (hello = Hello!)         

Example: The following identifiers which legitimate and which are not legitimate

HelloWorld (legal), DataClass (legal) ,, _ 983 (legal), $ b_c7 (legal), class (illegal, keyword same name), DataClass # (illegal, no identifiers element #), 98.4 ( illegal, can not start with a number), Hello World (illegal, not the constituent elements of the space identifier)

Commonly known as the 2.2 naming convention

(1) classes and interfaces: large hump Nomenclature

  Large hump nomenclature: capitalize the first letter (if more than one word, the first letter of each word is capitalized)

  eg:HelloWorld, StudentManagementSystem

(2) methods and variables: small hump Nomenclature

  Small hump nomenclature: the first letter lowercase (if more than one word, lowercase first letter of the first word, the first letter of another word is capitalized)

(3) Package: the nature of the package is a folder

  Reverse domain name with  Separated domain www.alibaba.com

Corresponding to the package name:.. Com.alibaba project name module name

Such as: 

com.alibaba.taobao.user
com.alibaba.taobao.product
com.alibaba.taobao.order
com.alibaba.taobao.dealer
com.alibaba.taobao.pay

(4) Constant : java program running process, its value remains constant amount

public static final double PI = 3.14;
SECRET_KEY=”dfasjdfdaslfjsdla324$%^&*”;

 

3. Constant

The concept and classification 3.1 constants

(1) Concept: In the course of running the program value remains constant amount

(2) Category:

  Literals:

    String constants: a content of double quotes

    Decimal constants: all integers

    Character constant: All decimals

    Boolean constants: true false

    Empty constants: single quotes contents enclosed, only one character (letter, number, symbol, Chinese), such as: ',', '3', 'in'

    Custom constants: fina with modified variable final int a = 100

Such as

class ConstDemo{
	public static void main(String[] args){
		System.out.println("字符串");
		System.out.println("100");
		System.out.println("200.1");
		System.out.println('好');
		System.out.println(true);
		System.out.println(false);
	}
}

  

4 hexadecimal related content

     Every few into one: using computers are binary

There are four commonly used binary ::

Binary: 0,1,10,11,100,101,110,111, .... 0,1 0b
octal: 0,1, .. 7, 10, ... 17, 20, 77 .... 100 ..... 0 -70
decimal: 9,10 ... 0,1, ... 19, 20 ... 99, 100 ... 0-9 default.
hex: 0,1, ... 9, a, b, c, d, e, f, 10 .... 0-9a-f 0x

Mutual conversion between hex:

(1) converted to decimal radix.

1234  =  1000+ 200 + 30 + 4 = 1*10^3 + 2*10^2 + 3*10^1 + 4*10^0

Law: Any binary number is converted to decimal in the binary digit corresponding to each bit is multiplied by a minus side, do accumulate

Binary number 0b10100 = 1 * 2 ^ 4 + 0 * 2 ^ 3 + 1 * 2 ^ 2 + 0 * 2 ^ 1 + 0 * 2 ^ 0 = 16 + 4 = 20

Octal number 0137 = 8 ^ 1 * 2 * 3 + 7 + 8 * 8 ^ 1 ^ 0 = 64 + 24 + 7 = 95

Hexadecimal number 0x13d = 1 * 16 ^ 2 + 3 ^ 16 * 1 + 13 * 16 ^ 0 = 256 + 48 + 13 = 256 + 61 = 317

(2) converted into decimal radix.

Law: turn it into several binary punishable by a few, until the quotient is zero, then the remainder reversal

Such as:

76 converts to a binary

Exercise:
The 75 turn into a binary 0b1001011
the 135 turn into octal 0207
to 143 converted to hexadecimal 0x8f

 (3) fast conversion method:

a conversion between binary and decimal .2

8421 yards:,

It expresses the meaning of the data on each bit corresponds to a fixed value, only the data value at the corresponding position to the addition, to obtain the decimal value corresponding to the binary, as follows:

 

Example:

0b10011001 = 128+16 + 9 = 153
0b11101100 = 128+64+32 + 12 = 192 + 44 = 236

 20 = 16 + 4 =0b10100

b. rapid conversion between binary and octal

Second turn eight: three binary turn into an octal
eight-to-2: put an octal binary split into three

 Case

0b10101010101110

 Octal corresponding to: 010 101 010 101 025 257 110 =

 07764 = 07(111)  7(111) 6(110) 4(100)    

The corresponding binary is: 0b111101010

 c. between binary and hexadecimal conversion

II to 16: turn into a four bit binary hex
16 2 revolutions of: a band split into four 16-bit binary

 Case

0b11 1010 1011 1111 0101 = 0x3abf5
0b1 0101 1110 1011 =0x15db
0x7ade = 0b0111 1010 1101 1110

 d. x进制和y进制相互转换:

如,6 进制转7 进制:中间使用10 进制作为桥梁

 

有符号的表示法

为了解决负数在计算机中的存储,就出现了原码,反码,补码
(1)原码: 用最高位代表符号, 正数的符号位是0,负数的符号位是1,其他位代表数值
  使用8 位二进制来表示(计算机中存的数据单位一般是byte)
  7 的原码: 0 000 0111
  -7 的原码: 1 000 0111

 (2)反码: 正数的反码就是原码, 负数的反码,符号位不变,其他为取反

  7 的反码: 0 000 0111
  -7 的反码: 1 111 1000

 (3)补码: 正数的补码就是原码,负数的补码,在反码的基础上末位加1

  7 的补码: 0 000 0111
  -7 的补码: 1 111 1001

 计算机中存储的都是二进制的补码

练习:
已知某数X 的原码为0b10110100,试求X 的补码和反码。
补码: 11001100
反码: 11001011

 

已知某数X 的补码0b11101110,试求其原码。
原码: 10010010

 

5.  变量

概念: 在java 程序运行的过程中,其值可以在一定范围内发生改变的量

定义格式:
    数据类型变量名= 初始化值;
    数据类型:
        整数: int
        字符串: String
    变量名: 小驼峰
    初始值: 变量的初始化值

变量的声明:
    数据类型  变量名;

 1 class VarDemo{
 2     public static void main(String[] args){
 3         int age = 18; //定义变量
 4         String nickName = "一缕82 年的清风~";
 5         int golds = 300;
 6         int level = 1;
 7         System.out.println(nickName);//一缕82 年的清风~
 8         golds = 3000;// 对变量重新赋值,不加数据类型
 9         age = 19;
10         nickName = "哈哈之力";
11         int weight = 100;
12         System.out.println(golds);//3000
13         System.out.println(age);//19
14         System.out.println(nickName);//哈哈之力
15         System.out.println(weight);//错误: 可能尚未初始化变量weight
16     }
17 }

变量的注意事项

    1. 定义在方法中的变量叫做局部变量, 局部变量不赋初值不能使用
      int weight;
      //定义在方法中的变量叫做局部变量, 局部变量不赋初值不能使用
      System.out.println(weight);//错误: 可能尚未初始化变量weight
    2. 变量所在的大括号叫做变量的作用域,同一个作用域中不能存在多个名字相同的变量
      int golds = 300;
      int golds = 30;//报错,已存在的变量golds
    3. 同一行可以定义多个变量,但是不建议这么使用.因为可读性差
      int a,b,c=10;//  等价于int a;int b;int c=10;
      System.out.println(a);// 错误: 可能尚未初始化变量a

 

6.数据类型

6.1 分类和范围:

(1)基本数据类型
  四类八种
    整数类: byte  short   int   long
    小数类: float    double
    字符类: char
    布尔类: boolean

(2)引用数据类型: 除了基本数据类型以外的所有类型
  类: String
  接口
  数组

类型        字节        位数       默认值          范围
byte     1     8       0         -128~127
short       2     16       0         -32768~32767 -2^15-- 2^15 -1
int      4      32         0        -2147483648~2147483647
long      8      64      0      -9223372036854775808~9223372036854775807
float      4      32         0.0     -3.4E38~3.4028235E38
double     8      64       0.0     -1.79E-308~1.7976931348623157E308
char      2      16       \u0000      0~65535
boolean      1        8       false       true 或false

 

bit: 一个二进制位
字节(byte) 8 个bit 256
1kb = 1024byte
1mb = 1024kb
1gb = 1024mb
1tb = 1024gb
1pb = 1024tb
…….

案例:使用8种数据类型定义变量

class DataTypeDemo{
    public static void main(String[] args){
        byte b1 = 22;
        short s1 = 10;
        int i1 = 44;
        long l1 = 55;
        float f1 = 12.4f;//从double(8字节)转换到float(4字节)可能会有损失,所以此处要加f将12.4变为float类型的小数
        double d1 = 12.56;
        char ch = '凡';
        boolean b2 = true;
        System.out.println(b1);
        System.out.println(s1);
        System.out.println(i1);
        System.out.println(l1);
        System.out.println(f1);
        System.out.println(d1);
        System.out.println(ch);
        System.out.println(b2);
    }
}

定义的注意事项

1. 整数的默认类型是int, 小数的默认类型是double
  System.out.println(70);// 70 是int 类型的
  System.out.println(123.4);// double 类型的
2. 定义float 类型的小数, 要在小数的后面加上f
  float f1 = 12.4;//.错误: 不兼容的类型: 从double 转换到float 可能会有损失
  原因: 这是一条赋值语句,是把等号右面的值,赋值给等号左边的变量,等号右面的值12.4,默认是double 类型的,把double 赋值给float,可能会造成损失
  解决方案: 在小数的后面加上f(F), float f1 = 12.4f; //12.4f 代表一个float 类型值
3. 3byte b1 = 123; 是把int 类型的123 赋值给byte,也是把大类型的数据赋值给小类型,但是并不会报错. 定义byte,short 类型的变量的时候,如果=右面的数值在等号左边的类型的范围之内,就可以赋值;
  byte by = 129;// 错误: 不兼容的类型: 从int 转换到byte 可能会有损失
4. 定义long 类型的变量的时候,如果等号右边的值在int 的范围之内,则可以直接赋值,如果等号右边的数值超出int 的范围,需要在数值的后面加上一个L(l),推荐使用大写的
  long l1 = 2147483649;// 2147483649 超出int 的最大值,会报错
  // 错误: 过大的整数: 2147483649

 

6.2 默认值

 8 种基本数据类型,都有各自对应的默认值:

  byte,short,int ,long   默认值为0
  float double      默认值0.0
  char         \u0000 (空格)
  boolean         false

局部变量是没有默认值的,全局变量有默认值
定义在方法中的变量是没有默认值的,要想使用,必须赋值
静态的方法中只能调用外部用static 修饰的变量或方法

具体验证代码如下

class DataTypeDemo2{
    int a;
    static double b;
    static boolean bo;
    public static void main(String[] args){
        // 定义在方法中的变量叫做局部变量,局部变量不赋初值不能使用
        byte b1;
        //System.out.println(b1);//错误: 可能尚未初始化变量b1
        //System.out.println(a);//错误: 无法从静态上下文中引用非静态变量a
        System.out.println(b);//0.0
        System.out.println(bo);//false
    }
}

 

6.3 运算规则

byte,short,char 不能直接做运算,要想做运算必须先转成int,给byte,short, 赋值的时候,先看等号的右边有没有变量,

         如果有变量,就按照上面的规则做运算,如果结果的类型范围超出左边的范围,就会产生错误        

    如果都是常量,就看最终的结果是否在左边的范围之内,如果在,就可以直接赋值

解释:当右边的值为变量时,只有在运行时才知道值,编译时只能知道其类型,所以就不知道值是否在范围内,只能通过数据类型的范围大小来判断,当右边的值为常数时,在编译时就知道了编译             结果,就可以直接判断其在不在范围内。

 

默认可以发生转换(范围小的想大的转)

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

float 3.4*10 ^38 > 3.4*8^38 = 3.4*(2^3)^38 = 3.4*2^114 
long: 2^63 -1

通过最大值的比较,得出结论, float 的最大值远大于long 的最大值,  所以我们可以把long 赋值给float 类型的变量
案例如下:

class DataTypeDemo3{
    public static void main(String[]args){
        byte b1=3,b2=4,b;
        //b=b1+b2;//错误: 不兼容的类型: 从int 转换到byte 可能会有损,
        b=3+4;//正确的, byte b = 7;
        float f = 12.4f;
        float f1 = 134L;
        float f2 = 120;//正确的
        // float f3 = 120.0;   错误的,要加f,因为小数默认类型为double
        System.out.println(f1);//134.0
    }
}

6.4  char类型

char 类型代表的是字符.用单引号括起来,里面只能有一个字符:
char 占两个字节(0-65535),用的是unicode
utf-8: 表示英文和数字用1 个字节,表示中文用3 个字节
gbk: 表示英文和数字用1 个字节,表示中文用两个字节

 

char 类型直接打印,用的是他所代表的字符
char 类型参与运算,用的是他底层的编码

定义char 类型:
      char ch = ‘a’;
      char ch1 = 98;
      char ch2 = ‘\u0061’//a
char 类型的默认值其实就是编码是0 的字符,表现形式是一个空格,也可以表示成'\u0000'

class DataTypeDemo4{
  public static void main(String[] args){
    //char: 如果直接打印,用的是他所代表的字符,一旦参与运算,使用编码来运算
    char ch = 'a';
    System.out.println(ch);//a
    System.out.println(ch+1);//98
    char ch2 =98;
    System.out.println(ch2);//b
    char ch3 = '\u0063';// 后面是4位的十六进制,99
    System.out.println(ch3);//c
    //char 默认值: 0 \u0000 表现形式是一个空格
    System.out.println('3'+4);//55
    System.out.println('3'+'4');//103
  }
}

6.5 加法运算

+ : 正号, 加法运算, 字符串的拼接

要注意运算的顺序: 从左往右依次运算,有括号先算括号里的(先算乘除,后算加减)

String 和任意类型做加法,得到的都是String,值就是拼接后的结果

布尔类型不能和其他的基本数据类型做运算

class DataTypeDemo5{
  public static void main(String[] args){
    int a = +10;
    System.out.println('a');// a
    System.out.println('a'+1);//98
    System.out.println("hello"+'a'+1);// helloa1
    System.out.println('a'+1+"hello");//98hello
    System.out.println("5+5="+5+5);//5+5=55
    System.out.println("5+5="+(5+5));//5+5=10
    System.out.println(5+5+"=5+5");//10=5+5
    //System.out.println(true+'1');//: 错误: 二元运算符'+' 的操作数类型错误
    System.out.println(true+"1");// true1
  }
}

 

7. 强制类型转换

强制类型转换的格式:
      目标类型变量名= (目标类型) 要转的值(常量或变量);
基本数据类型: 除了boolean 类型以外,其他7 中类型都可以互相转换
        一般是在把大类型转成小类型的时候用强转
引用数据类型: 必须具备子父类的关系     向下转型

  

class ForceTypeZH{
    public static void main(String[] args){
    int a = 10;
    long b = a;//默认转换小的给大的
    System.out.println(b);//
    long c = 10;
    //int d = c;// 错误: 不兼容的类型: 从long 转换到int 可能会有损失
    //非要转,就强转
    int d = (int)c;
    byte by = (byte)130;
    System.out.println(by);//-126
    }
}

为什么130强制转换成byte类型时,其值就变成-126,原因如

此处因为130开始为int类型(4byte),转为byte类型后,其位数为8,则丢弃前面3字节

 

Guess you like

Origin www.cnblogs.com/jj1106/p/11274856.html