Static variables and methods belong to the class rather than any instance. They are shared across all instances of the class. A static method cannot access a non-static variable because an instance is not present. Static variables and methods are accessed using the class name rather than an instance. Static initialization blocks run when the class is first loaded.
2. Static variables and Methods
❖
❖
Variables and methods marked as static belongs to class, rather than any particular
instance.
One copy is shared across all class instances.
➢
❖
❖
❖
❖
●
Ex : https://gist.github.com/rajeevprasanna/8754235
A static method can’t access a nonstatic(instance) variable, because there is not
instance. ex : https://gist.github.com/rajeevprasanna/8754328
Static=class, nonstatic=instance.
Access static method or variable by dot operator on className.
Static methods can’t be overridden.
ex : https://gist.github.com/rajeevprasanna/8754437
Static or initialization block: Initialization blocks run when the class is first loaded (a static
initialization block) or when an instance is created. ex : https://gist.github.com/rajeevprasanna/8756880
3. Data types and primitives
Data Types : Primitive types are special data types built into the language; they
are not objects created from a class
Literal : A Literal is the source code representation of a fixed value; literals are
represented directly in your code without requiring computation
boolean result = true;
boolean - is data type
true - is literal
Refer : http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
4. Float and Double
➔
➔
➔
A float is a 32 bit IEEE 754 floating point.
A double is a 64 bit IEEE 754 floating point.
You shouldn't ever compare floats or doubles for equality; because, you
can't really guarantee that the number you assign to the float or double is
exact.
Ex : https://gist.github.com/rajeevprasanna/8754840
5. Character literals
●
●
Char literal is represented by a single character in single quotes
It stores the 16-bit Unicode integer value of the character in question
■ char a = 'a';
■ char letterN = 'u004E'; // The letter 'N' in unicode representation
■ char a = 0x892;
// hexadecimal literal
■ char b = 982;
// int literal
■ char c = (char)70000; // The cast is required; 70000 is out of char range
■ char d = (char) -98; // Ridiculous, but legal
■ char e = -29; //Error : Possible loss of precision; needs a cast
■ char f = 70000 //Error : Possible loss of precision; needs a cast
●
Use an escape code if you want to represent a character that can't be typed in as a literal,
including the characters for line feed, newline, horizontal tab, backspace, and single quotes.
■ char c = '"'; // A double quote
■
●
char d = 'n'; // A newline
If given character is out of range, it is displayed as ?. Refer table here
○ Ex : https://gist.github.com/rajeevprasanna/8755247
6. Assignment operators
●
●
Result of an expression involving anything int-sized or smaller is always an int
Multiply an int and a short and you'll get an int. Divide a short by a byte and you'll get...an int.
ex : https://gist.github.com/rajeevprasanna/8755470
●
●
●
Assigning Floating-Point Numbers : In order to assign a floating-point literal to a
float variable, you must either cast the value or append an f to the end of the literal(compiler
treat as double if no casting is there).
○
float f = (float) 32.3
○
float g = 32.3f
○
float h = 32.3F
byte a = 128; // Error : byte can only hold up to 127(Assigning a Literal That Is Too Large for the
Variable)
Array instance variable initialization : Array elements are always, always, always given
default values, regardless of where the array itself is declared or instantiated.
7. Passing variables into methods
❖
❖
❖
Passing primitive variables
Passing reference variables
Does java use Pass-By-Value ?
➢
❖
❖
Java is actually pass-by-value for all variables running within a single VM. Pass-by-value
means pass-by-variable-value. And that means, pass-by-copy-of- the-variable!
➢ ex : https://gist.github.com/rajeevprasanna/8755774
Shadowing instance/static primitive variables.
➢ ex: https://gist.github.com/rajeevprasanna/8755805
Shadow instance reference variables.
➢ ex : https://gist.github.com/rajeevprasanna/8755845
8. Arrays
❖
❖
❖
❖
Declaring arrays
➢ int[] key;//preferred, int key []; (primitive array declaration)
➢ Thread[] threads; //Declaring array of object references.
Array object is created on the heap.
You must specify the size of the array at creation time. The size of the array is the number of
elements the array will hold.
One dimensional array :
➢ int[] testScores;
// Declares the array of ints
testScores = new int[4];
❖
❖
❖
➢ int[] testScores = new int[4];//Initialize at the time of declaration.
➢ int[] carList = new int[]; // Will not compile; needs a size
Multi dimensional array :
➢ Multidimensional arrays, remember, are simply arrays of arrays.
➢ So a two- dimensional array of type int is really an object of type int array (int []), with each
element in that array holding a reference to another int array.
➢ int[][] myArray = new int[3][]; //valid
Array initialization: https://gist.github.com/rajeevprasanna/8756729
Array index starts from 0 to arrayLength-1.
9. Wrapper class
The wrapper classes in the Java API serve two primary purposes:
●
●
●
●
To provide a mechanism to "wrap" primitive values in an object so that the primitives can be
included in activities reserved for objects, like being added to Collections, or returned from a
method with an object return value.
To provide an assortment of utility functions for primitives. Most of these functions are related
to various conversions: converting primitives to and from String objects, and converting
primitives and String objects to and from different bases (or radix), such as binary, octal, and
hexadecimal.
Wrapper objects are immutable. Once they have been given a value, that value cannot be
changed.
Methods : valueOf, xxxValue, toXxxString,
10. Overloading
●
●
●
●
Widening. ex : https://gist.github.com/rajeevprasanna/8757265
Autoboxing
Var-args
When overloading, compiler will choose widening over boxing.
○ Widening beats boxing
○ Widening beats var-args
○ Boxing beats var-args
○
●
ex : https://gist.github.com/rajeevprasanna/8757252
Widening reference variables. ex: https://gist.github.com/rajeevprasanna/8757479
11. Equality operator
❖
❖
❖
❖
== equals (also known as "equal to")
!= not equals (also known as "not equal to")
Equal operation on primitives, references and enums.
➢ ex: https://gist.github.com/rajeevprasanna/8757682
instanceof Comparison:
➢
❖
The instanceof operator is used for object reference variables only, and you can use it to
check whether an object is of a particular type.
➢ By type, we mean class or interface type—in other words, if the object referred to by the
variable on the left side of the operator passes the IS-A test for the class or interface type
on the right side
■ ex: https://gist.github.com/rajeevprasanna/8757785
➢ You can't use the instanceof operator to test across two different class hierarchies.
■ ex: https://gist.github.com/rajeevprasanna/8757802
Remember that arrays are objects, even if the array is an array of primitives
➢ int [] nums = new int[3]; if (nums instanceof Object) { } // result is true