Thursday, 28 June 2018

Why main() in JAVA is public !!

Answer :
              main() in JAVA is public due to the following two reasons:

1. main() method can be accessed by any no. of classes.
2. main() method can be invoked by JVM from everywhere.


Note: The keyword public is a modifier.

Monday, 26 March 2018

Does finally always execute in Java?

Yes, finally will be called.
The only times finally won't be called are:
  1. If you invoke System.exit();
  2. If the JVM crashes first;
  3. If there is an infinite loop (or some other non-interruptable, non-terminating statement) in the try block;
  4. If the OS forcibly terminates the JVM process.
  5. If the host system dies; e.g. power failure, hardware error, etc.

Is Java “pass-by-reference” or “pass-by-value”?

Java is always pass-by-value. Unfortunately, they decided to call the location of an object a "reference". When we pass the value of an object, we are passing the reference to it. This is confusing to beginners.

Example;

public static void main( String[] args ) {
    Dog aDog = new Dog("Max");
    // we pass the object to foo
    foo(aDog);
    // aDog variable is still pointing to the "Max" dog when foo(...) returns
    aDog.getName().equals("Max"); // true, java passes by value
    aDog.getName().equals("Fifi"); // false 
}

public static void foo(Dog d) {
    d.getName().equals("Max"); // true
    // change d inside of foo() to point to a new Dog instance "Fifi"
    d = new Dog("Fifi");
    d.getName().equals("Fifi"); // true
}
In this example aDog.getName() will still return "Max". The value aDog within main is not changed in the function foo with the Dog "Fifi" as the object reference is passed by value. If it were passed by reference, then the aDog.getName() in main would return "Fifi" after the call to foo.

Can main() method overloaded ??

Yes, main() can be overloaded. Let’s see the following examples for better clarity :

ExampleMain.java

class ExampleMain
{
public static void main(String[] args)
{
System.out.println("Hiii");
}

public static void main(int x)
{
System.out.println("Hello");
}

}

Output
Hiii


Tuesday, 2 January 2018

JAVA 9 private method in interface

Hii frnds, here we will know an exciting feature of JAVA 9, that is definition of private method in interface. That means it is possible that we can happily define a private method inside the interface.
IntefaceExample.java
interface Test
{
void show();
default void print()
{
System.out.println("JAVA means SILAN TECHNOLOGY");
display();
}
private void display()
{
System.out.println("www.java8s.com");
}

}

class Demo implements Test
{
public void show()
{
System.out.println("SILAN TECHNOLOGY");
}
}

class InterfaceExample
{
public static void main(String[] args)
{
Demo obj=new Demo();
obj.show();
obj.print();
}

} 

Output
 SILAN TECHNOLOGY
JAVA means SILAN TECHNOLOGY
www.java8s.com

Monday, 18 December 2017

Can we compile and execute a java program without a class??

Ans: Yes, we can compile and execute without a class. Let’s see the following code:

Test43.java
enum Test43{
;  
public static void main(String[] args)
{
System.out.println("Silan Technology");
}

}

Output
Silan Technology

Friday, 8 December 2017

JAVA 9 Features

JAVA 9 Features :

1. Modularity -- jigsaw

2. REPL --  JShell

3. try with resource enhancements

4. Factory methods in collection

5. private methods in interface

6. Stream API enhancements

7. Http 2 Client(java.util.http)

8. G1 garbage collector