StringBuffer capacity vs ensureCapacity and trimtoSize()

package com.me.practice;

public class StringBufferClass {
public static void main(String[] args) {

//By default it’s 0
StringBuffer stringBuffer=new StringBuffer();

//By default it’s 16
System.out.println(stringBuffer.capacity());

//Now appending “xxxx”
stringBuffer.append(“madhu”);

//Capacity won’t change if length < default capacity
System.out.println(stringBuffer.capacity());
System.out.println(stringBuffer.length());

//Capacity will change if length > default capacity
stringBuffer.append(“madhugggggggggggggggggggggggg”);
System.out.println(stringBuffer.capacity());
System.out.println(stringBuffer.length());

//Capacity won’t change if ensureCapacity < current capacity
stringBuffer.ensureCapacity(23);
System.out.println(stringBuffer.capacity());
System.out.println(stringBuffer.length());

//Capacity will change if ensureCapacity > current capacity
stringBuffer.ensureCapacity(123);
System.out.println(stringBuffer.capacity());
System.out.println(stringBuffer.length());

//Capacity will reduce to actual size
stringBuffer.trimToSize();
System.out.println(stringBuffer.capacity());

}
}

 

16
16
5
34
34
34
34
123
34
34

Advertisements

ScheduledThreadPool in JAVA

A ThreadPoolExecutor that can additionally schedule commands to run after a given delay, or to execute periodically. This class is preferable to Timer when multiple worker threads are needed, or when the additional flexibility or capabilities of ThreadPoolExecutor (which this class extends) are required.

Delayed tasks execute no sooner than they are enabled, but without any real-time guarantees about when, after they are enabled, they will commence. Tasks scheduled for exactly the same execution time are enabled in first-in-first-out (FIFO) order of submission.

https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledThreadPoolExecutor.html

Timer VS ScheduledThreadPool

223down voteaccepted

According to Java Concurrency in Practice:

  • Timer can be sensitive to changes in the system clock, ScheduledThreadPoolExecutor isn’t.
  • Timer has only one execution thread, so long-running task can delay other tasks. ScheduledThreadPoolExecutor can be configured with any number of threads. Furthermore, you have full control over created threads, if you want (by providing ThreadFactory).
  • Runtime exceptions thrown in TimerTask kill that one thread, thus making Timer dead ūüė¶ … i.e. scheduled tasks will not run anymore. ScheduledThreadExecutor not only catches runtime exceptions, but it lets you handle them if you want (by overriding afterExecute method from ThreadPoolExecutor). Task which threw exception will be canceled, but other tasks will continue to run.

If you can use ScheduledThreadExecutor instead of Timer, do so.

 

ScheduledExecutorService scheduledExecutorService=
Executors.newScheduledThreadPool(10);

for(int i=0;i<10;i++){
Thread_Creation creation=new Thread_Creation(“meeee”);
scheduledExecutorService.schedule(creation, 10, TimeUnit.SECONDS);
//Wiil do parallelly
}

Note that all the schedule() methods return instance of ScheduledFuture that we can use to get the thread state information and delay time for the thread.

ScheduledFuture extends Future interface, read more about them at Java Callable Future Example.

There are two more methods in ScheduledExecutorService that provide option to schedule a task to run periodically.

 

http://www.journaldev.com/2340/java-scheduler-scheduledexecutorservice-scheduledthreadpoolexecutor-example

 

Timer and TimerTask -Execute a task for every 5 seconds in JAVA

https://docs.oracle.com/javase/7/docs/api/java/util/Timer.html

https://docs.oracle.com/javase/7/docs/api/java/util/TimerTask.html

 

package com.practice.thread;

import java.util.Timer;
import java.util.TimerTask;

