Java has been rated number one in TIOBE popular programming developers which are used by over 10 Million developers over 15 billion devices supporting Java. It is used for creating applications for trending technologies like Big Data to household devices like Mobiles and DTH Boxes, it is used everywhere in today’s information age.
Friday, August 6, 2021
Top 20 Java Multithreading Interview Questions & Answers
Wednesday, August 4, 2021
Different Method Calls in Java
Java language is one of the most popular languages among all programming languages. There are several advantages of using the java programming language, whether for security purposes or building large distribution projects. One of the advantages of using JAVA is that Java tries to connect every concept in the language to the real world with the help of the concepts of classes etc, and every class in Java has its own methods, either inherited methods or user-defined methods that are used to define the behavior of the class. In this article, we will discuss different types of methods and ways to call them.
Types of Methods:
1. User-Defined Methods: These are the methods implemented by the user in the particular class to perform a particular operation.
2. Abstract Methods: These are the methods that do not contain the body of the method and implements inside the abstract class.
3. Predefined Methods: These are the methods that are predefined and available in the java library to perform operations, for eg the hashcode() method.
4. static Methods: These are methods that are accessible without any instance of the class. The memory management of these methods is different from ordinary methods.
Method Type 1: User-Defined Methods
User-Defined nonstatic methods can be called or accessed only with the help of an instance of the class.
Syntax:
<ClassName> object=new <ClassName>
object.<MethodName>
Example
// Java Program to Illustrate User-Defined Methods
// Importing essential input output classes
import java.io.*;
// Class 1
class GFG {
// Method 1
// Method of this class
void hello()
{
// Print statement whenever this method s called
System.out.println("This is the userDefinedMethod");
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Creating instance of the class
// inside the main() method
GFG ob = new GFG();
// Calling the method of class 1
// inside class 2
ob.hello();
}
}
Tuesday, August 3, 2021
How to Setup Jackson in Java Application?
JSON(Javascript Object Notation) is the most popular format for the exchange of data in the world of web applications. The browsers can easily parse json requests and convert them to javascript objects. The servers parse json requests, process them, and generates a new json response. JSON is self-describing and easy to understand. The process of converting java objects to json is called serialization and the process of converting json to java objects is called deserialization.
Consider a sample illustration below to get to know the file structure of json as shown below:
Illustration:
We have a Student class with attributes like id, name, address, city, hobby. Let’s understand how the corresponding json file looks as follows:
{"id":"S1122","name":"Jane","address":"XYZ Street","city":"Mumbai","hobby":"Badminton, Dancing"}
JSON data is written as a name/value pair where the name is the attribute/property name and value is the value for that attribute.
Now let us do discuss out Jackson JSON library before proceeding to set up Jackson for any java application.
◉ For java applications, it is very difficult to work with Json strings. So in java applications we need a json parser which parses Json files and converts them to java objects.
◉ Jackson is one such Java Json library used for parsing and generating Json files. It has built in Object Mapper class which parses json files and deserializes it to custom java objects. It helps in generating json from java objects.
◉ Jackson also has a Jackson Json Parser and Jackson Json Generator which parses and generates json one token at a time.
Setup:-
To use Jackson library in our application, we need to add the below dependencies in the pom.xml file of our maven project.
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.6</version>
</dependency>
On adding this dependency in pom.xml, the following jar files get added in Maven dependencies folder in eclipse:
◉ jackson-core-2.9.6.jar
◉ jackson-annotations-2.9.6.jar
◉ jackson-databind-2.9.6.jar
Note: If we are not using maven project, then we need to download and add these jar files in our classpath.
Implementation: Let’s understand how the Jackson library parses json files and generates them.
Let’s consider Employee class with attributes like name, id, deptName, salary, rating. We use Jackson library to generate a json file from the Employee object. We update one of its attributes – deptName. We serialize the employee object to a json file and then deserialize it back to an employee object with the updated value for the deptName attribute.
Example 1
// Java Program to Illustrate Setting Up of Jackson by
// parsing Jackson library json files and
// generating the same
// Importing required classes
import java.io.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an employee object with it's attributes
// set
Employee employee = getEmployee();
ObjectMapper mapper = new ObjectMapper();
// Try blcok to check for exceptions
try {
// Serailizes emp object to a file employee.json
mapper.writeValue(
new File(
"/home/suchitra/Desktop/suchitra/projects/java-concurrency-examples/jackson-parsing/src/main/resources/employee.json"),
employee);
// Deserializes emp object in json string format
String empJson
= mapper.writeValueAsString(employee);
System.out.println(
"The employee object in json format:"
+ empJson);
System.out.println(
"Updating the dept of emp object");
// Update deptName attribute of emp object
employee.setDeptName("Devops");
System.out.println(
"Deserializing updated emp json ");
// Reading from updated json and deserializes it
// to emp object
Employee updatedEmp = mapper.readValue(
mapper.writeValueAsString(employee),
Employee.class);
// Print and display the updated employee object
System.out.println("Updated emp object is "
+ updatedEmp.toString());
}
// Catch block to handle exceptions
// Catch block 1
// Handling JsonGenerationException
catch (JsonGenerationException e) {
// Display the exception along with line number
// using printStackTrace() method
e.printStackTrace();
}
// Catch block 2
// Handling JsonmappingException
catch (JsonMappingException e) {
// Display the exception along with line number
// using printStackTrace() method
e.printStackTrace();
}
// Catch block 3
// handling generic I/O exceptions
catch (IOException e) {
// Display the exception along with line number
// using printStackTrace() method
e.printStackTrace();
}
}
// Method 2
// To get the employees
private static Employee getEmployee()
{
// Creating an object of Employee class
Employee emp = new Employee();
emp.setId("E010890");
emp.setName("James");
emp.setDeptName("DBMS");
emp.setRating(5);
emp.setSalary(1000000.00);
// Returning the employee
return emp;
}
}
// Class 2
// Helper class
class Employee {
// Member variables of this class
private String id;
private String name;
private String deptName;
private double salary;
private int rating;
// Member methods of this class
public String getId() { return id; }
public void setId(String id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getDeptName() { return deptName; }
public void setDeptName(String deptName)
{
// This keyword refers to current instance
this.deptName = deptName;
}
public double getSalary() { return salary; }
public void setSalary(double salary)
{
this.salary = salary;
}
public int getRating() { return rating; }
public void setRating(int rating)
{
this.rating = rating;
}
@Override public String toString()
{
return "Employee [id=" + id + ", name=" + name
+ ", deptName=" + deptName + ", salary="
+ salary + ", rating=" + rating + "]";
}
}
Monday, August 2, 2021
Multi-Language Programming – Java Process Class, JNI and IO
Multilanguage programming, as the name suggests, involves the use of more than one programming language in a single program. There are a huge number of programming languages out there and it is a common experience that we wished we could use components from other languages as well. Well, at the first go, it may seem to be weird to use more than one language, compile codes in more than one language but at the end of the day, it is very useful.
Before diving into the details and code-based stuff, let us go back in the history of programming languages a bit – the history of Java and the history of operating systems. It was in the 1940s when assembly-level programming languages came into existence. 1951 – the Regional Assembly language came into existence. 1958 – ALGOL; 1959 – COBOL (Common Business Oriented Language) and finally BASIC (Beginners All-purpose Symbolic Instruction Code) in 1964. Finally, we got C in 1972. Python came in 1990 but it only got popular in the current days due to the advent of Data Science and Machine Learning techniques. Java came into existence in 1995. Other languages like Go, Rust, Dark, Kotlin, Swift, Scala, Scratch, etc. are very recent developments. Now, why did I say all these? It may seem irrelevant to the topic of discussion but actually, it is not.
It is agreeable that different programming languages have different capabilities. For example, C and C++ support pointers; Python is good for Data Science and AI-based fields; R is good for Data analytics and mathematical operations. Speaking from my personal experience, BASIC was the first language I used, in like 2011. (I used QBASIC mainly). Though QBASIC is an interpreted language, I later shifted to QB64, which is a modernized version of BASIC and it mostly compiled and generates an EXE file. Apart from these, there are times we have also used machine-executable scripts for achieving certain goals. For example, a bash script to toggle your computer Bluetooth, a bat file to communicate with a connected peripheral, and many more.
Now, there might be times when you are like, “Gosh! I wish this feature of language X was there in language Y too.” Many a time we all have faced it. But now, with the help of Process class, JNI, and IO in Java, we can access the features of any programming language in Java, provided the same is already installed on the machine, along with the dependencies. For example, we can use the Python OpenCV or the Python text to speech (PyTTSx3) libraries from java.
Implementation:
A short program to run a Text to speech engine from Java. It consists of two files namely say be it tt.py of python and GFG.java of java.
Fie 1: tts.py
# Python Demo Program
# System for reading command line arguments
import sys
# Our Text to speech module
import pyttsx3
if __name__ == "__main__":
engine = pyttsx3.init()
# Command line inputs saved in arg
for arg in sys.argv[1:]:
# Speaking the input
engine.say(arg)
engine.runAndWait()
“The ProcessBuilder.start() and Runtime.exec methods create a native process and return an instance of a subclass of Process that can be used to control the process and obtain information about it.”
Implementation:
Illustration:
Friday, July 30, 2021
Interfaces and Polymorphism in Java
Java language is one of the most popular languages among all programming languages. There are several advantages of using the java programming language, whether for security purposes or building large distribution projects. One of the advantages of using JA is that Java tries to connect every concept in the language to the real world with the help of the concepts of classes, inheritance, polymorphism, interfaces, etc. In this article, we will discuss polymorphism and interface concepts.
Polymorphism is that it has many forms that mean one specific defined form is used in many different ways. The simplest real-life example is let’s suppose we have to store the name of the person and the phone number of the person, but there are many situations when a person has two different phone numbers. We have to save the same phone number under the same name.
Let us interpret it with help . So, in java, the problem can be solved using an object-oriented concept, void insertPhone(String name, int phone). So, this method is used to save the phone number of the particular person. Similarly, we can use the same form but a different signature means different parameters to store the alternative phone number of the person’s void insertPhone(String name, int phone1, int phone2). One method has two different forms and performs different operations. This is an example of polymorphism, which is method overloading.
Types of polymorphism in Java:
1. Run time polymorphism
2. Compile-time polymorphism
Type 1: Run time polymorphism
This type of polymorphism is resolved by the java virtual machine, not by the java compiler. That’s why this type of polymorphism is called run-time polymorphism. Run time polymorphism occurs during method overriding in java.
Example
// Java Program to Illustrate Run-time polymorphism
// Importing I/O classes
import java.io.*;
// Class 1 (Parent class)
class GFG1 {
//name method
void name() {
System.out.println("This is the GFG1 class");
}
}
// Class 2 (Chile class)
// Main class extending parent class
public class GFG extends GFG1 {
// Method 1
void name() {
// Print statement
System.out.println("This is the GFG class");
}
// Method 2
// Main drive method
public static void main(String[] args) {
// Now creating 2 objects with different references and
// calling the Method 1 over the objects
// Case 1: GFG1 reference and GFG1 is the object
GFG1 ob = new GFG1();
ob.name();
// Case 2: GFG1 reference and GFG is the object
GFG1 ob1 = new GFG();
ob1.name();
}
}
Wednesday, July 28, 2021
Difference Between JSF2 and Seam3
JSF2 vs Seam3
JSF2 and Seam3 are basically two types of application framework that help to create the latest and up-to-date web 2.0 applications. These applications are created by the method of unification and integration of different technologies present, depending upon the application framework you are using. There are some basic differences between these two application frameworks JSF2 and Seam 3.
Seam3 Application Framework
Seam3 application framework is basically a platform, having a set of developing tools and the modules which makes it easier for us to create the Java EE 6 web applications through it. All the developing tools provided in this application framework are basically given by the JBoss Tools and the Seam Forge. The JBoss tools are basically designed to make the developers to write, test and deploy many of the enterprise Java based applications. Similarly, the Seam Forge tools provide the ability to enhance the project’s API and its shell.
Seam is used to create the internet applications in Java by integrating many of the useful technologies, which includes the JavaServer Faces (JSF), Enterprise Java Beans (EJB 3.0), Java Persistence (JPA), Asynchronous JavaScript and XML (AJAX), Business Process Management (BPM). These technologies are combined to give the simple, easier and much more sophisticated tooling experience provided to the developers.
The design of the seam3 has been made by keeping in view the most important aspects of providing the developer with simplicity in creating these web-based applications. It also helps the developers by giving them the opportunities to assemble the entire complex applications by using simple technologies like Plain Old Java Objects (POJOs), componentized UI widgets and some of the XML.
JSF2 Application Framework
The JSF2 is another application platform, created by using the Java Community Process (JCP), which helps to create many of the web application by reusing and assembling the developing components in a page. This JSF2 is quite similar to seam3 in many ways, as both of them are the platforms to create the web-based applications by integrating some special technologies, which are mostly different for both of these frameworks. JSF is basically establish on Model-View-Controller (MVC) design pattern, that is why most of the applications created on it are much easier and simple to handle as compared to other Servlets or Java platforms.
Difference between JSF2 and Seam3:
There is some basic difference between the two application frameworks, despite of many similarities which are:
• Seam3 can execute most of the web-based application, much faster and simpler than the JSF2. This makes the seam the more efficient application framework to work with.
• JSF2 is the simplest application platform available that is why it can run without providing much of the headache to the developer, but the Seam3 is basically a complex application framework which needs the proper setting of its Embedded JBoss tools, before working with it.
• Seam3 is much better to use with the low-level integration tests, so that one can stay away of the complexity it can cause with high level java based application for the web.
Difference between JSF2 and Seam3:
There is some basic difference between the two application frameworks, despite of many similarities which are:
• Seam3 can execute most of the web-based application, much faster and simpler than the JSF2. This makes the seam the more efficient application framework to work with.
• JSF2 is the simplest application platform available that is why it can run without providing much of the headache to the developer, but the Seam3 is basically a complex application framework which needs the proper setting of its Embedded JBoss tools, before working with it.
• Seam3 is much better to use with the low-level integration tests, so that one can stay away of the complexity it can cause with high level java based application for the web.
Source: differencebetween.com



















