In today’s technology-driven world, we all can see how frequently tech advancements are coming into the existence and subsequently how old tools & technologies are becoming obsolete and uncompetitive. But the case is not the same with every single technology as JAVA is one of those few technologies out there that is enjoying a strong position in the tech world for more than 2 decades!
Sunday, August 29, 2021
10 Most Popular Java Frameworks That You Must Try
Friday, August 27, 2021
Difference Between Assembler and Interpreter
Assembler vs Interpreter
What is an Assembler?
What is an Interpreter?
What is the difference between an Assembler and an Interpreter?
Wednesday, August 25, 2021
DuoDecimal in Java
Duodecimal represents a system which is a notation system in which a number having its base as 12 is called a duodecimal number. In java, we can use to find the conversation of duodecimal numbers into respective binary, octal, decimal, hexadecimal number, or any other base numbers. In java, we can use predefined packages or user-defined methods to perform the following conversion.
Let us portray out few sample sets of duodecimal conversation into other based numbers which are listed below as follows:
The duodecimal number of (15b)12 is converted to the binary number (11010111)2
The duodecimal number of (182)12 is converted to the octal number (362)8
The duodecimal number of (262)12 is converted to the decimal number (362)10
The duodecimal number of(288052)12 is converted to the hexadecimal number (A563E)16
The duodecimal number of (58576a2)12 is converted to the hexatrigesimal number (A563E)32
Procedure:
There are certain steps required to convert into a duodecimal number which is listed below as follows:
1. Take a hexadecimal number as user input.
2. Create a user-defined function to convert it into a decimal number.
3. Create another user-defined function that will convert a decimal number into a duodecimal number.
4. Print the resultant duodecimal number.
Example:
// Java Program Illustrating DuodecimalNumber via Conversion
// of Hexadecimal Numbers into Duodecimal Numbers
// Importing utility classes
import java.util.*;
// Main class
// Representing equivalent duodecimal No of Hexadecimal No
class Main {
// Method 1
// Returning the decimal number of the given hexadecimal
// number
public static String
convertToDec(String value, int base,
Map<Character, Integer> hexatoDec)
{
int sum = 0;
int pow = 0;
String tempData = value;
// Logic to find equivalent decimal number
for (int i = tempData.length() - 1; i >= 0; i--) {
// charAt() represents element at 'i'th index
int val = tempData.charAt(i) - '0';
if (base == 16
&& hexatoDec.containsKey(
tempData.charAt(i))) {
val = hexatoDec.get(tempData.charAt(i));
}
// Math.pow() calculates x^n
sum += (val) * (Math.pow(base, pow++));
}
return String.valueOf(sum);
}
// Method 2
// Converting decimal number into Duodecimal number and
// return it into main() method.
public static String
convertToDuoDecimal(String value, int base,
Map<Integer, Character> dectoHex,
Map<Character, Integer> hextoDec)
{
String val = value;
int newBase = base;
// Checks whether the base is decimal or not
if (newBase != 10) {
// If the base is not 10, it call the
// convertToDec() method which return the
// corresponding decimal number of the given
// number.
val = convertToDec(value, base, hextoDec);
// After converting the number, new base is
// updated Say be it 10
newBase = 10;
}
// Converting the string number into integer
// using parseInt()
int temp = Integer.parseInt(val);
int rem;
String duoDecimal = "";
// Creating duoDecimalChars[] array for defining the
// characters
char duoDecimalChars[]
= { '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'A', 'B' };
// Logic to find equivalent duodecimal number
while (temp > 0) {
rem = temp % 12;
duoDecimal = duoDecimalChars[rem] + duoDecimal;
temp = temp / 12;
}
return duoDecimal;
}
// Method 3
// Main driver method
public static void main(String[] args)
{
// Createing a variable to store hexadecimal number
String val;
// Custom input in main() for hexadecimal number
val = "3A4C2";
// Creating a hexatoDec and dectoHexa for storing
// values by creating object of Map class Delaring
// object of character and integer type
Map<Character, Integer> hexatoDec = new HashMap<>();
Map<Integer, Character> dectoHex = new HashMap<>();
// Logic to store date into hexatoDec and dectoHexa
// map
for (int i = 0; i < 6; i++) {
dectoHex.put(10 + i, (char)('A' + i));
hexatoDec.put((char)('A' + i), 10 + i);
}
// Call the convertToDuoDecimal() and printing the
// returned value of it.
System.out.println(
"Duodecimal : "
+ convertToDuoDecimal(val, 16, dectoHex,
hexatoDec));
}
}
Output
Friday, August 20, 2021
Java 8 - Functional Interfaces
Functional interfaces have a single functionality to exhibit. For example, a Comparable interface with a single method ‘compareTo’ is used for comparison purpose. Java 8 has defined a lot of functional interfaces to be used extensively in lambda expressions. Following is the list of functional interfaces defined in java.util.Function package.
Functional Interface Example
Predicate <T> interface is a functional interface with a method test(Object) to return a Boolean value. This interface signifies that an object is tested to be true or false.
Create the following Java program using any editor of your choice in, say, C:\> JAVA.
Java8Tester.java
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
public class Java8Tester {
public static void main(String args[]) {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
// Predicate<Integer> predicate = n -> true
// n is passed as parameter to test method of Predicate interface
// test method will always return true no matter what value n has.
System.out.println("Print all numbers:");
//pass n as parameter
eval(list, n->true);
// Predicate<Integer> predicate1 = n -> n%2 == 0
// n is passed as parameter to test method of Predicate interface
// test method will return true if n%2 comes to be zero
System.out.println("Print even numbers:");
eval(list, n-> n%2 == 0 );
// Predicate<Integer> predicate2 = n -> n > 3
// n is passed as parameter to test method of Predicate interface
// test method will return true if n is greater than 3.
System.out.println("Print numbers greater than 3:");
eval(list, n-> n > 3 );
}
public static void eval(List<Integer> list, Predicate<Integer> predicate) {
for(Integer n: list) {
if(predicate.test(n)) {
System.out.println(n + " ");
}
}
}
}
Here we've passed Predicate interface, which takes a single input and returns Boolean.
Verify the Result
Compile the class using javac compiler as follows −
C:\JAVA>javac Java8Tester.java
Now run the Java8Tester as follows −
C:\JAVA>java Java8Tester
It should produce the following output −
Print all numbers:
1
2
3
4
5
6
7
8
9
Print even numbers:
2
4
6
8
Print numbers greater than 3:
4
5
6
7
8
9
Source: tutorialspoint.com
Wednesday, August 18, 2021
Multithreading in Java
Multithreading is a Java feature that allows concurrent execution of two or more parts of a program for maximum utilization of CPU. Each part of such program is called a thread. So, threads are light-weight processes within a process.
Threads can be created by using two mechanisms :
1. Extending the Thread class
2. Implementing the Runnable Interface
Thread creation by extending the Thread class
We create a class that extends the java.lang.Thread class. This class overrides the run() method available in the Thread class. A thread begins its life inside run() method. We create an object of our new class and call start() method to start the execution of a thread. Start() invokes the run() method on the Thread object.
// Java code for thread creation by extending
// the Thread class
class MultithreadingDemo extends Thread {
public void run()
{
try {
// Displaying the thread that is running
System.out.println(
"Thread " + Thread.currentThread().getId()
+ " is running");
}
catch (Exception e) {
// Throwing an exception
System.out.println("Exception is caught");
}
}
}
// Main Class
public class Multithread {
public static void main(String[] args)
{
int n = 8; // Number of threads
for (int i = 0; i < n; i++) {
MultithreadingDemo object
= new MultithreadingDemo();
object.start();
}
}
}
Thread creation by implementing the Runnable Interface
Thread Class vs Runnable Interface
Monday, August 16, 2021
Abstract Syntax Tree (AST) in Java
Abstract Syntax Tree is a kind of tree representation of the abstract syntactic structure of source code written in a programming language. Each node of the tree denotes a construct occurring in the source code.
There is numerous importance of AST with application in compilers as abstract syntax trees are data structures widely used in compilers to represent the structure of program code. An AST is usually the result of the syntax analysis phase of a compiler. It often serves as an intermediate representation of the program through several stages that the compiler requires, and has a strong impact on the final output of the compiler.
Let us do discuss the use of AST before proceeding further to the implementation part. AST’s are mainly used in compilers to check code for their accuracy. If the generated tree has errors, the compiler prints an error message. Abstract Syntax Tree (AST) is used because some constructs cannot be represented in context-free grammar, such as implicit typing. They are highly specific to programming languages, but research is underway on universal syntax trees.
Flow Chart:
id + id * id would have the following syntax tree which is as follows:
Implementation:
Friday, August 13, 2021
Four Main Object Oriented Programming Concepts of Java
Object-oriented programming generally referred to as OOPS is the backbone of java as java being a completely object-oriented language. Java organizes a program around the various objects and well-defined interfaces. There are four pillars been here in OOPS which are listed below. These concepts aim to implement real-world entities in programs.
◉ Abstraction
◉ Encapsulation
◉ Inheritance
◉ Polymorphism
Abstraction is a process of hiding implementation details and exposes only the functionality to the user. In abstraction, we deal with ideas and not events. This means the user will only know “what it does” rather than “how it does”.
There are two ways to achieve abstraction in Java
1. Abstract class (0 to 100%)
2. Interface (100%)
Real-Life Example: A driver will focus on the car functionality (Start/Stop -> Accelerate/ Break), he/she does not bather about how the Accelerate/ brake mechanism works internally. And this is how the abstraction works.
Certain key points should be remembered regarding this pillar of OOPS as follows:
◉ The class should be abstract if a class has one or many abstract methods
◉ An abstract class can have constructors, concrete methods, static method, and final method
◉ Abstract class can’t be instantiated directly with the new operator. It can be possible as shown in pre tag below:
A b = new B();
◉ The child class should override all the abstract methods of parent else the child class should be declared with abstract keyword
Pillar 2: Encapsulation
Pillar 3: Inheritance
Pillar 4: Polymorphism in java
Monday, August 9, 2021
Difference Between Implements and Extends
Implements vs Extends
Implements and Extends are two keywords found in Java programming language that provides a means of transferring added functionality to a new class. Implements keyword is used explicitly for implementing an interface, while Extends keyword is used for inheriting from a (super) class. Please note that the concepts of inheritance and interfaces are present in most of the other object oriented programming languages like C# and VB.NET, but they offer different syntax or keywords for applying those concepts. This article only focuses on Implements and Extends keywords defined in Java.
Extends
Extends keyword is used to implement the concept of inheritance in Java programming language. Inheritance essentially provides code reuse by allowing extending properties and behavior of an existing class by a newly defined class. When a new subclass (or derived class) extends a super class (or parent class) that subclass will inherit all attributes and methods of the super class. The subclass can optionally override the behavior (provide new or extended functionality to methods) inherited from the parent class. A subclass cannot extend multiple super classes in Java. Therefore, you cannot use extends for multiple inheritance. In order to have multiple inheritance, you need to use interfaces as explained below.
Implements
Implements keyword in Java programming language is used for implementing an interface by a class. An interface in Java is an abstract type that is used to specify a contract that should be implemented by classes, which implement that interface. Usually an interface will only contain method signatures and constant declarations. Any interface that implements a particular interface should implement all methods defined in the interface, or should be declared as an abstract class. In Java, the type of an object reference can be defined as an interface type. But that object must either be null or should hold an object of a class, which implements that particular interface. Using Implements keyword in Java, you can implement multiple interfaces to a single class. An Interface cannot implement another interface. However an interface can extend a class.
Difference between Implements and Extends
Although, Implements and Extends are two keywords that provide a mechanism to inherit attributes and behavior to a class in Java programming language, they are used for two different purposes. Implements keyword is used for a class to implement a certain interface, while Extends keyword is used for a subclass to extend from a super class. When a class implements an interface, that class needs to implement all the methods defined in the interface, but when a subclass extends a super class, it may or may not override the methods included in the parent class. Finally, another key difference between Implements and Extends is that, a class can implement multiple interfaces but it can only extend from one super class in Java. In general, usage of Implements (interfaces) is considered more favorable compared to the usage of Extends (inheritance), for several reasons like higher flexibility and the ability to minimize coupling. Therefore in practice, programming to an interface is preferred over extending from base classes.
Source: differencebetween.com
Friday, August 6, 2021
Top 20 Java Multithreading Interview Questions & Answers
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.