Monday 17 December 2018

JAVA ArrayList Constructors

JAVA ArrayList Constructors


                 java.util.AbstractList
                          java.util.ArrayList

public class ArrayList
extends AbstractList
1.    ArrayList() : Create an empty ArrayList object with default initial capacity 10. Once ArrayList reaches its max capacity then new ArrayList will be created with a new capacity.
new capacity=(current capacity*3/2)+1
i.e (10*3/2)+1=16 
after this new ArrayList is created the previous one will be deleted and it will be treated as garbage.

2.    ArrayList(int initialcapacity) : Constructs an empty ArrayList with the specified initial capacity given by the user.
3.    ArrayList(Collection c) : For every Collection object, we can create an equivalent ArrayList.
ArrayListExample1.java
importjava.util.*;
classArrayListExample1
{
    public static void main(String[] args)
    {
            ArrayList al=new ArrayList();
            al.add("Sashi");
            al.add("Satyajit");
            al.add(20);
            al.add(null);
            System.out.println(al);          //[Sashi,Satyajit,20,null]
            al.remove(1);
           al.add(1,"Sasmita");
           al.add("Nilan");
           System.out.println(al);           //[Sashi,Sasmita,20,null,Nilan]
      }
}
Output
[Sashi,Satyajit,20,null]
[Sashi,Sasmita,20,null,Nilan]


Thanks & Regards:
Trilochan Tarai
SILAN Technology
java8s.com
0674-2361252

Wednesday 7 November 2018

Difference between JAVA Collection & Collections


Collection is a root level interface of the Java Collection Framework. Most of the classes in Java Collection Framework implements from this interface. List, Set and Queue are main sub interfaces of this interface. JDK doesn’t provide any direct implementations of this interface. But, JDK provides direct implementations of it’s sub interfaces. ArrayList, Vector, HashSet, LinkedHashSet, PriorityQueue are some indirect implementations of Collection interface. Map interface, which is also a part of java collection framework, doesn’t inherit from Collection interface. Collection interface is a member of java.util package.
Collections is an utility class in java.util package. It consists of only static methods which are used to operate on objects of type Collection. For example, it has the method to find the maximum element in a collection, it has the method to sort the collection, it has the method to search for a particular element in a collection. Below is the list of some important methods of Collections class.
Collections.max()
Collections.min()
Collections.sort()
Collections.synchronizedCollection()



Thanks & Regards:
Trilochan Tarai
9439202111

Thursday 11 October 2018

JAVA 11 Features

Some JAVA 11 Features


1. Nest-Based Access Control
2. Dynamic Class-File Constants
3. Epsilon: A No-Op Garbage Collector
4. Remove the JAVA EE and CORBA Modules
5. HTTP Client(Standard)
6. Local-Variable Syntax for Lambda Parameters
7. Unicode 10
8. Flight Recorder


Thanks & Regards:
Trilochan Tarai
SILAN Technology & java8s.com
+91-9439202111
0674-2361252

Tuesday 21 August 2018

Why JAVA is Object-Oriented ???

Why JAVA is OO(Object-Oriented) ??

A Java Program is nothing but a class or more than one class. Everything we will write inside a class. That concept is Object Oriented.

Another fact is Object Oriented Features are supporting like Class, Object, Data Abstraction & Encapsulation, Inheritance, Polymerphism, Dynamic Binding, Message Passing etc.

Another important point is when a method is invoking by an object then the method definition is executing. This is object oriented concept, JAVA is supporting.




Thanks & Regards:
Trilochan Tarai
JAVA Expert
SILAN Technology & java8s.com
trilochan@silantechnology.com
9439202111

What is JAVA ???

1. JAVA is a general purpose high level programming language.

2. JAVA is a technology.

3. JAVA is an OOP(Object Oriented Programming) Language.

4. JAVA is a platform.


Note : JAVA is itself  a platform because JAVA having its own JRE(JAVA Run-time Environment).



Thanks & Regards:
Trilochan Tarai
JAVA Expert
SILAN Technology & java8s.com
9439202111

Sunday 12 August 2018

Why can't i use static variable in java constructor?

Example:
class Student{
Student() {
static int i = 0;
System.out.println(i++); 
}
public static void main(String args[]){
Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
}

Here, Why can't i use static variable in java constructor?

Ans:
If you want to declare static variable then declare it outside of the constructor, at class level like this -
public class Student{
private static int i;
}
You declaration of static occurred at your constructor which is a local variable and local variable can not be static. And that's why you are getting - illegal modifier for parameter i. And finally for initializing static variable you may use a static initialization block (though it's not mandatory) -
public class Student{
private static int i;
static {
i = 5;
}
}

Tuesday 7 August 2018

JAVA 10 exciting Feature : Local Variable Type Inference

JAVA 10 :  Local Variable Type Inference
Like JavaScript, Kotlin, and Scala, now Java 10 introduced var keyword that allows you to declare a local variable without specifying its type. The type will be inferred from context. For example, when you say var name = "Silan", then the compiler will already know the type is String.
The var keyword can only be used for local variables, i.e. variables inside methods or code blocks — you cannot use it for member variable declaration inside the class body.
But like Python, it doesn't make Java a dynamically typed language. Java is still a statically typed language, and once the type is assigned, you cannot change it. For example, var name = "Silan" is ok, but if we will assign name =100; then it is not right.
This is one of the most eye-catching features of Java 10. It reduces the amount of boilerplate code needed to declare local variables in Java.
Overall we can say in a single statement that we can declare variables without specifying the associated type. A declaration such as:
List<String> l=new ArrayList<String>();
Can be written as var l=new ArrayList<String>();

Note: Type Inference is not a new Concept in JAVA but it is a new concept for local variables. 

Tuesday 3 July 2018

Conversion from String to StringBuffer and StringBuilder

    Here in this context We can directly pass String class object to StringBuffer and StringBuilder class constructors. As String class is immutable in java, so for editing a string, we can perform same by converting it to StringBuffer or StringBuilder class objects. 

Example.java
package java8s;

public class Example {

          public static void main(String[] args) {
                  
                   String str="SILAN";
                  
                   //Conversion from String object to StringBuffer
                   StringBuffer sb1=new StringBuffer(str);
                  
                   System.out.println(sb1);
                  
                   sb1.reverse();
                   System.out.println(sb1);
                  
                   //Conversion from String object to StringBuilder
                    StringBuilder sb2=new StringBuilder(str);
                   sb2.append("Technology");
                  
                   System.out.println(sb2);
                                     

          }

}

Output:
SILAN 
NALIS
SILANTechnology

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.