Java SE Guide,Java Tutorial,amit,amit manjhi,java, spring, sql,plsql,pl sql,webservices,oracle,java tutorial, interview questions,interview coding,java2080,java 20:80,manjhi,2080,amit manjhi 2080,amit manjhi 2080 blogger, amitin2019, manjhi blogger,java interview questions, java blog,amit manjhi software engineer,amit manjhi,
Sunday, June 15, 2025
Java 8 , ways to print List
package com.test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class PrintingList {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("System");
list.add("org");
System.out.println("--------using toString() to print list----------");
System.out.println(list);
System.out.println("--------using iterator to print list----------");
Iterator<String> i = list.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
System.out.println("--------using For loop to print list----------");
for(int j = 0 ;j<list.size();j++)
{
System.out.println(list.get(j));
}
System.out.println("--------using Enhanced For loop to print list----------");
for(String item : list)
{
System.out.println(item);
}
System.out.println("--------using foreach() with lamda to print list----------");
list.forEach(x->System.out.println(x));
// Here we are using forEach() or List. it takes Consumer functional Interface as paramer. Consumer Funtional Interface has accept() abstract method. So we are basically implementing accept() in forEach() using lamda expression.
}
}
--------using toString() to print list----------
[System, org]
--------using iterator to print list----------
System
org
--------using For loop to print list----------
System
org
--------using Enhanced For loop to print list----------
System
org
--------using foreach() with lamda to print list----------
System
org
Friday, August 12, 2022
Interview Codings
Reverse String
-------------------------
package strings;
public class Reverse_String {
public static void main(String[] args)
{
String original_string1 = "aidnI";
String original_string2 = "aidnI si taerg";
char[] ch = original_string1.toCharArray();
for(int i = ch.length-1;i>=0;i--)
{
System.out.print(ch[i]);
}
}
}
Thursday, November 19, 2020
Sunday, January 12, 2020
Get All Methods of Objects Class
import java.lang.reflect.Method;
public class Show_Methods {
public static void main(String[] args) throws ClassNotFoundException
{
int count = 0;
Class c = Class.forName("java.lang.Object");
Method[] m = c.getDeclaredMethods();
for(Method m1 : m)
{
count++;
System.out.println(m1.getName());
}
System.out.println("Total No. of Methods: "+count);
}
}
----------------------------------------------------------------
OutPut:
---------
finalize
wait
wait
wait
equals
toString
hashCode
getClass
clone
notify
notifyAll
registerNatives
Total No. of Methods: 12
Different ways to create objects in Java
ways to create objects in Java
This is the most common way to create an object in java. I read somewhere that almost 99% of objects are created in this way.
MyObject object = new MyObject();
If we know the name of the class & if it has a public default constructor we can create an object in this way.
MyObject object = (MyObject) Class.forName("subin.rnd.MyObject").newInstance();
MyObject anotherObject = new MyObject(); MyObject object = anotherObject.clone();
Object deserialization is nothing but creating an object from its serialized form.
ObjectInputStream inStream = new ObjectInputStream(anInputStream );
MyObject object = (MyObject) inStream.readObject();
Monday, January 6, 2020
TryLock game
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
class MyThread4 extends Thread
{
static ReentrantLock l = new ReentrantLock();
MyThread4(String name)
{
super(name);
}
@Override
public void run()
{
do
{
try {
if(l.tryLock(5000,TimeUnit.MICROSECONDS))
{
System.out.println(Thread.currentThread().getName()+" ..Got Lock and running safe code");
Thread.sleep(30000);
l.unlock();
System.out.println(Thread.currentThread().getName()+" ..Released the lock");
break;
}
else
{
System.out.println(Thread.currentThread().getName()+" ..Unable to get Lock and executing alternative code");
Thread.sleep(5000);
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
while(true);
}
}
public class ReentrantLock_Demo4
{
public static void main(String[] args)
{
MyThread4 mtt1 = new MyThread4("F1 Thread");
MyThread4 mtt2 = new MyThread4("F2 Thread");
mtt1.start();
mtt2.start();
}
}
ReentrantLock_Demo3 (trylock)
import java.util.concurrent.locks.ReentrantLock;
class MyyThread extends Thread
{
static ReentrantLock l = new ReentrantLock();
MyyThread(String name)
{
super(name);
}
@Override
public void run()
{
if(l.tryLock())
{
System.out.println(Thread.currentThread().getName() + " ..got lock and performing safe operation.");
try
{
Thread.sleep(2000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
l.unlock();
}
else
{
System.out.println(Thread.currentThread().getName() + " ..unable to get lock and performing alternative operation.");
}
}
}
public class ReentrantLock_Demo3
{
public static void main(String[] args)
{
MyyThread mt = new MyyThread("F1 Thread");
MyyThread mt2 = new MyyThread("F2 Thread");
mt.start();
mt2.start();
}
}
Synchronized Demo_3 (Using ReentrantLock)
import java.util.concurrent.locks.ReentrantLock;
class Displays
{
ReentrantLock l = new ReentrantLock();
public void wish(String name)
{
l.lock();
for(int i=0;i<10;i++)
{
System.out.print("GoodMorning: ");
try
{
Thread.sleep(2000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
System.out.print(name);
System.out.println();
}
l.unlock();
}
}
class MyThread6 extends Thread
{
String name;
Displays d;
MyThread6(String name, Displays d)
{
this.name = name;
this.d = d;
}
public void run()
{
d.wish(name);
}
}
public class Synchronized_Using_ReentrantLock
{
public static void main(String[] args)
{
Display d = new Display();
MyThread5 mt1 = new MyThread5("Dhoni", d);
MyThread5 mt2 = new MyThread5("Yuvraj", d);
mt1.start();
mt2.start();
}
}
----------------------------------------------------------
output:
---------
GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Yuvraj
GoodMorning: Yuvraj
GoodMorning: Yuvraj
GoodMorning: Yuvraj
GoodMorning: Yuvraj
GoodMorning: Yuvraj
GoodMorning: Yuvraj
GoodMorning: Yuvraj
GoodMorning: Yuvraj
GoodMorning: Yuvraj
Synchronized Demo_2
class Display
{
//public void wish(String name)
public synchronized void wish(String name)
{
for(int i=0;i<10;i++)
{
System.out.print("GoodMorning: ");
try
{
Thread.sleep(2000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
System.out.print(name);
System.out.println();
}
}
}
class MyThread5 extends Thread
{
String name;
Display d;
MyThread5(String name, Display d)
{
this.name = name;
this.d = d;
}
public void run()
{
d.wish(name);
}
}
public class SynchronizedDemo
{
public static void main(String[] args)
{
Display d = new Display();
MyThread5 mt1 = new MyThread5("Dhoni", d);
MyThread5 mt2 = new MyThread5("Yuvraj", d);
mt1.start();
mt2.start();
}
}
//Here we are making wish method as synchronized so at a time only one thread can access wish method, this is to handle Race Condition.
---------------------------------------------------------------------------------------
output:
---------
GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Dhoni
GoodMorning: Yuvraj
GoodMorning: Yuvraj
GoodMorning: Yuvraj
GoodMorning: Yuvraj
GoodMorning: Yuvraj
GoodMorning: Yuvraj
GoodMorning: Yuvraj
GoodMorning: Yuvraj
GoodMorning: Yuvraj
GoodMorning: Yuvraj
Synchronized Demo_1
class Display
{
public void wish(String name)
{
for(int i=0;i<10;i++)
{
System.out.print("GoodMorning: ");
try
{
Thread.sleep(2000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
System.out.print(name);
System.out.println();
}
}
}
class MyThread5 extends Thread
{
String name;
Display d;
MyThread5(String name, Display d)
{
this.name = name;
this.d = d;
}
public void run()
{
d.wish(name);
}
}
public class SynchronizedDemo
{
public static void main(String[] args)
{
Display d = new Display();
MyThread5 mt1 = new MyThread5("Dhoni", d);
MyThread5 mt2 = new MyThread5("Yuvraj", d);
mt1.start();
mt2.start();
}
}
//Here on Same object of Display 'd' two threads are accessing wish()
//so we get irregular output
----------------------------
output (here we get irrigular output)
GoodMorning: GoodMorning: DhoniYuvraj
GoodMorning:
GoodMorning: DhoniYuvraj
GoodMorning:
GoodMorning: DhoniYuvraj
GoodMorning:
GoodMorning: YuvrajDhoni
GoodMorning:
GoodMorning: Dhoni
GoodMorning: Yuvraj
GoodMorning: Dhoni
GoodMorning: Yuvraj
GoodMorning: Dhoni
GoodMorning: Yuvraj
GoodMorning: Dhoni
GoodMorning: Yuvraj
GoodMorning: Dhoni
GoodMorning: Yuvraj
GoodMorning: Dhoni
Yuvraj
Join Example
class Test1 extends Thread
{
static Thread ChildTh;
@Override
public void run()
{
/* try
{
ChildTh.join(); // waiting for the main thread to complete
}
catch (InterruptedException e)
{
e.printStackTrace();
} */
for(int i = 65;i<75;i++)
{
System.out.print((char)i+" ");
}
}
}
public class Join_Exp
{
public static void main(String[] args)
{
Test1.ChildTh= Thread.currentThread();
Test1 t = new Test1();
t.start();
try
{
t.join(); // waiting for child thread to complete
}
catch (InterruptedException e)
{
e.printStackTrace();
}
for(int i = 65;i<75;i++)
{
System.out.print(i+" ");
}
}
}
Thread Group Example
public class Adv_Thread1 {
public static void main(String[] args)
{
ThreadGroup g1 = new ThreadGroup("First Group");
System.out.println(g1.getParent().getName());
ThreadGroup g2 = new ThreadGroup(g1,"Second group ");
System.out.println(g2.getParent().getName());
ThreadGroup g3 = new ThreadGroup(g1,"Third Group");
System.out.println(g3.getParent().getName());
Thread t1 = new Thread(g1, "thread_01");
Thread t2 = new Thread(g1, "thread_02");
Thread t3 = new Thread(g1, "thread_03");
Thread t4 = new Thread(g2, "thread_04");
g1.setMaxPriority(3);
Thread t5 = new Thread(g1, "thread_05");
Thread t6 = new Thread(g1, "thread_06");
System.out.println(t1.getPriority());
System.out.println(t2.getPriority());
System.out.println(t3.getPriority());
System.out.println(t4.getPriority());
System.out.println(t5.getPriority());
System.out.println(t6.getPriorit:y());
}
}
Check System threads
public class System_Threads
{
public static void main(String[] args)
{
System.out.println(Thread.currentThread().getThreadGroup().getParent().getName());
System.out.println("*********************************************");
ThreadGroup gr = Thread.currentThread().getThreadGroup().getParent();
Thread [] arr = new Thread[gr.activeCount()];
gr.enumerate(arr);
for(Thread t1 : arr)
{
System.out.println(t1.getName()+"~~"+t1.isDaemon());
}
}
}
Sunday, December 8, 2019
EmployeeServiceConsumer
--------------
1.Get the wsdl file from the Provider.
2.Test the service through SOAP UI.
3.Generate the artifacts.
C:>wsimport -d src -keep -verbose wsdl location
output: artifacts.
1.Service class.
2.Interface name
3.request and response classes
4.endpoint url.
4.Write webservice client code.
1.create service class object
2.call the port and it returns interface
3.prepare the webservice request.
4.call the getOrder API.
5.call the getPrice API.
Step to develop client in eclipse.
1.Create java project.
2. create lib folder. copy all the jars(jaxws) in to lib folder.
3.generate artifacts using wsdl.
Goto Command prompt execute fillowing commnad
E:\WorkSpace\webservice\JaxWsConsumer>wsimport -d src -keep -verbose http://localhost:2020/JaxWsOrderServiceProvider/online?wsdl
4. Write client application code in main method based class.
Steps to write logic in client class.
1.create service class object
2.call the port and it returns interface
3.prepare the webservice request.
4.call the getOrder API.
5.call the getPrice API.
Sample example for SAOP using Apache CXF and Spring Boot.
cxf spring boot dependency
cxf-spring-boot-starter-jars
application.properties
cxf.jars.component-scan=true
server.context-path=/helloapp
Steps:
1.Create maven project
2.Create End point class(service)
3.Create configuation class.
4.Test the application
======================================================================
================EmployeeServiceConsumer=======================
======================================================================
*) Parse wsdl to generate stub (artifacts) using wsimport
package test.Client;
import java.util.List;
import com.rameshit.employee.Employee;
import com.rameshit.employee.EmployeeService;
import com.rameshit.employee.EmployeeServiceImplService;
public class EmpServiceConsumer_Test
{
public static void main(String[] args)
{
//1.create service class object
EmployeeServiceImplService employeeServiceImplService = new EmployeeServiceImplService();
//2.call the port and it returns interface
EmployeeService employeeService = employeeServiceImplService.getEmployeeServiceImplPort();
//3.prepare the webservice request. (Bean ka object bana ke access karo...)
Employee emp = new Employee();
/*emp.setCompany("Apple");
emp.setEmpid(0071);
emp.setFname("Amit Kumar");
emp.setLname("Manjhi");
emp.setLocation("Boston");
emp.setSalary(999999); */
//4.call the getOrder API.
//5.call the getPrice API.
emp = employeeService.getEmployeesInfo(505);
System.out.println("Company Name: "+emp.getCompany());
System.out.println("Employe Id: "+emp.getEmpid());
System.out.println("Employe First Name: "+emp.getFname());
System.out.println("Employe Last Name: "+emp.getLname());
System.out.println("Employe Location: "+emp.getLocation());
System.out.println("Employe Salary: "+emp.getSalary());
//---------
System.out.println("******************************************************");
List<Employee> employeeList = employeeService.getAllEmployee();
for(Employee emp_n : employeeList)
{
System.out.println("Company Name: "+emp_n.getCompany());
System.out.println("Employe Id: "+emp_n.getEmpid());
System.out.println("Employe First Name: "+emp_n.getFname());
System.out.println("Employe Last Name: "+emp_n.getLname());
System.out.println("Employe Location: "+emp_n.getLocation());
System.out.println("Employe Salary: "+emp_n.getSalary());
System.out.println();
System.out.println("**********"+emp_n.getFname()+"***************");
}
}
}
output:
Company Name: IBM
Employe Id: 555
Employe First Name: Amit
Employe Last Name: Kumar
Employe Location: Noida
Employe Salary: 98789
******************************************************
Company Name: Amazon
Employe Id: 4414
Employe First Name: Amit
Employe Last Name: Manjhi
Employe Location: Sillicon Vally
Employe Salary: 9878999
*************************Amit*****************************
Company Name: Amazon
Employe Id: 4414
Employe First Name: Amit
Employe Last Name: Manjhi
Employe Location: Sillicon Vally
Employe Salary: 9878999
*************************Amit*****************************
Company Name: Amazon
Employe Id: 4414
Employe First Name: Amit
Employe Last Name: Manjhi
Employe Location: Sillicon Vally
Employe Salary: 9878999
*************************Amit*****************************
Company Name: Amazon
Employe Id: 4414
Employe First Name: Amit
Employe Last Name: Manjhi
Employe Location: Sillicon Vally
Employe Salary: 9878999
*************************Amit*****************************
Company Name: Amazon
Employe Id: 4414
Employe First Name: Amit
Employe Last Name: Manjhi
Employe Location: Sillicon Vally
Employe Salary: 9878999
*************************Amit*****************************
=====================wsdl===============
EmployeServiceProvider
import java.util.List;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface EmployeeService
{
@WebMethod
Employee getEmployeesInfo(Integer eid);
//List<Employee> getAllEmployee();
}
package test_webPublisher;
import javax.xml.ws.Endpoint;
import com.rameshit.employee.EmployeeServiceImpl;
public class SoapPublisher {
public static void main(String[] args)
{
Endpoint.publish("http://localhost:8888/EmployeeServiceProvider/employee", new EmployeeServiceImpl());
//System.out.println("SuccessFully Published. ");
//System.out.println("RUNNING ON : http://localhost:8888/EmployeeServiceProvider/employee");
//System.out.println("Use: http://localhost:8888/EmployeeServiceProvider/employee?wsdl");
}
}
Tuesday, October 8, 2019
Rough Notes for Maven
Tomcat Plugin
Servlet API
Driver (jdbc)
=========================================================
javax servlet dependency maven
---------------------------------------
https://mvnrepository.com/artifact/javax.servlet/servlet-api/3.0-alpha-1
maven tomcat plugin
--------------------------
http://tomcat.apache.org/maven-plugin-2.2/
java 8 plugin maven
------------------------
https://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html
mysql8 dependency maven
-----------------------------------
https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.11
Sunday, October 6, 2019
Send Free SMS using Java code
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
public class SendSMS
{
public static void main(String[] args)
{
try {
// Construct data
String apiKey = "apikey=" + "2D5i+ah7Dtw-6Ggu4kukZUJrnHddMJLXjNvFGstF3A"; // only 10 credits left
String message = "&message=" + "This is your message";
String sender = "&sender=" + "TXTLCL"; //not to be changed
String numbers = "&numbers=" + "8744973456";
// Send data
HttpURLConnection conn = (HttpURLConnection) new URL("https://api.textlocal.in/send/?").openConnection();
String data = apiKey + numbers + message + sender;
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Length", Integer.toString(data.length()));
conn.getOutputStream().write(data.getBytes("UTF-8"));
final BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
final StringBuffer stringBuffer = new StringBuffer();
String line;
while ((line = rd.readLine()) != null)
{
stringBuffer.append(line);
System.out.println(">>> "+stringBuffer);
}
rd.close();
//return stringBuffer.toString();
}
catch (Exception e)
{
System.out.println("Error SMS " + e);
//return "Error " + e;
}
}
}
==================================================
---------
NOTE
---------
Website : https://control.textlocal.in/settings/apikeys/
id: amitin2015@gmail.com
P/W: S****g***19
===================================================
Thursday, September 26, 2019
-
Exception in thread "main" java.lang.IllegalStateException: Cannot get a STRING value from a NUMERIC cell at org.apache.poi.xss...
-
package multithreading; import java.util.concurrent.locks.ReentrantLock; class MyyThread extends Thread { static ReentrantLock l = n...









