Declarations and Access Control (1)

access

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.

时间: 2024-10-30 06:30:41

Declarations and Access Control (1)的相关文章

Declarations and Access Control (2)

access Objective 2Declare classes, inner classes, methods, instance variables static, variables and automatic (method local) variables, making appropriate use of all permitted modifiers (such as public final static abstract and so forth). State the s

Swift中的Access Control权限控制介绍

  这篇文章主要介绍了Swift中的Access Control权限控制介绍,本文讲解了private.internal.public三个关键字的使用,需要的朋友可以参考下 如果您之前没有接触过权限控制,先来听一个小故事: 小明是五道口工业学院的一个大一新生,最近他有点烦恼,因为同屋经常用他的热水壶,好像那是自己家的一样,可是碍于同学情面,又不好意思说.直到有一天,他和学姐小K吐槽. 学姐听了之后,说:大学集体生活里面,大部分东西都是默认室友可以共用的.如果你不想别人拿,我可以帮你做封印,只要打

【原创】RabbitMQ 之 Access Control(翻译)

Access Control  When the server first starts running, and detects that its database is uninitialised or has been deleted, it initialises a fresh database with the following resources: 当服务器启动运行后,检测到所使用的数据库未进行过初始化,或者被删除了,则会使用如下资源初始化一个新的数据库:  a virtual

新的RBAC:基于资源的权限管理(Resource-Based Access Control)

本文讨论以角色概念进行的权限管理策略及主要以基于角色的机制进行权限管理是远远不够的.同时将讨论一种更好的权限管理方式. What is a Role? 什么是角色 当说到程序的权限管理时,人们往往想到角色这一概念.角色是代表一系列行为或责任的实体,用于限定你在软件系统中能做什么.不能做什么.用户帐号往往与角色相关联,因此,一个用户在软件系统中能"做"什么取决于与之关联的各个角色. 例如,一个用户以关联了"项目管理员"角色的帐号登录系统,那这个用户就可以做项目管理员能

Windows Azure AppFabric 入门教学系列 (五):初探Access Control Service

本文是Windows Azure AppFabric入门教学的第五篇文章.本文会对AppFabric中的Access Control Service(ACS)做初步的讲解.为了使后续的学习顺利进行请确保已浏览本教程的第一篇文章,并以按照该文完成了AppFabric项目和命名空间的创建.我们知道,AppFabirc由Service Bus 和 Access Control Service组成,在前一篇教程中我们已介绍过Service Bus,所以本文将简略的介绍如何使用ACS服务来确保安全性. 同

Swift中的Access Control权限控制介绍_Swift

如果您之前没有接触过权限控制,先来听一个小故事: 小明是五道口工业学院的一个大一新生,最近他有点烦恼,因为同屋经常用他的热水壶,好像那是自己家的一样,可是碍于同学情面,又不好意思说.直到有一天,他和学姐小K吐槽. 学姐听了之后,说:大学集体生活里面,大部分东西都是默认室友可以共用的.如果你不想别人拿,我可以帮你做封印,只要打上private标记,它们就看不到你的东西,更加用不了你的东西了. 小明说哇靠学姐你还会妖法...... Swift语言从Xcode 6 beta 5版本起,加入了对权限控制

获得客房端的MAC(Media Access Control)地址

Get the clients MAC(Media Access Control) address, a hardware address that uniquely identifies each node of a network. Works great on LAN's. Firewalls and Proxy's will be an issue depending what side of them you're coding for. Can't Copy and Paste th

18.9. ACL - Access Control List

$ sudo modprobe loop $ dd if=/dev/zero of=file bs=1k count=100 $ sudo losetup /dev/loop0 file $ sudo mkfs.ext3 /dev/loop0 $ sudo mkdir /mnt/loop $ sudo mount -o rw,acl /dev/loop0 /mnt/loop/ $ sudo chown neo.neo -R /mnt/loop $ cd /mnt/loop/ 18.9.1. ge

Distributed Access Control System 1.4.27发布 访问控制系统

Distributed Access Control System简称DACS,是一个轻量级单点登录的http://www.aliyun.com/zixun/aggregation/38609.html">访问控制系统.它提供了灵活.模块化的验证方法,强大.透明的规则为基础的Web服务授权检查,CGI程序或几乎任何程序. Distributed Access Control System 1.4.27该版本进行了一些轻微的错误修正版,升级到第三方的支持包,包括Mac OS X10.7.2升