Java Programming Fundamentals stage notes day02 Java basic grammar (on)

Java basic grammar (on)

Content Outline

  • Keywords and reserved words

  • Identifier

  • Java naming conventions

  • Variable concept

  • Variable Category

  • Variable range

  • Basic data type conversion

  • String operation and basic data types

  • The variable part of the exercise

  • Ary and its conversion

  • The original code, anti-code complement

  • Hexadecimal conversion

  • Binary conversion exercises

  • Arithmetic Operators


  • Keywords and reserved words

    • Keyword definition: Java language has special meaning as defined in word use

    • Features Keywords: Keywords all lowercase letters

    • Reserved word definition: after keywords as possible, it has not yet use

  • Identifier

    • You can place your own name called identifier

    • note:

    • ①26 case letters, 0-9, $, _ composed of

    • ② not as the beginning of the digital

    • ③ identifier is not unlimited length, no spaces

  • Java naming conventions

    • Package name: multi-word lowercase william.com

    • Class name, interface name: multi-word capitalized class TestDemo

    • Variable names, method names: the first letter lowercase, uppercase testDemoFunction from the first letter of the second word

    • Constant name: All words capitalized, many words connected with underscores TEST_NUM

  • variable

    • ① ② stored in memory with a program storage unit range ③, the data store

    • Variable declaration format: variable name = variable type variable value int number = 0;

    • Variables must be declared re-use, and can not be repeated

    • Scope variables: {} variable assignment statement within the scope only to play a role in the

  • Variable Category

    • By data type

?wx_fmt=png

    • Statement by position classification

          ① class within and outside the method declaration: Member variables

 ② Method inner body: local variables?wx_fmt=png

  • Variable range

    • Integer types: byte (8bit. 1 byte =) -128 ~ 127 |  Short (2 bytes) |  int (. 4 bytes) |  Long (bytes. 8)

①long num = 123445678L; add back "L"

②double a = 0:11;

③float num = 0.11F; add back "F"

  • Character type: char (2-byte) characters

①Java picked in a  Unicode encoding, you may store a letter characters, a character other languages

 char c1 = 'a';   char c2 = ''; char c3 =  '9';

 可存储转义字符,如 char c4='\u000a';  (16进制)表示 \n

          ?wx_fmt=png

  • 基础数据类型转换

    • 自动类型提升

①byte,short,char->int->long->float->double(小容量自动转换换成大容量,容量为表数范围)

②多类型数据混合运算,将数据类型转换成最大的再进行运算

int a=10;

long b=1000L;

long c=a+b;

③byte,short,char运算时转换成int类型运算

  • 强制类型转换:

①大容量放到小容量 ②使用强制转换符③boolean不能强制类型转换

byte,short,char之间做运算会先将自身提升成int类型

int b=1;

int a=1;

byte c=(byte)b + (byte)a;   //编译不通过,byte类型运算时会自动转为int类型

正确写法:byte c = byte(b+a);


基本数据类型与String类型运算

  • String s = "hello";  String s2 = new String("hello2");

  • String类型与其他类型相加都转为String类型

  • Java的整型常量默认为int类型,Java的浮点类型默认为double类型

变量部分练习

?wx_fmt=png

  • 进制

    • 二进制(binary)0,1 ,满2进1.以0b或0B开头。

    • 十进制(decimal)0-9 ,满10进1。

    • 八进制(octal)0-7 ,满8进1. 以数字0开头表示。

    • 十六进制(hex)0-9及A-F,满16进1. 以0x或0X开头表示。

    • 010   0b10  0x10  10    

?wx_fmt=png

  • 原码、反码、补码(二进制整数)

    • 原码:直接将一个数值换成二进制数。最高位是符号位

    • 负数的反码:是对原码按位取反,只是最高位(符号位)确定为1。

    • 负数的补码:其反码加1。

①计算机以二进制补码的形式保存所有的整数。

②正数的原码、反码、补码都相同

③负数的补码是其反码+1

  • 进制转换

    • 十进制->二进制: 2取余的逆    如13转换:连除2余数分别为1、0、1、1;逆序为1101

    • 二进制->十进制  乘以2的幂数

    • 二进制  八进制互转

?wx_fmt=png

    • 二进制   十六进制互转

?wx_fmt=png

    • 十进制 八进制互转:先转成二进制

    • 十进制 十六进制互转:先转成二进制

  • 进制练习

?wx_fmt=png

  • 算术运算符

?wx_fmt=png

 取模结果的正负和被模数的正负有关  

 System.out.println( 4 %  -3);   // 1

        System.out.println(-4 %   3);   //-1

        System.out.println(-4 % -3);   //-1

② 整数之间做除法只保留整数部分

③ 计算机中运算顺序不同可能会导致不同结果

    n2 = 10 / 4 * 4;//8.0    

    n2 = 10 * 4 / 4;    //10.0

  • 练习

随意给出一个整数,打印显示它的个位数,十位数,百位数的值。格式如下:

数字xxx的情况如下:

个位数:

十位数:

百位数:



Guess you like

Origin blog.51cto.com/10421297/2423662