public class TimerClass extends TimerTask{

@Override
public void run() {
// TODO Auto-generated method stub
System.out.println(“——–START———–“);
SomeWork();
System.out.println(“————–END——–“);
}

private void SomeWork() {
System.out.println(“INSIDE LOGIC PROGERSSSSSS”);
}

public static void main(String[] args) {
TimerClass timerTask=new TimerClass();
//have cancel() ,run() and scheduledExecutionTime() to know the time of task execution

Timer timer=new Timer();
//Run as daemon thred,By default thread is not daemon
//uses FIFO
//By default it is Thread-safe
//It uses Object.wait(long) to schedule .

//timer.schedule(timerTask, 1000,5000);

timer.scheduleAtFixedRate(timerTask, 0, 5000);
//Schedules the specified task for execution at the specified time.
//If the time is in the past, the task is scheduled for immediate execution.

//timer.cancel();

}

}

 

——–START———–
INSIDE LOGIC PROGERSSSSSS
————–END——–
——–START———–
INSIDE LOGIC PROGERSSSSSS
————–END——–
——–START———–
INSIDE LOGIC PROGERSSSSSS
————–END——–
——–START———–
INSIDE LOGIC PROGERSSSSSS
————–END——–
——–START———–
INSIDE LOGIC PROGERSSSSSS
————–END——–
——–START———–
INSIDE LOGIC PROGERSSSSSS
————–END——–

…………………………….

…………….continues………………….

ThreadPoolExecutor(C) in JAVA

  • ThreadPoolExecutor class used for executing a group of threads by using the Executors(I) factory methods.
  • It is a known implementation of Executor(I),ExecutorService(I) and it allows callble or runnable objects as params.
  • It creates the thread objects and executes it and destirys it by using of varoius methods in ThreadPoolExecutor class.

Thread pools address two different problems: they usually provide improved performance when executing large numbers of asynchronous tasks, due to reduced per-task invocation overhead, and they provide a means of bounding and managing the resources, including threads, consumed when executing a collection of tasks. Each ThreadPoolExecutor also maintains some basic statistics, such as the number of completed tasks.

  • getcurrentPoolSize() ==> gives current pool elemnts size
  • getCorepoolSize() ¬† ¬† ====> Intial size i.e no of Queue elemnts.
  • getMaxPoolSize() ¬† ¬† =====> Large size ¬†i.e MAX elements.
  • When the new task is submitted to execute() and less than getCuurentSize() ¬† a new thread is created.
  • if more than coresize and less than max size then a thread created in the queue(synchronus queue,LinkedBlockingqueu,ArrayblockingQueue).

A detailed desc is below.

 

Source:

