Studyon Minte9.com
Fundamentals (Docs)

Study

Variables



--- Fiedls

An object stores its state in fields.

- Instance Variables (non-static fields). Their values are unique to each instance of a class.
	class Bicycle {
		int speed = 10;
	}
        b1 = new Bicycle();
        b2 = new Bicycle();
        b2.speed = 50;

- Class Variables (Static Fields). A class variable is any field declared with the static modifier; this tells the 
compiler that there is exactly one copy of this variable in existence, regardless of how many times the class 
has been instantiated. 
	static int numGears = 6; // conceptually the same number of gears will apply to all instances
	final static int numGears = 6; // indicates that the number of gears will never change

- Local Variables. There is no special keyword designating a variable as local; that determination comes entirely 
from the location in which the variable is declared. Local variables are only visible to the methods in 
which they are declared.
	public method_a () {
		int b = 0; // local varible
	}

- Parameters
	static void main(String[] args) // args is the parameter


--- Naming

Variable names are case-sensitive. 
Convention is to always begin your variable names with a letter, not "$" or "_". 
	int speed = 0;
        int $speed = 0; // valid but not recommended
        int _speed = 0; // valid but not recommended

If it consists of more than one word, capitalize the first letter of each subsequent word.
	int currentGear = 0;

If your variable stores a constant value, capitalize every letter and separate subsequent words with the 
underscore character.

	static final int NUM_GEARS = 6;


Data Types

The Java programming language is statically-typed, which means that all variables must first be declared 
before they can be used. 
	int gear = 1;
Java programming language supports eight other primitive data types:
	byte (8 bit integer - it has a minimum value of -128 and a maximum value of 127)
	short (16 bit integer - it has a minimum value of -32,768 and a maximum value of 32,767)
	int (32 bit integer)
	long (64 bit integer)
	float (32 bit floating point)
	double (32 bit floating point)
	boolean (TRUE or FALSE)
	char (16 bit unicode character)
	String
	Java programming language also provides special support for character strings. Enclosing your 
character string within double quotes will automatically create a new String object; for example, String s = 
"this is a string"; String objects are immutable, which means that once created, their values cannot be 
changed.
Default variables
	byte - 0
	short - 0
	int - 0
	long - 0L
	float - 0.0f
	double - 0.0d
	char - 'u000'
	String - null
	boolean - false
Literals
You may have noticed that the new keyword isn't used when initializing a variable of a primitive type. 
A literal is the source code representation of a fixed value.
	boolean result = true;
	char capitalC = 'C';
	byte b = 100;
	short s = 10000;
	int i = 100000;
Integer Literals
An integer literal is of type long if it ends with the letter L or l; otherwise it is of type int.
	int decVal = 26;      // The number 26, in decimal
	int hexVal = 0x1a;    // The number 26, in hexadecimal
	int binVal = 0b11010; // The number 26, in binary
Floating Point Literals
A floating-point literal is of type float if it ends with the letter F or f; otherwise its type is double.
	double d1 = 123.4;
	double d2 = 1.234e2; // same value as d1, but in scientific notation
	float f1  = 123.4f;

Arrays

An array is a container object that holds a fixed number of values of a single type.
	int[] anArray; // declares an array of integers
	anArray = new int[10]; // allocates memory for 10 integers
	anArray[0] = 10; // initialize first element
	anArray[1] = 20;
Like declarations for variables of other types, an array declaration has two components: the array's type and
 the array's name.
Similarly, you can declare arrays of other types:
	byte[] anArrayOfBytes;
	short[] anArrayOfShorts;
	long[] anArrayOfLongs;
	float[] anArrayOfFloats;
	double[] anArrayOfDoubles;
	boolean[] anArrayOfBooleans;
	char[] anArrayOfChars;
	String[] anArrayOfStrings;
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};
Multidimensional array
	String[][] names = {{"aaa", "bbb"},
                            {"ccc", "ddd", "eee"}};
        System.out.println(names[0][1] + " " + names[1][names[1].length - 1]);
        // bbb eee
Arraycopy
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)
	---
	char[] arr1 = {'a', 'b', 'c', 'd', 'e'};
        char[] arr2 = new char[3];
        System.arraycopy(arr1, 2, arr2, 0, 3);
        System.out.println(new String(arr2));
        // cde