Java basic data types and arrays

First, the key identifier

Identifier is used to identify the class name, variable name character sequence effective, method name, type name, array name, file name. Java language specified identifier has the following requirements:

  • Identified by letters, underline, dollar signs and numbers, the length is not limited.
  • The first character of an identifier can not be a numeric character.

  • Identifiers can not be keywords.
  • Identifiers can not be true, false and null (although true, false and null is not a keyword).

 The above mentioned key identifier can not be, because the key is to have a few words for a particular purpose or be given a specific meaning. Java keywords are lowercase. Meet capital is certainly not a keyword. Here are some common Keywords:

 

 

 

 Second, the basic data types

Java has eight basic data types, and their respective bytes occupied in the following ranges:

 

 Wherein special attention is required, long variables must have the suffix L, float variables must have the suffix F or f.

 Java data types (excluding types of logic) from low to high precision by respectively: byte short char int long float double, which also determines the rules for type conversions:

  • When the value assigned to the variable low-level high-level variables, the system automatically converts the data type.
  • When the value assigned to the variable high-level low-level variables, must display type conversion operation. Type name display :( format conversion) the value to be converted; for example: int x = (int) 35.55.

Third, the array

1, declare an array of

1  // declare one-dimensional array 
2  a float A [];
 . 3  char [] B;
 . 4  // declare a two-dimensional array 
. 5  a float A [] [];
 . 6  char [] [] B;

Note that, because Java declare an array, the brackets can be in front of so int [] a, b [] actually declared a one-dimensional array and a two-dimensional array b.

2, the array initialization

Java arrays are static and must be initialized before use, but after initializing a specified length, this length can not be changed. Common methods are:

1  // static initializer, a given initial value, is determined by the length of the system 
2  int [] = A {l, 2,3 }; 
 . 3  
. 4  // Ibid 
. 5  int [] B = new new  int [] {l, 2,3 };
 . 6  
. 7  // dynamic initialization, the specified array length, given the initial value 
. 8  int [] = A new new  int [100 ];
 . 9  for ( int I = 0; I <a.length; I ++ ) {
 10 A [ I] = I;
 . 11 }

About the length of the array, we can use to get the length,

  • For one-dimensional array, the value "array name .length" is the number of elements in the array
  • For two-dimensional array, the value ".length array name" is the number of one-dimensional array containing the

3, the reference to the array

We know that memory addresses are usually very long, because it is not easy to remember, so give these addresses to take a name, which is the reference variables, these references are stored in a variable called "stack memory" of the region, "reference", that is, Java objects in heap memory address assigned to the "stack memory" variable. The Java array is a reference type variable, so if two of the same type have the same array of references, they have exactly the same elements.

. 1  class Example
 2  {
 . 3      public  static  void main (String args [])
 . 4      {
 . 5          int [] A = {1,2,3,4 };
 . 6          int [] = {100,200, 300 B };
 . 7          System.out.println ( "number of elements in the array a =" + a.length);
 . 8          System.out.println ( "number of elements of the array b =" + to b.length);
 . 9          System.out.println ( "a reference to an array = "+ a);
 10          System.out.println (" reference array b = "+ b);
 . 11          a = b;
 12 is          System.out.println (" number of elements in the array a = "+a.length);
13         System.out.println("数组b的元素个数="+b.length);
14         System.out.println("数组a的引用="+a);
15         System.out.println("数组b的引用="+b);
16         System.out.println("a[0]="+a[0]+",a[1]="+a[1]+",a[2]="+a[2]);
17         System.out.println("b[0]="+b[0]+",b[1]="+b[1]+",b[2]="+b[2]);
18     }
19 }

 

Guess you like

Origin www.cnblogs.com/langkexinghan/p/12605973.html