package com.practice.thread;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class Thread_Creation implements Runnable{

public String stringThread=null;

public Thread_Creation(String stringThread) {
super();
this.stringThread = stringThread;
}

public String getStringThread() {
return stringThread;
}

public void setStringThread(String stringThread) {
this.stringThread = stringThread;
}

@Override
public String toString() {
// TODO Auto-generated method stub
return this.stringThread.toString();
}

@Override
public void run() {
// TODO Auto-generated method stub
// default-method in runnable(I)

System.out.println(“Start”);
System.out.println(Thread.currentThread().getName()+stringThread);
logicMethod();
System.out.println(“stop”);
System.out.println(Thread.currentThread().getName()+stringThread);

}

private void logicMethod() {
// TODO Auto-generated method stub
//We are simply sleep it for 5 sec.
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

public class Simple_Thread{
public static void main(String[] args) {
Runnable runnable=null;
ExecutorService executorService=Executors.newFixedThreadPool(5);
//It will creates the new threads of 15.
//One dis-advantage is if num is big it get overloaded.

for(int i=0;i<10;i++){
runnable=new Thread_Creation(” “+i);
executorService.execute(runnable);
}
}
}

 

Start
Start
Start
Start
pool-1-thread-1 0
Start
pool-1-thread-3 2
pool-1-thread-5 4
pool-1-thread-2 1
pool-1-thread-4 3
stop
stop
stop
stop
pool-1-thread-3 2
stop
pool-1-thread-5 4
Start
pool-1-thread-3 5
Start
pool-1-thread-5 6
pool-1-thread-2 1
pool-1-thread-4 3
pool-1-thread-1 0
Start
Start
pool-1-thread-4 8
Start
pool-1-thread-2 7
pool-1-thread-1 9
stop
stop
pool-1-thread-5 6
pool-1-thread-3 5
stop
pool-1-thread-1 9
stop
pool-1-thread-2 7
stop
pool-1-thread-4 8

 

package com.practice.thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

class Rejected_Threads implements RejectedExecutionHandler{

@Override
public void rejectedExecution(Runnable runnable, ThreadPoolExecutor arg1) {
// TODO Auto-generated method stub
System.out.println(runnable+”got rejected:”);
}

}

class Mainlogic_Thread implements Runnable{

public ThreadPoolExecutor executor;
public boolean run=true;
public int seconds;

public Mainlogic_Thread(ThreadPoolExecutor threadpoolObject, int seconds) {
super();
this.executor = threadpoolObject;
this.seconds = seconds;
}

public void shutdown(){
this.run=false;
}

@Override
public void run() {
// TODO Auto-generated method stub
while(run){
System.out.println(
String.format(“[monitor] [%d/%d] Active: %d, Completed: %d, Task: %d, isShutdown: %s, isTerminated: %s”,
this.executor.getPoolSize(),
this.executor.getCorePoolSize(),
this.executor.getActiveCount(),
this.executor.getCompletedTaskCount(),
this.executor.getTaskCount(),
this.executor.isShutdown(),
this.executor.isTerminated()));
try {
Thread.sleep(seconds*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}
}
public class Thread_pool_executer {
public static void main(String[] args) throws InterruptedException {
Rejected_Threads rejected_Threads=new Rejected_Threads();
ThreadFactory threadFactory=Executors.defaultThreadFactory();
ThreadPoolExecutor threadPoolExecutor=new ThreadPoolExecutor(2, 4, 10,
TimeUnit.SECONDS, new ArrayBlockingQueue<>(2),threadFactory,rejected_Threads);

//Notice that while initializing the ThreadPoolExecutor, we are keeping initial pool
//size as 2, maximum pool size to 4 and work queue size as 2. So if there are 4
//running tasks and more tasks are submitted, the work queue will hold only 2 of
//them and rest of them will be handled by RejectedExecutionHandlerImpl.

Mainlogic_Thread mainlogic_Thread=new Mainlogic_Thread(threadPoolExecutor, 3);
Thread thread=new Thread(mainlogic_Thread);
thread.start();

for(int i=0; i<10; i++){
threadPoolExecutor.execute(new Thread_Creation(“cmd”+i));
}

System.out.println(threadPoolExecutor.getCorePoolSize());
System.out.println(threadPoolExecutor.getMaximumPoolSize());

Thread.sleep(30000);
//shut down the pool
threadPoolExecutor.shutdown();
//It won’t execute any pending taks if we call shutdown()
//shut down the monitor thread
//If shout down is called it goes to rejection if we try to put it in executor queue
threadPoolExecutor.execute(new Thread_Creation(“nene”+2));
Thread.sleep(5000);
mainlogic_Thread.shutdown();

}
}

 

Start
cmd6got rejected:
pool-1-thread-1cmd0
cmd7got rejected:
Start
cmd8got rejected:
pool-1-thread-2cmd1
cmd9got rejected:
2
4
Start
pool-1-thread-3cmd4
Start
pool-1-thread-4cmd5
[monitor] [0/2] Active: 0, Completed: 0, Task: 6, isShutdown: false, isTerminated: false
stop
stop
pool-1-thread-2cmd1
pool-1-thread-1cmd0
Start
pool-1-thread-2cmd2
Start
pool-1-thread-1cmd3
stop
stop
pool-1-thread-3cmd4
pool-1-thread-4cmd5
stop
stop
pool-1-thread-1cmd3
pool-1-thread-2cmd2
[monitor] [4/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [4/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [4/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
nene2got rejected:
[monitor] [0/2] Active: 0, Completed: 6, Task: 6, isShutdown: true, isTerminated: true
[monitor] [0/2] Active: 0, Completed: 6, Task: 6, isShutdown: true, isTerminated: true

ThreadFactory(I) in JAVA

Here are some notes on ThreadFactory(I):

A Sample Programme:

package com.practice.thread;

import java.awt.List;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.concurrent.ThreadFactory;

class LogicThread implements Runnable{

@Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
}

}

class ThreadFactoryImpl implements ThreadFactory{
private String threadName;
public ThreadFactoryImpl(String threadName) {
super();
this.threadName = threadName;
this.listThreads = new ArrayList<String>();
}
private int counter=1;
private java.util.List<String> listThreads;

@Override
public Thread newThread(Runnable r) {
// TODO Auto-generated method stub
Thread t = new Thread(r, threadName + “-Thread_” + counter);
counter++;
listThreads.add(String.format(“Created thread %d with name %s on %s \n”, t.getId(), t.getName(), new Date()));
return t;
}

public String getThreadValues(){
StringBuffer stringBuffer=new StringBuffer();
Iterator<String> iterator=listThreads.iterator();
while(iterator.hasNext()){
stringBuffer.append(iterator.next());
}
return stringBuffer.toString();
}
}
public class ThreadFactoryExample {
public static void main(String[] args) {
ThreadFactoryImpl threadFactoryImpl=new ThreadFactoryImpl(“Thread_Factory”);
LogicThread logicThread=new LogicThread();
//Thread logic i.e sleep
Thread thread;
for(int i=0;i<10;i++){
thread=new Thread(logicThread);
thread=threadFactoryImpl.newThread(thread);
//Creating thread using Facoty Impl by using newThread()
thread.start();
}
System.out.println(threadFactoryImpl.getThreadValues());
}
}

 

Created thread 11 with name Thread_Factory-Thread_1 on Sun Jan 22 14:24:17 IST 2017
Created thread 13 with name Thread_Factory-Thread_2 on Sun Jan 22 14:24:17 IST 2017
Created thread 15 with name Thread_Factory-Thread_3 on Sun Jan 22 14:24:17 IST 2017
Created thread 17 with name Thread_Factory-Thread_4 on Sun Jan 22 14:24:17 IST 2017
Created thread 19 with name Thread_Factory-Thread_5 on Sun Jan 22 14:24:17 IST 2017
Created thread 21 with name Thread_Factory-Thread_6 on Sun Jan 22 14:24:17 IST 2017
Created thread 23 with name Thread_Factory-Thread_7 on Sun Jan 22 14:24:17 IST 2017
Created thread 25 with name Thread_Factory-Thread_8 on Sun Jan 22 14:24:17 IST 2017
Created thread 27 with name Thread_Factory-Thread_9 on Sun Jan 22 14:24:17 IST 2017
Created thread 29 with name Thread_Factory-Thread_10 on Sun Jan 22 14:24:17 IST 2017

Thread_Factory-Thread_1
Thread_Factory-Thread_3
Thread_Factory-Thread_2
Thread_Factory-Thread_4
Thread_Factory-Thread_9
Thread_Factory-Thread_5
Thread_Factory-Thread_6
Thread_Factory-Thread_7
Thread_Factory-Thread_8
Thread_Factory-Thread_10

 

HashMap vs TreeMap vs HashTable vs concurrentHashMap values and keys

import java.util.*;
import java.util.concurrent.*;

public class StaticVsNormalVsNo_Imports{
public static void main(String[] args)throws Exception{
ArrayList<Integer> arraylist=new ArrayList<Integer>();
System.out.println(arraylist.add(10));
System.out.println(arraylist);

//Key shouldn’t be null in treemap
//If we put two keys similar it will ovverride
//multiple Null values are allowed
TreeMap<String,String> treeMap=new TreeMap<String,String>();
treeMap.put(“null”,null);
treeMap.put(“null1”,null);
treeMap.put(“null”,”nuller”);
treeMap.put(“null2″,null);
System.out.println(treeMap);

//one key can be null
//If we put two keys similar it will ovverride
//Multiple null values allowed
HashMap<String,String> hashMap=new HashMap<String,String>();
hashMap.put(null,null);
hashMap.put(null,”null1”);
hashMap.put(“null1”,null);
hashMap.put(“null2”,null);
hashMap.put(“null3″,null);
hashMap.put(null,”nuller”);
System.out.println(hashMap);

//No nulls as keys as well as values too
Hashtable<String,String> hashTable=new Hashtable<String,String>();
hashTable.put(“null”,”null-1″);
hashTable.put(“null1″,”null”);
hashTable.put(“null1″,”nuller”);
System.out.println(hashTable);

//No nulls as keys as well as values too
//thread safe
Map<String,String> conMap= new ConcurrentHashMap<String,String>();
conMap.put(“null1″,”null”);
conMap.put(“null”,”nuller”);
System.out.println(conMap);
}
}

 

D:\OCJP\imports>javac StaticVsNormalVsNo_Imports.java

D:\OCJP\imports>java StaticVsNormalVsNo_Imports
true
[10]
{null=nuller, null1=null, null2=null}
{null=nuller, null3=null, null1=null, null2=null}
{null=null-1, null1=nuller}
{null=nuller, null1=null}

If we put null as key in treemap or hashtable or concurrentHastable then it won’t ¬†throws any ¬†error at Compile Time.but it will throw below error at runtime

D:\OCJP\imports>javac StaticVsNormalVsNo_Imports.java

D:\OCJP\imports>java StaticVsNormalVsNo_Imports
2147483647
-2147483648
7b
true
[10]
{null=nuller, null1=null, null2=null}
{null=nuller, null3=null, null1=null, null2=null}
Exception in thread “main” java.lang.NullPointerException
at java.util.Hashtable.put(Hashtable.java:464)
at StaticVsNormalVsNo_Imports.main(StaticVsNormalVsNo_Imports.java:44)

Static vs Normal vs No imports in JAVA

Why imports are useful:

An import declaration allows a static member or a named type to be referred to by a simple name that consists of a single identifier. Without the use of an appropriate import declaration, the only way to refer to a type declared in another package, or a static member of another type, is to use a fully qualified name.

A single-type-import declaration imports a single named type, by mentioning its canonical name.

A type-import-on-demand declaration imports all the accessible types of a named type or package as needed. It is a compile time error to import a type from the unnamed package.

A single static import declaration imports all accessible static members with a given name from a type, by giving its canonical name.

A static-import-on-demand declaration imports all accessible static members of a named type as needed.

http://stackoverflow.com/questions/2903166/meaning-of-the-import-statement-in-a-java-file

What happen if we don’t declare imports in JAVA file:

The below program I am trying to call one of the method add of Arraylist i.e java.util.Arraylist i.e¬†https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#add(E)¬†.As i didn’t declared any imports in my .java file it throws error like below.

public class StaticVsNormalVsNo_Imports{
public static void main(String[] args)throws Exception{
Arraylist arraylist=new Arraylist();
System.out.println(arraylist.add());
}
}

o/p:
D:\OCJP\imports>javac StaticVsNormalVsNo_Imports.java
StaticVsNormalVsNo_Imports.java:4: error: cannot find symbol
System.out.println(arraylist.add());
^
symbol: method add()
location: variable arraylist of type Arraylist
1 error

If we try to  get rid of the error by doing some changes like creating my own Arraylist class as below.

public class StaticVsNormalVsNo_Imports{
public static void main(String[] args)throws Exception{
Arraylist arraylist=new Arraylist();
System.out.println(arraylist.add(10));
System.out.println(arraylist);
}
}

class Arraylist{
public String add(int a){
return “Mehtod message……..”+a;
}
}

o/p:
D:\OCJP\imports>javac StaticVsNormalVsNo_Imports.java

D:\OCJP\imports>java StaticVsNormalVsNo_Imports
Mehtod message……..10
Arraylist@15db9742

But it is not the functionality what i intended to do  so I move one step forward and I am giving here the fully qualified name i.e java.util.Arraylist<E>.

public class StaticVsNormalVsNo_Imports{
public static void main(String[] args)throws Exception{
java.util.ArrayList<Integer> arraylist=new java.util.ArrayList<Integer>();
System.out.println(arraylist.add(10));
System.out.println(arraylist);
}
}

class Arraylist{
public String add(int a){
return “Mehtod message……..”+a;
}
}

o/p:
D:\OCJP\imports>java StaticVsNormalVsNo_Imports
true
[10]

Now functionality is working as expected ,but what if i want’s to use 100 or 10 arraylists.In this case writing of¬†java.util.ArrayList<Integer> arraylist=new java.util.ArrayList<Integer>(); 100 times or 10 times doesn’t make sense.So here java people import statement to rescue.

import java.util.ArrayList;

public class StaticVsNormalVsNo_Imports{
public static void main(String[] args)throws Exception{
ArrayList<Integer> arraylist=new .ArrayList<Integer>();
System.out.println(arraylist.add(10));
System.out.println(arraylist);
}
}

o/p:

true [10]

What import java.util.ArrayList saying is whenever compiler sees the word ArrayList in the class it will use the java.util.ArrayList i.e in the JAVA API document there is a package called util and in that package there is class called ArrayList.

Here A package may contains many number of classes if i want to use one or more classes in a perticular package ,We are having an option wildcard character(*).If compiler sees the * symobol in import declaration it will search for all the classes in the entire package.

import java.util.*;

public class StaticVsNormalVsNo_Imports{
public static void main(String[] args)throws Exception{
ArrayList<Integer> arraylist=new ArrayList<Integer>();
System.out.println(arraylist.add(10));
System.out.println(arraylist);

//Key shouldn’t be null in treemap
//If we put two keys similar it will ovverride
//multiple Null values are allowed
TreeMap<String,String> treeMap=new TreeMap<String,String>();
treeMap.put(“null”,null);
treeMap.put(“null1”,null);
treeMap.put(“null”,”nuller”);
treeMap.put(“null2″,null);
System.out.println(treeMap);

//one key can be null
//If we put two keys similar it will ovverride
HashMap<String,String> hashMap=new HashMap<String,String>();
hashMap.put(null,null);
hashMap.put(null,”null1”);
hashMap.put(“null1”,null);
hashMap.put(“null2″,null);
hashMap.put(null,”nuller”);
System.out.println(hashMap);

//No nulls as keys as well as values too
Hashtable<String,String> hashTable=new Hashtable<String,String>();
hashTable.put(“null”,”null-1″);
hashTable.put(“null1″,”null”);
hashTable.put(“null1″,”nuller”);
System.out.println(hashTable);

}
}

o/p:

D:\OCJP\imports>java StaticVsNormalVsNo_Imports
true
[10]
{null=nuller, null1=null, null2=null}
{null=nuller, null1=null, null2=null}
{null=null-1, null1=nuller}

static imports in JAVA:

If we want to access the static members and methods of a class with out using the classname evrytime i.e Classname.XXXXX etc.Here is the example

System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.toHexString(123));

instead of putting class name every time we will go for static import.

import java.util.*;
import java.util.concurrent.*;

import static java.lang.System.out;
import static java.lang.Integer.*;
//static import won’t work
public class StaticVsNormalVsNo_Imports{
public static void main(String[] args)throws Exception{
out.println(MAX_VALUE);
out.println(MIN_VALUE);
out.println(toHexString(123));

o/p:

D:\OCJP\imports>javac StaticVsNormalVsNo_Imports.java

D:\OCJP\imports>java StaticVsNormalVsNo_Imports
2147483647
-2147483648
7b

but it is having dis advantage like

  • readability
  • both Long and Integer having MAX_VALUE etc similar properties in this case if we put wild card import(*) ¬†it leads us to Compile Time Error.