Thursday, June 29, 2017

Why do we need to go for Synchronized method ?


val = false
if(!val) {
   val = true;
}

Suppose say there are two threads A and B or even more....

Always the Interviewning time between two threads could be extremely less..probably depending upon the RAM speed of the server or our computer...

So If Thread A checks for the value of false...and before setting it as true..Thread B would access
the same if loop with the value set as false only...

So that's why we need to go for synchronized method so that only one thread can access that block at a time.

Synchronized method() {

val = false
if(!val) {
  val = true;
}

}

Boolean Operator or more than one exclamatory operator

public class Collectionsclassnotinrtdotjar {

        static boolean method(){
boolean moreThanOne = false;
if(!moreThanOne){
return !!!moreThanOne;
}else{
}
return fucking;
}
public static void main(String[] args) throws Exception {

System.out.println(method());

        }

}

Sunday, June 25, 2017

Autocloseable example

try(InputStream in1 = new FileInputStream(filename)){
}

try executing this...

Exception in ternary operator

Object a = (null==mode||"".equalsIgnoreCase(mode))?new Exception("ssssss"):"hj";
System.out.println(a);

final in try catch


What is the output of this program ?

final String c;
try
{
c = "a";
}
catch (Exception e)
{
c = "a";
}

Answer : Compilation error because c has been already assigned value in try block