转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 数据库 >> Access >> 正文
DeclarationsandAccessControl(1)         ★★★★

DeclarationsandAccessControl(1)

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1502 更新时间:2009/4/22 21:27:57

  1) Declarations and Access Control
Objective 1
Write code that declares, constructs and initializes arrays of any base type using any of the permitted forms, both for declaration and for initialization.

1.    Arrays are Java objects. (An object is a class instance or an array.)  and may be assigned to variables of type Object.  All methods of class Object may be invoked on an array.  If you create an array of 5 Strings, there will be 6 objects created.
2.    Arrays should be
1.    Declared. (int[] a; String b[]; Object []c;  Size should not be specified now)
2.    Allocated (constructed). ( a = new int[10]; c = new String[arraysize] )
3.    Initialized. for (int i = 0; i < a.length; a[i++] = 0)
3.    The above three can be done in one step. int a[] = { 1, 2, 3 }; or  int a[] = new int[] { 1, 2, 3 }; But never specify the size with the new statement.
4.    Java arrays are static arrays. Size has to be specified at compile time. Array.length returns array’s size, cannot be changed.  (Use Vectors for dynamic purposes).
5.    Array size is never specified with the reference variable, it is always maintained with the array object. It is maintained in array.length, which is a final instance variable.  Note that arrays have a length field (or property) not a length() method. When you start to use Strings you will use the string, length method, as in  s.length();
6.    Anonymous arrays can be created and used like this:  new int[] {1,2,3} or new int[10]
7.    Arrays with zero elements can be created. args array to the main method will be a zero element array if no command parameters are specified. In this case args.length is 0.
8.    Comma after the last initializer in array declaration is ignored.

    int[] i = new int[2] { 5, 10};  // Wrong
int i[5] = { 1, 2, 3, 4, 5};  // Wrong
int[] i[] = {{},  new int[] {} }; // Correct
int i[][] = { {1,2}, new int[2] }; // Correct
int i[] = { 1, 2, 3, 4, } ; // Correct
int i[][] = new int [10][];  //Correct,  i.length=10.
    int [] i, j[] == int i[], j[][];
9.    Array indexes start with 0. Index is an int data type.
10.    Square brackets can come after datatype or before/after variable name. White spaces are fine. Compiler just ignores them.
11.    Arrays declared even as member variables also need to be allocated memory explicitly.  
static int a[];
static int b[] = {1,2,3};
public static void main(String s[]) {
    System.out.println(a[0]); // Throws a null pointer exception
    System.out.println(b[0]); // This code runs fine
    System.out.println(a); // Prints ‘null’
    System.out.println(b); // Prints a string which is returned by toString
}
12.    Once declared and allocated (even for local arrays inside methods), array elements are automatically initialized to the default values.(0 for numeric arrays, false for boolean, '\0' for character arrays, and null for objects).
13.    If only declared (not constructed), member array variables default to null, but local array variables will not default to null(compile error).
14.    Java doesn’t support multidimensional arrays formally, but it supports arrays of arrays. From the specification - “The number of bracket pairs indicates the depth of array nesting.” So this can perform as a multidimensional array. (no limit to levels of array nesting)
15.    Arrays must be indexed by int values; short, byte, or char values may also be used as index values because they are subjected to unary numeric promotion and become int values. An attempt to access an array component with a long index value results in a compile-time error.
16.    All array accesses are checked at run time; an attempt to use an index that is less than zero or greater than or equal to the length of the array causes an ArrayIndexOutOfBoundsException to be thrown.
17.    Every array implements the interfaces Cloneable and java.io.Serializable.
18.    Array Store Exception
If an array variable v has type A[], where A is a reference type, then v can hold a reference to an instance of any array type B[], provided B can be assigned to A.
Thus, the example:
class Point { int x, y; }
class ColoredPoint extends Point { int color; }
class Test {
    public static void main(String[] args) {
        ColoredPoint[] cpa = new ColoredPoint[10];
        Point[] pa = cpa;
        System.out.println(pa[1] == null);
        try {
            pa[0] = new Point();
        } catch (ArrayStoreException e) {
            System.out.println(e);
        }
    }
}
produces the output:
true
java.lang.ArrayStoreException
Testing whether pa[1] is null, will not result in a run-time type error. This is because the element of the array of type ColoredPoint[] is a ColoredPoint, and every ColoredPoint can stand in for a Point, since Point is the superclass of ColoredPoint.
On the other hand, an assignment to the array pa can result in a run-time error. At compile time, an assignment to an element of pa is checked to make sure that the value assigned is a Point. But since pa holds a reference to an array of ColoredPoint, the assignment is valid only if the type of the value assigned at run-time is, more specifically, a ColoredPoint. if not, an ArrayStoreException is thrown.

19.    Every element of an array must be of the same type The type of the elements of an array is decided when the array is declared. If you need a way of storing a group of elements of different types, you can use the collection classes.


没有相关教程
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Sql Server  · MySql
    · Access  · ORACLE
    · SyBase  · 其他
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台