This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Test { | |
int instanceVar = 10; | |
void hideInstance() { | |
int instanceVar = 15; | |
System.out.println("Local variable: " + instanceVar); //This will print local variable, as instance variable is hidden | |
//To access instance variable here, use this.instanceVariable | |
System.out.println("Instance variable: " + this.instanceVar); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileNotFoundException; | |
/** | |
* Most common checked and unchecked exceptions | |
* https://stackoverflow.com/questions/1263128/most-common-checked-and-unchecked-java-exceptions | |
*/ | |
class ExceptionExample { | |
//This is throwing (possibility of throwing) checked exception | |
void sampleMethod() throws FileNotFoundException { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Rule #1: | |
* If the super-class overridden method does not throw an exception, | |
* subclass overriding method can only throws the unchecked exception, | |
* throwing checked exception will lead to compile-time error. | |
* | |
* Rule #2: | |
* If the super-class overridden method does throws an exception, subclass | |
* overriding method can only throw same, subclass exception. Throwing parent | |
* exception in Exception hierarchy will lead to compile time error. Also there |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface one { | |
public void printOne(); | |
} | |
interface two { | |
public void printTwo(); | |
} | |
interface three extends one, two{ | |
public void printThree(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Car { | |
int number; | |
Car(int number) { | |
this.number = number; | |
} | |
void printNumber() { | |
System.out.println(this.number); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LabelsExample { | |
public static void main (String[] args) { | |
//Labels with break; Similar functionality like goto | |
outerLoop: | |
for (int i=0;i<10;i++) { | |
for (int j=0;j<10;j++) { | |
if (j == 1) { | |
System.out.println(j); | |
break outerLoop; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TestFinalVariable { | |
public final int foo; | |
// If variable is static and final, it must be initialized right away. | |
public static final int bar = 10; | |
//In constructor, initialization of final variable is allowed, as compiler knows that the constructor can only be called 1 time per object. | |
//Static variables can not be initialized even in constructor, as they are shared between multiple objects and that's why multiple object | |
//initialization can updathe the same copy of final variable, which is not allowed and thus static final variable must be initialized on declaration | |
TestFinalVariable(int foo) { | |
this.foo = foo; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.EnumSet; | |
import java.util.EnumMap; | |
enum Directions { | |
// Abstract method needs to be overriden in each of the enums | |
// First line of Enum should be always enum declaration. Constructor, variables declaration not allowed. | |
EAST(0) { | |
@Override | |
public void printDirection() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ShutDownHookExample { | |
public static void main(String[] args) { | |
Runtime.getRuntime().addShutdownHook(new Thread() { | |
public void run() { | |
System.out.println("Shutdown hook is running."); | |
} | |
}); | |
System.out.println("Application terminating."); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
T = np.load('datasets/T.npy') | |
for i in range(12): | |
for j in range(4): | |
print (T[i,:,j]) | |
''' | |
[0.9 0.1 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] for starting state (1,1) and action Down | |
[0.9 0. 0. 0. 0.1 0. 0. 0. 0. 0. 0. 0. ] for starting state (1,1) and action Left | |
[0.1 0.1 0. 0. 0.8 0. 0. 0. 0. 0. 0. 0. ] for starting state (1,1) and action Up |
NewerOlder