google.com, pub-5747754088801811, DIRECT, f08c47fec0942fa0 Skip to main content

Java Interview Question

What is JVM,JDK,JRE, JIT?


Java Development Kit(JDK)
The Java Development Kit (JDK) is a software development environment used for developing Java applications and applets. It includes the Java Runtime Environment (JRE), an interpreter/loader (java), a compiler (javac), an archiver (jar), a documentation generator (javadoc) and other tools needed in Java development.

JVM
JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides runtime environment in which java bytecode can be executed.
JVMs are available for many hardware and software platforms. JVM, JRE and JDK are platform dependent because configuration of each OS differs. But, Java is platform independent.
The JVM performs following main tasks:
Loads code
Verifies code
Executes code
Provides runtime environment

JRE
JRE is an acronym for Java Runtime Environment.It is used to provide runtime environment.It is the implementation of JVM. It physically exists. It contains set of libraries + other files that JVM uses at runtime.
Implementation of JVMs are also actively released by other companies besides Sun Micro Systems.

JIT
The JIT compiler compiles the bytecodes of that method into native machine code, compiling it "just in time" to run. When a method has been compiled, the JVM calls the compiled code of that method directly instead of interpreting it.

Flow
Program -> Compiler -> Class file -> JVM(JIT) -> Convert byte code into machine code.
 

 

 

How to sort custom class of arraylist?


We can sort custom class with help of Comparator and  Comparable interfaces.

Let see example of it.

Create project Java Interview

Create package com.java.interview

Create custom class Student add below private member and implements Comparable.

Implement compareTo method shown like below.

How to sort of set with custom class?

package com.java.interview;

public class Student implements Comparable<Student> {

    private int rollNo;
    private String name;
    private String address;
   
   
    public Student(int rollNo, String name, String address) {
        super();
        this.rollNo = rollNo;
        this.name = name;
        this.address = address;
    }
    public int getRollNo() {
        return rollNo;
    }
    public void setRollNo(int rollNo) {
        this.rollNo = rollNo;
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    @Override
    public int compareTo(Student student) {
        if(this.rollNo > student.getRollNo()){
            return 1;
        }
        else if(this.rollNo < student.getRollNo()){
            return -1;
        }
        else {
            return 0;
        }
    }
   
    @Override
    public String toString() {
        return "Student [rollNo=" + rollNo + ", name=" + name + ", address=" + address + "]";
    }
   
}

Create another class  CustomClassSort

Add main method shown like below.


package com.java.interview;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class CustomClassSort {
    public static void main(String[] args) {
        Student student1 = new Student(3, "Sachin", "Mumbai");
        Student student2 = new Student(2, "Sehwag", "Delhi");
        Student student3 = new Student(1, "Dravid", "Banglore");
        List<Student> students = new ArrayList<Student>();
        students.add(student1);
        students.add(student2);
        students.add(student3);
        Collections.sort(students);
      
        System.out.println("Sorting student base on the Roll No");
        for(Student student : students){
            System.out.println(student.toString());
        }
        Collections.sort(students, new Comparator<Student>(){
            @Override
            public int compare(Student student1, Student student2) {
                return student1.getName().compareTo(student2.getName());
            }
        });
      
        System.out.println("Sorting student base on the Name");
        for(Student student : students){
            System.out.println(student.toString());
        }
    }
}



Above class generate below out put



Sorting name base on the Roll No
Student [rollNo=1, name=Dravid, address=Banglore]
Student [rollNo=2, name=Sehwag, address=Delhi]
Student [rollNo=3, name=Sachin, address=Mumbai]
Sorting name base on the Name
Student [rollNo=1, name=Dravid, address=Banglore]
Student [rollNo=3, name=Sachin, address=Mumbai]
Student [rollNo=2, name=Sehwag, address=Delhi]



 Sort The Set 

It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time.
To get a sensible ordering, you need to use a different Set implementation such as TreeSet or ConcurrentSkipListSet.

Create one Custom class FullName like below.

package com.java.interview;

public class FullName {
    String firstName;
    String lastName;

    public FullName(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return "FullName [firstName=" + firstName + ", lastName=" + lastName + "]";
    }
   
}
 

Create class to check the tree sort. We require to provide comparator as argument in treeset.

package com.java.interview;

import java.util.*;

public class SetSort{

    public static void main(String[] args)
    {
        FullName person1 = new FullName("Stephen", "Harper");
        FullName person2 = new FullName("Jason", "Kenney");
        FullName person3 = new FullName("Peter", "MacKay");
        FullName person4 = new FullName("Rona", "Ambrose");
       
        TreeSet<FullName> names = new TreeSet<FullName>(new Comparator<FullName>(){

            @Override
            public int compare(FullName o1, FullName o2) {
              
                return o1.getFirstName().compareTo(o2.getFirstName());
            }
          
        });

        names.add(person3);
        names.add(person1);
        names.add(person4);
        names.add(person2);
     

        System.out.println(names);     
   }
}
 

Output

[FullName [firstName=Jason, lastName=Kenney], FullName [firstName=Peter, lastName=MacKay], FullName [firstName=Rona, lastName=Ambrose], FullName [firstName=Stephen, lastName=Harper]]
 











Comments

Popular posts from this blog

Disable cache content of browser access on back button after logout liferay dxp

Some time we have requirement where we do not want to allow back button after logout and show cached browser content. Liferay provide properties which with we are able to restrict to see content on back button after logout. If user click back button after logout it will show the login page. We require to provide below properties in portal-ext.properties. Restart the server once applying below properties browser.cache.signed.in.disabled=true

Liferay crud porrtlet with Jasper Reports.

I have used Meera Prince Crud operation demo for the Jasper reports which allow to export report in PDF , word, XLS format. It will look lie below image. Find Source code for the complete example on below location. LIFERAY JASPER REPORT INTEGRATION 1 I have available PhoneManger Crud Portlet which have pagination and crud functionality. 2 I create report-listing-mobile.jrxml  file with help of Ireport tool. <?xml version="1.0" encoding="UTF-8"?> <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report-listing" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin=&q