1/19
Looks like no tags are added yet.
Overloading
When two or more methods in the same class have the same method name. To be valid, any 2 definitions of the method name must have different signatures. A signature consists of the name of a method together with its parameter list. Differing signatures must have different numbers and/or types of parameters.
Overloading and automatic type conversion
If Java cannot find a method signature that exactly matches a method invocation, it will try to use automatic type conversion.
No argument constructor
If you do not include any constructors in your class, Java will automatically create a default or no-argument constructor that takes no arguments, performs no initializations, but allows the object to be created.
Overloading based on the type returned.
Java does not permit methods with the same name and different return types in the same class.
Default variable initializations
Instance variables are automatically initialized in Java. Boolean types are initialized to false, other primitives are initialized to the zero of their type. Class types are initialized to null.
Lists
Java has various forms of lists. Example of an ArrayList that stores strings is:
ArrayList<String> strlist = new ArrayList<String>();
Wrapper types
The process of converting from a primitive to an object is called “boxing“, the reverse process is called “unboxing“. In Java 5, the compiler converts back and forth itself and is known as autoboxing.
Array
Arrays in Java have some of the features of Python lists, including indexing, but unlike lists, their number of elements is fixed at creation time and is unchangeable after that.
Switch statement
It implements multiway branching. When a switch statement is evaluated, one of a number of different branches is executed. The choice of which branch to execute is determined by a controlling expression enclosed in parentheses after the keyword switch. The controlling expression must evaluate to a char, int, short or byte. Each branch statement in a switch statement starts with the reserved word case, followed by a constant called a case label, followed by a colon, and then a sequence of statements. Each case label must be of the same type as the controlling expression. There can also be a section labeled default. It is optional and is usually last.
Example of a switch statement

Equals vs ==
The == operator checks for reference equality, meaning it only returns true if both references point to the same memory location. The .equals() method is designed to check for value equality and is more appropriate for comparing the content of objects.
Example of the equals method in a class

Iterate over a list
ArrayList lst = new ArrayList(); lst.add(1); lst.add(1); lst.add(2); lst.add(3);
for (Integer i : lst) {
System.out.println(i)
}
Iterate over an array
int l[] = {1,1,2,3};
for(int i : l) {
System.out.println(i);
}
Boolean expressions
When 2 Boolean expressions are combined using ‘and‘ operator, the entire expression is true provided both expressions are true. When 2 expressions are combined using ‘or‘ operator, the entire expression is true as long as one of the expressions is true.
Short circuit evaluation
Java can take a shortcut when the evaluation of the first part of a Boolean expression produces a result that evaluation of the second part cannot change.
A do while statement
Is used to execute a portion of code and then repeat it based on the evaluation of a Boolean expression. The Boolean expression is checked after each iteration of the loop body
▪ If true, the loop body is executed again
▪ If false, the loop statement ends
Example of a do while statement
int a, b, c;
do {
System.out.println(“Enter 3 sides for a triangle.");
a = in.nextInt();
b = in.nextInt();
c = in.nextInt();
if (a*a + b*b != c*c) {
System.out.println("Not a right triangle: Try again!");
}
} while (a*a + b*b != c*c);
Break statement
When a break statement is executed, it ends the nearest enclosing switch or loop statement.
Continue statement
When executed, the continue statement ends the current loop body iteration of the nearest enclosing loop statement ▪ Note that in a for loop, the continue statement transfers control to the update expression
When loop statements are nested, remember that any break or continue statement applies to the innermost, containing loop statement.