Array
In Java code, arrays are dynamically created objects. An array can contain several variables of the same type. These variables can be basic types or object references. An array can even contain other arrays.
Declare array Variables
When declaring an array variable, the Code creates a variable to include references to the array object. It does not create an array object or allocate space for array elements. The specified array size is invalid during declaration. Square brackets can appear at the beginning of the Declaration as part of the type, or as part of the array identifier:
Int [] I; // array of int
Byte B []; // array of byte
Object [] O, // array of Object
Short s [] []; // array of arrays of short
Build an array
You can use the new operator to construct an array. It must include the size of the array and the type of the elements it contains. For multi-dimensional arrays, you can only specify the size of the first dimension:
Int [] marks = new int [100];
String [] [] S = new string [3] [];
Initialize an array
The array initialization value can be written as a comma-separated expression in a column in curly brackets:
String s [] = {New String ("apple"), new string ("Mango ")};
Int I [] [] = {1, 2}, {3, 4 }};
You can also use loops to initialize Arrays:
Int I [] = new int [5];
For (Int J = 0; j <I. length; j ++)
{
I [J] = J;
}
Access array elements
The array index starts from 0 and ends with n-1, where N is the array size. To get the array size, use the array instance variable named length. If you try to access an index value out of the range 0 to n-1, an arrayindexoutofboundsexception will be thrown.
Declare classes, variables, and methods
Now let's take a look at the methods used to modify classes, methods, and variables. There are two modifiers: access modifier and non-access modifier. The access modifier allows us to restrict access to code or provide more access.
Class Modifier
Available access modifiers include public, private, and protected. However, a top-level class can only have public and default access levels. If no access modifier is specified, this class will have default access. Only the classes in the same package can see the classes with default access. If a class is declared as public, classes in other packages can also access it.
Let's take a look at the effects of non-access modifiers of some classes. Final keywords (for more information about keywords, see Java keywords and identifiers) are not allowed to extend classes. Abstract classes cannot be instantiated, but can be extended by quilt classes:
Public final class Apple {..}
Class greenapple extends Apple {}// not allowed, compile time error
Method modifier and variable Modifier
All access modifiers can be used for class members. Private Members can only access from inside the class. Protected members can only be accessed by classes in the same package or subclass of the class. Public members can be accessed by all other classes.
If no access modifier is specified, these Members will have default access, and only other classes in the same package can access them.
Now we will discuss other modifiers that can be used for member declarations. Some of them can only be used for methods, and others can only be used for variables, as shown in:
Figure 1. modifier of methods and variables
The synchronized method can only be accessed by one thread at a time. Transient variables cannot be serialized. Abstract methods are not implemented. They must be implemented by the first concrete subclass of the class they contain. At least a class containing the abstract method must be declared as abstract. However, abstract classes do not necessarily need to include Abstract METHODS:
Public abstract class myabstractclass
{
Public abstract void test ();
}
The native modifier indicates that this method is not written in Java but in a native language. Strictfp keywords (for more information about keywords, see Java keywords and identifiers) are only used for methods and classes, and it forces floating points to comply with iee754 standards. The variable can be declared as volatile. In this case, the thread must coordinate the work copy and master copy of the field each time the variable is accessed.
Static variables are shared by all instances of the class. You can use the static method and variable without any instance in this class:
Class statictest
{
Static int I = 0;
Static void getvar ()
{
I ++;
System. Out. println (I );
}
}
Class Test
{
Public static void main (string ARGs [])
{
Statictest. getvar ();
}
}
Constructors Use constructors when creating objects using classes. The constructor name must match the class name and have no return type. They can be overloaded, but not inherited by the quilt class. You can call constructors only from other constructors. To call the constructor in the same class, call this () function with matched parameters. To call the constructor in the superclass, call the super () function with matching parameters. When a subclass object is created, all super class constructors are called from top to bottom in the hierarchy. If the default constructor does not provide any other constructor in the class, the compiler creates the default constructor. It does not have any parameters. The default constructor calls the non-parameter constructor of the superclass. It has the same access modifier as the class. However, even if a constructor is written in the class, the compiler will not provide the default constructor. For example, the following class has a constructor that defines two parameters. If we try to instantiate this class without passing parameters, the compiler will give an error because there is no default constructor:
Class dot
{
Int X, Y;
DOT (int x, int y)
{
This. x = X;
This. Y = y;
}
}
If you call the default constructor of a class, and the super class does not have a constructor without parameters, your code cannot be compiled. The reason is that the default constructor of the subclass implicitly calls the non-parameter constructor of its superclass. For example:
Class dot
{
Int X, Y;
DOT (int x, int y)
{
This. x = X;
This. Y = y;
}
}
Class mydot extends dot {}
Class Test
{
Public static void main (string ARGs [])
{
Mydot dot = new mydot ();
}
}
Summary This section discusses important concepts in the first objective. We have discussed how to declare and construct one-dimensional and multi-dimensional arrays. When understanding the effects of methods and class modifiers, you must master the valid combination of modifiers. For example, a final method cannot be declared as abstract. We also learned about constructor. Remember, the compiler only provides the default no-argument constructor if you have not compiled Any constructor.
Exercise
Problem:
What are the results of compiling and running the following programs?
Class box
{
Int B, W;
Void box (int B, int W)
{
This. B = B;
This. W = W;
}
}
Public class mybox extends box
{
Mybox ()
{
Super (10, 15 );
System. Out. println (B + "," + W );
}
Static public void main (string ARGs [])
{
Mybox box = new mybox ();
}
}
Option:
A. It cannot be compiled. The main method is not declared correctly.
B. Output 10, 15
C. Output 0, 0
D. None of the above
Correct answer:
D
Note:
This program cannot be compiled, because super (10, 15) in the subclass constructor is called and there is no matching constructor in the base class. Void box (int B, int W) is not a constructor because it provides the return type. If it is a constructor, the variables W and H will be initialized to 10 and 15. This program can be correctly compiled and output for 10, 15. There is no error in the declaration of the main () method. Static and public can appear in any order.