Saturday, February 17, 2018

Java variables, primitive types, Array

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html


The Java programming language defines the following kinds of variables:


  • Instance Variables (Non-Static Fields) 
  • Class Variables (Static Fields)
  • Local Variables
  • Parameters



Primitive Types:

Data TypeDefault Value (for fields)
byte0
short0
int0
long0L
float0.0f
double0.0d
char'\u0000'
String (or any object)  null
booleanfalse


Array:

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.

// declares an array of integers
int[] anArray;
  1. type: int[]
  2. name: anarray
Note: an array declaration has two components: the array's type and the array's name. An array's type is written as type[]

// create an array of integers
anArray = new int[10];


If this statement is missing, then the compiler prints an error like the following, and compilation fails:
ArrayDemo.java:4: Variable anArray may not have been initialized.


Alternatively, you can use the shortcut syntax to create and initialize an array:
int[] anArray = { 
    100, 200, 300,
    400, 500, 600, 
    700, 800, 900, 1000
};

 A multidimensional array is an array whose components are themselves arrays.  A consequence of this is that the rows are allowed to vary in length.

        String[][] names = {
            {"Mr. ", "Mrs. ", "Ms. "},
            {"Smith", "Jones"}
        };


Copying Arrays

The System class has an arraycopy method that you can use to efficiently copy data from one array into another:
public static void arraycopy(Object src, int srcPos,
                             Object dest, int destPos, int length)

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.


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



Array Manipulations

Java SE provides several methods for performing array manipulations (common tasks, such as copying, sorting and searching arrays) in the java.util.Arrays class.

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));
    }
}

Some other useful operations provided by methods in the java.util.Arrays class, are:
  • Searching an array for a specific value to get the index at which it is placed (the binarySearch method).
  • Comparing two arrays to determine if they are equal or not (the equals method).
  • Filling an array to place a specific value at each index (the fill method).
  • Sorting an array into ascending order. This can be done either sequentially, using the sort method, or concurrently, using the parallelSort method introduced in Java SE 8. Parallel sorting of large arrays on multiprocessor systems is faster than sequential array sorting.
QUIZ:
  1. The term "instance variable" is another name for non-static field.
  2. The term "class variable" is another name for static field.
  3. A local variable stores temporary state; it is declared inside a method.
  4. A variable declared within the opening and closing parenthesis of a method is called a parameter.
  5. What are the eight primitive data types supported by the Java programming language? byte, short, int, long, float, double, boolean, char
  6. Character strings are represented by the class java.lang.String.
  7. An array is a container object that holds a fixed number of values of a single type.




No comments:

Post a Comment

java special for collection size, array size, and string size

Size: For Collections (eg: Map, List, etc ): usually it use collection.size(), eg         Map<Character, Integer> map ...