- Casting
- Boxing and Unboxing
- Byte addition exception
- Protected access specifier
- Object type vs reference type (reference type is used when accessing instance data, static data and static methods, object type is used in every other case)
- Finally block special case with return statements
- Special handling of exceptions thrown by AutoCloseable interface's close function using the
getSuppressed()
function - Just this slide is very important
5 == 5.0
returnstrue
5.1 == 5
returnsfalse
5.0 == 5L
returnsfalse
5f == 5d
returnstrue
5f == 5
returnstrue
-
int a = 15, b = 5; System.out.println(a + b + "ab" + a + b); // 20ab155
-
String a = "Str", b = "Str"; a == b; // true, since the reference is the same internally in the String pool a == new String("Str"); // false, since now the ref. is not same
-
String a = "123"; // created during compile time String b = "456"; // created during compile time String c = "123456"; // created during compile time String d = a + "456"; // created during runtime because of the presence of a variable String e = "123" + "456"; // created during compile time hence refers to the string in the string pool String f = new String("123"); // created during runtime because of the `new` keyword String g = "123"; // created during compile time hence refers to the string in the string pool System.out.println(c == d); // false System.out.println(c == e); // true System.out.println(a == f); // false System.out.println(a == g); // true
-
case '7': System.out.println("char"); break; case '0' + 7: System.out.println("int"); break; // compilation error, duplicate case label
- implicit casting takes place in compound assignment operators Eg:
byte b = 3; int i = 2; b += i;
will not give compilation error butb = b + i
will give compilation error - type casting can only take place for compatible data types with a parent-child relationship otherwise compilation error. (eg:
String str = (String)(23);
gives compilation error) - type casting of arrays can also take place if it has a parent-child relationship (eg:
Cars[] c = new Cars[2]; Suzuki[] s = new Suzuki[3]; c = s;
gives no error, and doesn't matter the array size of both)
- instant block scope are called before everytime any construcror is called
- static block scope are called only once when the class is created
String[] args
parameter variable does not need to be calledargs
- main method cannot be final or private
- arrays can also be declared with 3 dots(...) instead of square brackets([]). (For example:
String...
equalsString[]
) - arrays can be initialized as:
new int[] {1, 2, 3, 4, 5}
- array cannot be created with an array size of 0 or less than 0
- variable can have
_
(underscore) and/or all currency symbols($, €, £, ¥, ₣, ₹, ﷼, ₻, ₽, ₾, ₺, ₼, ₸, ₴, ₷, ฿, 원, ₫, ₮, ₯, ₱, ₳, ₵, ₲, ₪, ₰) in their names. - local and instance variables need to be initialized before being used otherwise Compilation error. Whereas array variables and instance variables are automatically set to their default values on initialization/declaration.
- defining values for long and float variables using literal values should be done by appending 'L' or 'F' to the literal otherwise compiler error will be thrown.
- octal-literal:
0123
(should only start with zero, thats it) (can have suffixes appended to it eg: 'L' suffixed for long value) - hex-literal:
0xff
('x' can also be uppercased) (can have suffixes appended to it eg: 'L' suffixed for long value) - binary-literal:
0B1001
('b' can also be lowercased) (can have suffixes appended to it eg: 'L' suffixed for long value) - number-system functions available in Java are:
Integer.toBinaryString(decimalNumber)
,Integer.toOctalString(decimalNumber)
,Integer.toHexString(decimalNumber)
,Integer.parseInt(number_in_string, number_base)
(eg:Integer.parseInt("100110", 2);
,Integer.parseInt("fa81dc8", 16);
) - default values for byte=
0
; short=0
; int=0
; long=0L
; float=0.0f
; double=0.0d
; char='\u0000'
(space character); boolean=false
; String(or any Object)=null
; - Local variables can't use any of the access modifiers. Final is the one and only non access modifier that can be applied to local variable.
- function arguments can be final because they are considered local variables.
- labels can only be compile-time constants or literals but not double, float or long literals.
- case label can have a maximum value of 65535(
\uffff
) and a minimum value of 0(\u0000
).
- runtime exceptions do not need to be caught (using try-catch block) or thrown by a function in the function-definition line
- a function can declare that it throws an exception but it may not actually throw any exception
- try-with-resource implicitly has a finally block so no catch or finally is necessarily required for try-with-resources
- default interface methods are inheritable but static interface methods are not
- all interface methods are implicitly public and abstract; top-level interfaces can have public or private-package(default) access
- all interface variables are public, static and final
- syntax for calling interface methods:
Iface.super.foo();
provided that the inteface methods are static or default methods - functional interfaces only has one abstract method. static, default and methods inherited from Object class do not count as abstract methods in functional interface
- enum values must be the first thing inside the enum block
- enum constructors are private by default, cannot be changed
- semicolon should be put to differentiate between the enum values and other enum parts (constructors, instance vars, functions, etc)
- A function taking in double arguments will also be able to take in int values but then the arguments will have the fraction
.0
appended to the number. - no
this
keyword inside static functions - overridden method cannot throw a wider range of exceptions or extra checked exceptions than that in its parent class
- overridden method should match the function definition with that in its parent class
- when two objects are equal through the
equals()
function then their hashcode i.e.hashcode()
function should return the same value - even abstract classes have a constructor
- cannot have two public classes in one java file
- when two string with same value is created (both created without using the
new
keyword) then both of them will point to the same reference of the string in the string pool hence they both will be equal through theequals()
method. - cannot pass
void
inSystem.out.print()
/System.out.println()
functions but can pass any primitive data types, arrays or objects. - implicit boxing will only happen with wrapper classes like Integer, String, etc but not with classes like StringBuilder, StringBuffer.
Boolean.valueOf(string_value_here)
will returntrue
only if the value is a case-insensitive "true" string like for eg: "true", "TrUe", "tRUE", "TRUe". Otherwise it will return false by default even for strings " true" and "true ".- unreachable code will give compilation error.
- anything after an infinite loop is unreachable.
- concatenating 2 string literals will result in a compile time constant
- 2 compile time string constants (both created without using a new keyword) will always point to the same string in the string pool.
(int)'A';
returns 65(int)'a';
returns 97(int)'0';
returns 48- Operators order of precedence: 1
Operators Precedence postfix expr++
expr--
prefix, unary ++expr
--expr
+expr
-expr
~
!
multiplicative *
/
%
additive +
-
shift <<
>>
>>>
relational <
>
<=
>=
instanceof
equality ==
!=
bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
logical AND &&
logical OR ||
ternary ? :
assignment =
+=
-=
*=
/=
%=
&=
^=
|=
<<=
>>=
>>>=
double a = 2.56f;
here implicit typecasting takes placevar var = 2;
since var is not a java keyword
var variable;
cannot be declared uninititalizedvar variable = null;
not valid since the compiler will not be able to determine the typevar<Integer> al = new ArrayList<Integer>();
List<var> al = new ArrayList<>();
var<var> al = new ArrayList<>();
var obj = (a, b) -> (a + b);
-
class Demo { var x = 50; // cannot be declared as an instance variable public static void main(String[] args) { System.out.println(x); } }
var method_name() { return ("Inside Method1"); }
cannot be used as a return type of a methodvoid method_name(var v) { return ("Inside Method1"); }
cannot be used as a data type of a method argument