Skip to main content

Posts

Showing posts with the label Core Java

Jackson – Convert JSON array string to List

1.JSON array string

[{"name":"knowledgefactory1", "salaray":1000}, {"name":"knowledgefactory2", "salaray":1200}]

2. Create an object to map the above JSON fields.
package com.knf;

public class Employee {

String name;
Double salaray;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Double getSalaray() {
return salaray;
}

public void setSalaray(Double salaray) {
this.salaray = salaray;
}

@Override
public String toString() {
return "Employee [name=" + name + ", salaray=" + salaray + "]";
}

} 3.Convert the JSON array string to a List
package com.knf;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
public class JacksonArrayExample {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
String json = "[{\"name\&q…

Java Program to find Largest Number in an Array

We can find the largest number in an array in java by sorting the array and returning the largest number. Let's see the full example to find the largest number in java array.


package com.knf;

public class LargestInArrayExample1 {

public static void main(String args[]) {
int a[] = { 2, 1, 2, 5, 6, 3, 2, 9 };
int b[] = { 144, 666, 99, 677, 433, 422, 255 };
System.out.println("Largest: " + getLargest(a));
System.out.println("Largest: " + getLargest(b));
}

public static int getLargest(int[] a) {
int temp;
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a[a.length - 1];
}
}
Output:

Largest: 9
Largest: 677

Example 2 :Using Arrays
Let's see another example to get largest element in java array using Arrays.

package com.knf;

import java.util.Arrays;

public class LargestInArrayExample2 {

public static void main(String args[]) {
int a[] = { 2, 1, 2, 5, 6, 3, 2, 9 };
int b[] = { 144, 666, …

Java - Generics

In any nontrivial software project, bugs are simply a fact of life. Careful planning, programming, and testing can help reduce their pervasiveness, but somehow, somewhere, they'll always find a way to creep into your code. This becomes especially apparent as new features are introduced and your code base grows in size and complexity.

Fortunately, some bugs are easier to detect than others. Compile-time bugs, for example, can be detected early on; you can use the compiler's error messages to figure out what the problem is and fix it, right then and there. Runtime bugs, however, can be much more problematic; they don't always surface immediately, and when they do, it may be at a point in the program that is far removed from the actual cause of the problem.

Generics add stability to your code by making more of your bugs detectable at compile time.

Generic Methods
You can write a single generic method declaration that can be called with arguments of different types. Based on th…

Java-Encapsulation

Encapsulation (data Hiding) simply means binding object state(fields) and behaviour(methods) together. If you are creating class, you are doing encapsulation. In this guide we will see how to do encapsulation in java program The whole idea behind encapsulation is to hide the implementation details from users. If a data member is private it means it can only be accessed within the same class. No outside class can access private data member (variable) of other class.
Simple Example of Encapsulation in Java
How to implement encapsulation in java: 1) Make the instance variables private so that they cannot be accessed directly from outside the class. You can only set and get values of these variables through the methods of the class. 2) Have getter and setter methods in the class to set and get the values of the fields.

User Pojo Class

package com.ahold.scanit.datamigration;
//A User class which is a fully encapsulated class.   //It has a private data member and getter and setter methods.   public c…

Java-'this' keyword in java with example

this is a keyword in Java. It can be used inside the method or constructor of a class. It(this) works as a reference to the current object, whose method or constructor is being invoked. This keyword can be used to refer to any member of the current object from within an instance method or a constructor.
Following are the ways to use ‘this’ keyword in java :
1. Using ‘this’ keyword to refer current class instance variables 2. Using this() to invoke current class constructor 3. Using ‘this’ keyword to return the current class instance 4. Using ‘this’ keyword as method parameter 5. Using ‘this’ keyword to invoke current class method 6. Using ‘this’ keyword as an argument in the constructor call
Using ‘this’ keyword to refer current class instance variables
//Java code for using 'this' keyword to  //refer current class instance variables  public class KowledgeFactoryDemo {
int a; int b;
// Parameterized constructor KowledgeFactoryDemo(int a, int b) { this.a = a; this.b = b; }
void display() { // Disp…

Java- 'super' keyword in java with example

The super keyword in Java is a reference variable which is used to refer immediate parent class object.
Whenever you create the instance of subclass, an instance of parent class is created implicitly which is referred by super reference variable.

The use of super keyword
1) To access the data members of parent class when both parent and child class have member with same name
2) To explicitly call the no-arg and parameterized constructor of parent class
3) To access the method of parent class when child class has overridden that method.


To access the data members of parent class when both parent and child class have member with same name
We can use super keyword to access the data member or field of parent class. It is used if parent class and child class have same fields.


class Animal {
String action = "Animal sleeping";
}

public class Lion extends Animal {

String action = "Lion roaring";

public void action() {

System.out.println(action);
System.out.println(super.action);
}

public …

Java -Builder Design Pattern with example

The builder pattern, as name implies, is an alternative way to construct complex objects. This should be used only when you want to build different immutable objects using same object building process. Builder pattern is used to create instance of very complex object having telescoping constructor in easiest way. Method Chaining: In java, Method Chaining is used to invoke multiple methods on the same object which occurs as a single statement. Method-chaining is implemented by a series of methods that return the this reference for a class instance. Builder pattern was introduced to solve some of the problems with Factory and Abstract Factory design patterns when the Object contains a lot of attributes. Advantages It provides clear separation between the construction and representation of an object.It provides better control over construction process.It supports to change the internal representation of objects.Implementation Constructors in Java are used to create object and can take paramete…