Skip to main content

Java

Java - object-oriented, high-level, general-purpose programming language

Overview

Overview
NameStructureMethodsnullDuplicatesSynchronizedOrderedIteratorEnumeration
ArrayListDynamic Arrayadd, remove, get, setInsertion order
VectorDynamic Arrayadd, remove, get, setInsertion order
StackLIFOpush, pop, peekNo order
QueueFIFOenqueue, dequeue, peekNo order
ListLinearadd, remove, get, setInsertion order
LinkedListDoubly-Linked Listadd, remove, get, setInsertion order
SetCollectionadd, remove, containsNo order
HashSetHashTableadd, remove, containsNo order
Mapkey-value pairput, remove, get, containsKey1 key and multiple valuesonly valuesNo order
HashMapkey-value pairadd, remove, contains1 key and multiple valuesonly valuesNo order
HashTablekey-value pairput, remove, get, containsKeyonly valuesNo order
TreeSetRed-black treeadd, remove, containsNatural order
TreeMapRed-black treeput, remove, get, containsKeyonly valuesonly valuesNatural order

Collections

Collection Decision

Iterator vs Enumeration
AspectIteratorEnumeration
Typeinterfaceclass
Namespacejava.utiljava.util
Common methodshasNext / next / removehasMoreElements / nextElement
Use Caseremoves elements from collection during the iteration

used for passing through a collection, usually of unknown size

Practice

Practice
public static void main(String[] args) {
if (true)
break; // compile-time error: `break` is used inside switch and loops
}
public static void main(String[] args) {
System.out.println('j' + 'a' + 'v' + 'a'); // 106 + 97 + 118 + 97 = 418
}
public class Demo {
public static void main(String[] arr) {
Integer num1 = 100;
Integer num2 = 100;
Integer num3 = 500;
Integer num4 = 500;

if(num1==num2) { // num1 == num2
System.out.println("num1 == num2");
} else {
System.out.println("num1 != num2");
}

if(num3 == num4) { // num3 != num4
System.out.println("num3 == num4");
} else {
System.out.println("num3 != num4");
}
}
/*
Integer class has a caching range of -128 to 127. Whenever a number is between this range and autoboxing is used, it assigns the same reference
That's why for value 100, both num1 and num2 will have the same reference
For the value 500 (not in the range of -128 to 127), num3 and num4 will have different reference
*/
}
public static void main (String args[]) {
System.out.println(10 + 20 + "World"); // 30World
System.out.println("World" + 10 + 20); // World1020
System.out.println("World" + 10 * 20); // World200
}
class Test {
public static void main (String args[]) {
for(int i=0; 0; i++) // compile-time error: for loop demands a boolean value in the second part and we are providing an integer value, i.e., 0
{
System.out.println("Hello World");
}
}
}
class Test { int i; }

public class Main {
public static void main (String args[]) {
Test test = new Test();
System.out.println(test.i); // 0
}
}
class Test {
int test_a, test_b;

Test(int a, int b) {
test_a = a;
test_b = b;
}

public static void main (String args[]) {
Test test = new Test(); // compile-time error: no default constructor
System.out.println(test.test_a + test.test_b);
}
}
class OverloadingCalculation3 {

void sum(int a,long b){ System.out.println(1); }
void sum(long a,int b){ System.out.println(2); }

public static void main(String args[]) {
OverloadingCalculation3 obj = new OverloadingCalculation3();
obj.sum(20,20); // runtime error: ambiguity
}
}
class Base {
void method(int a) {
System.out.println(a);
}

void method(double d) {
System.out.println(d);
}
}

class Derived extends Base {
@Override
void method(double d) {
System.out.println(d);
}
}

public class Main {
public static void main(String[] args) {
new Derived().method(10); // Base class method called with integer a = 10
}
}
class Base {
public void baseMethod() {
System.out.println(1);
}
}

class Derived extends Base {
public void baseMethod() {
System.out.println(2);
}
}

