Java语法笔记之变量

来源:互联网 发布:sql日期截取年月日 编辑:程序博客网 时间:2024/05/22 04:16

Java语言基础(Java language basics)

变量(Variblae)

Java中的变量分为以下几种:
1. 实例变量
2.类变量:类变量通常由static修饰,表示该类中对该变量的修改都会影响该变量的值。若使用final修饰,则该变量一旦初始化,就不可修改。
3.局部变量
4.形参变量

变量的命名规则

  1. Java的变量是区分大小写的。Java变量由字母、数字、’$’和下划线组成。一般来说,变量以字符开头,’$’基本不放在变量开头。随后的字符可以是组成变量的任意字符。变量命名最好使用见名知意的单词,而且最好使用全称词,而要避免使用缩写。
  2. 如果变量名只有一个单词,最好全部使用小写字符。若变量有多个单词,可以将后续单词中的首字符大写,例如firstOne。若变量是一个常量,变量全部使用大写字符,若有多个单词使用下划线分隔。

基本数据类型

变量必须先声明,后使用。
1. byte: 该类型为8位有符号整型数。范围为-128~127。
2. short: 该类型为16位有符号整型数。范围为 -32,768~32,767。
3. int: 默认情况下,该类型为32位有符号整型数。范围为231 ~2311。在JavaSE8中,通过Integer类,可以将int作为无符号整型数使用,其范围为0~2321
4. long: 默认情况下,该类型为64位有符号整型数。范围为263 ~2631。类似的,在JavaSE8中,通过Long类,可以将long作为无符号长整型数使用,其范围为0~2641
5. float: 该类型为32位IEEE754标准的浮点数。
6. double: 该类型为32位IEEE754标准的浮点数。float和double类型均不能用于表示精确值。若要使用精确值,需要使用java.math.BigDecimal来替代。
7. boolean: boolean只能去两个值,即true和false。
8. char: 该类型为16位的Unicode字符。最小值为’\u0000’(0),最大值为’\uffff’(65535)。

除了上述常用的8种基本的数据类型外, java.lang.String类也比较常用。String类可以直接定义变量而不需要初始化。String变量是不变量,即一旦创建一个String对象,其值就无法改变。

  • 基本数据类型的默认值。不同于C语言,Java的基本数据类型在声明后,均有默认值,各变量的默认值如下:
数据类型 默认值 byte 0 short 0 int 0 long 0L float 0.0f double 0.0d boolean ‘\u0000’ char false String(or any object) null
  • 局部变量声明时不存在默认值,因此在声明局部变量时,最好赋初值。
  • 整型常量:不同长度的整型变量都可以通过int类型实现。特别地,若为长整型,需要在整型数后加L。例如:int max = 123L。再给整型变量赋值时,默认为十进制,要使用其他进制,方法如下:
// The number 26, in decimalint decVal = 26;//  The number 26, in hexadecimalint hexVal = 0x1a;// The number 26, in binaryint binVal = 0b11010;
  • 浮点型常量: 浮点型变量赋值时,若为float类型,必须在浮点型文本最后跟f或F。若double,文本最后的d或D是可选的。例如:
double d1 = 123.4;// same value as d1, but in scientific notationdouble d2 = 1.234e2;float f1  = 123.4f;
  • 数组:在Java中,数组是存放固定数量的单一类型数据的一个容器对象。因此声明一个数组时需要使用new关键字。此外,还可以使用内建函数length来获取数组长度。例如:
// declares an array of integers//the declaration does not actually create an array; it simply  //tells the compiler that this variable will hold an array of the  //specified type.int[] anArray;// allocates memory for 10 integersanArray = new int[10];//or declares and allocatees memory at a statementint[] anArray = new int[10];//you can also defined an array like C programing language//but this form is discouragedfloat anArrayOfFloats[];//you also can declare an array and initialize it at same timeint[] anArray = {     100, 200, 300,    400, 500, 600,     700, 800, 900, 1000};//The method of create multidimensional arrayclass MultiDimArrayDemo {    public static void main(String[] args) {        String[][] names = {            {"Mr. ", "Mrs. ", "Ms. "},            {"Smith", "Jones"}        };        // Mr. Smith        System.out.println(names[0][0] + names[1][0]);        // Ms. Jones        System.out.println(names[0][2] + names[1][1]);    }}//get the size  of an arraySystem.out.println(anArray.length);
  • java系统方法System.arraycopy可以讲一个数组中的一部分复制给另外一个数组。方法如下:
//The two Object arguments specify the array to copy from and the array to copy to. The three int arguments specify the starting position in the source array, the starting position in the destination array, and the number of array elements to copy.public static void arraycopy(Object src, int srcPos,                             Object dest, int destPos, int length)class ArrayCopyDemo {    public static void main(String[] args) {        char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e',                'i', 'n', 'a', 't', 'e', 'd' };        char[] copyTo = new char[7];        System.arraycopy(copyFrom, 2, copyTo, 0, 7);        System.out.println(new String(copyTo));    }}/*The output from this program is:caffein*/
  • Java中的java.util.Arrays类提供了操作数组的常用方法(例如复制,查找,排序)。例如:
class ArrayCopyOfDemo {    public static void main(String[] args) {        char[] copyFrom = {'d', 'e', 'c', 'a', 'f', 'f', 'e',            'i', 'n', 'a', 't', 'e', 'd'};        char[] copyTo = java.util.Arrays.copyOfRange(copyFrom, 2, 9);        System.out.println(new String(copyTo));    }}
1 0