public class Test {
public static void main (String args[]) {
Base b = new Derived();
b.baseMethod(); // Derived method called. Output: 2
}
}
abstract class Calculate {
abstract int multiply(int a, int b);
}

public class Main {
public static void main(String[] args) {
int result = new Calculate() {

@Override
int multiply(int a, int b) {
return a * b;
}
}.multiply(12,32);

System.out.println(result); // 384
}
}
class Calculation extends Exception {
public Calculation() {
System.out.println(1);
}

public void add(int a, int b) {
System.out.println(a+b);
}
}

public class Main {
public static void main(String []args) {
try {
throw new Calculation();
} catch(Calculation c) {
c.add(10,20); // Calculation class is instantiated. Output: 1, 30
}
}
}
public class Main {
void a() {
try {
System.out.println(1);
b(); // 2. method called
}catch(Exception e) {
System.out.println(2); // 8. Exception is caught
}
}

void b() throws Exception {
try{
System.out.println(3);
c(); // 3. method called
}catch(Exception e) { // 5. Exception is caught
throw new Exception(); // 6. throw exception
} finally {
System.out.println(4); // 7. finally block is called
}
}

void c() throws Exception {
throw new Exception(); // 4. throw exception
}

public static void main (String args[]) {
Main m = new Main();
m.a(); // 1. method called
}
}
public class Calculation {
int a;
public Calculation(int a) {
this.a = a;
}

public int add() {
a = a+10;
try {
a = a+10;
try {
a = a*10;
throw new Exception();
}catch(Exception e){
a = a - 10;
}
}catch(Exception e) {
a = a - 10;
}
return a;
}

public static void main (String args[])
{
Calculation c = new Calculation(10);
int result = c.add();
System.out.println("result = " + result); // result = 290
}
}
public class Test {
public static void main (String args[]) {
String s1 = "Hello";
String s2 = new String("Hello");
s2 = s2.intern();
System.out.println(s1 == s2); // true
/*
The intern method returns the String object reference from the string pool.
In this case:
s1 is created by using string literal
s2 is created by using the String pool
However, s2 is changed to the reference of s1, and the operator == returns true
*/
}
}
class Simple{
public Simple() { System.out.println(1); } // 1. Constructor is invoked
void message(){ System.out.println(2); } // 2. method is invoked
}

class Test1{
public static void main(String args[]) {
try {
Class c = Class.forName("Simple");
Simple s = (Simple) c.newInstance();
s.message(); // Output: 1, 2
} catch(Exception e) {
System.out.println(e);
}
}
}
public class Test {
Test(int a, int b) { }

Test(int a, float b) { }

public static void main (String args[]) {
byte a = 10;
byte b = 15;
Test test = new Test(a,b); // a = 10 b = 15
/*
Here, the data type of the variables a and b,
i.e., byte gets promoted to int,
and the first parameterized constructor with the 2 integer parameters is called
*/
}
}
public class Test{
Test() {
super(); // only 1 call can be made super() or this()
this(); // compile-time error: call to this must be first statement in constructor
System.out.println(1);
}

public static void main(String []args) {
Test t = new Test();
}
}
public class Animal {
void consume(int a) { System.out.println(a); }
static void consume(int a) { System.out.println(a); }

public static void main (String args[]) {
Animal a = new Animal();
a.consume(10); // compile-time error: method consume(int) is already defined in class Animal
Animal.consume(20); // compile-time error: non-static method consume(int) cannot be referenced from a static context
}
}
class Main {
public static void main(String args[]) {
final int i;
i = 20;
System.out.println(i); // 20
}
}
String s = new String("Welcome"); // 2 objects, one in string constant pool and other in non-pool (heap) is created
public static void main (String args[]) {
String a = new String("Hello");
String b = "Hello";

if(a == b) { System.out.println(1); } // false
if(a.equals(b)) { System.out.println(2); } // true
}