There are four ways to create a Object in Java. They are
1. Using new keyword - This is the most common way to create an object in java.
Example:
MyObject object = new MyObject();
2. Using Class.forName() - If we know the name of the class and if it has a public default constructor we can create an object in this way.
Example:
MyObject object = (MyObject) Class.forName("com.ranga.MyObject").newInstance();
3. Using clone() - The clone() can be used to create a copy of an existing object.
Example:
MyObject anotherObject = new MyObject();
MyObject object = (MyObject) anotherObject.clone();
4. Using object deserialization - Object deserialization is nothing but creating an object from its serialized form.
Example:
ObjectInputStream inStream = new ObjectInputStream(anInputStream );
MyObject object = (MyObject) inStream.readObject();
Sunday, May 17, 2015
What are all the different ways to create an object in Java?
Write a Program to Implement HashTable in Java Script
<!DOCTYPE html>
<html>
<body>
<script>
function HashTable() {
var hashTableItems = {};
this.setItem = function(key, value) {
hashTableItems[key] = value;
}
this.getItem = function(key) {
return hashTableItems[key];
}
}
var hashTable = new HashTable();
hashTable.setItem('name', 'Ranga Reddy');
console.log(hashTable.getItem('name'));
var hashTable1 = new HashTable();
hashTable1.setItem('age', '27');
console.log(hashTable1.getItem('age'));
</script>
</body>
</html>
Writing our own Collection mechanism to Sort different objects in Java.
SortCollections.java
---------------------------------------
package com.ranga.collections;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class SortCollections<T> {
private List<T> collectionList;
private String sortingField = null;
private boolean isAscending = true;
public SortCollections(List<T> objectList, String sortField) {
this(objectList, sortField, true);
}
public SortCollections(List<T> collectionList, String sortField, boolean isAscending) {
super();
this.collectionList = collectionList;
this.sortingField = sortField;
this.isAscending = isAscending;
}
@SuppressWarnings("rawtypes")
public List<T> sort( final Class claz) {
final String sortingField = this.sortingField;
final Boolean isAscending = this.isAscending;
Collections.sort(this.collectionList, new Comparator<T>() {
@Override
public int compare(T object1, T object2) {
try {
Field sortField = claz.getDeclaredField(sortingField);
sortField.setAccessible(true);
Object value1 = sortField.get(object1);
Object value2 = sortField.get(object2);
if (value1 instanceof String && value2 instanceof String) { // String field
String string1 = (String) value1;
String string2 = (String) value2;
if (true == isAscending) {
return string1.compareTo(string2);
} else {
return string2.compareTo(string1);
}
} else { // Numeric field
Number number1 = (Number) value1;
Number number2 = (Number) value2;
if (true == isAscending) {
if (number1.floatValue() > number2.floatValue()) {
return 1;
} else {
return -1;
}
} else {
if (number2.floatValue() > number1.floatValue()) {
return 1;
} else {
return -1;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
});
return this.collectionList;
}
}
Employee.java
---------------------------------------package com.ranga.collections;
import java.io.Serializable;
public class Employee implements Serializable {
private int id;
private String name;
private int age;
private float salary;
public Employee() {
super();
}
public Employee(int id, String name, int age, float salary) {
super();
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age
+ ", salary=" + salary + "]";
}
}
Application.java
---------------------------------------
package com.ranga.collections;
import java.util.ArrayList;
import java.util.List;
public class Application {
public static void main(String[] args) {
List<Employee> employees = new ArrayList<Employee>();
employees.add(new Employee(1,"Ranga", 26, 25677 ));
employees.add(new Employee(2,"Raja", 50, 2577 ));
employees.add(new Employee(3,"Vasu", 20, 30677 ));
employees.add(new Employee(4,"Mani", 45, 45677 ));
employees.add(new Employee(5,"Yasu", 29, 67677 ));
employees.add(new Employee(6,"Vinod", 80, 5677 ));
System.out.println(employees);
List<Employee> employeeList = new SortCollections<Employee>(employees, "name", false).sort(Employee.class);
System.out.println(employeeList);
employeeList = new SortCollections<Employee>(employees, "salary").sort(Employee.class);
System.out.println(employeeList);
}
}
Write a SQL Program to Swap the values in a single update query.
Given a Student table, Swap all Male to Female value and vice versa with a single update query.
Id Name Sex Salary
----------------------------
1 Ranga Male 2500
2 Vasu Female 1500
3 Raja Male 5500
4 Amma Female 500
Program:
------------------------
UPDATE Gender SET sex = CASE sex WHEN 'Male' THEN 'Female' ELSE 'Male' END
Write a SQL Program to Select every Nth record.
CREATE TABLE student(id int, name varchar(30), age int, gender char(6));
INSERT INTO student VALUES
(1 ,'Ranga', 27, 'Male'),
(2 ,'Reddy', 26, 'Male'),
(3 ,'Vasu', 50, 'Female'),
(4 ,'Ranga', 27, 'Male'),
(5 ,'Raja', 10, 'Male'),
(6 ,'Pavi', 52, 'Female'),
(7 ,'Vinod', 27, 'Male'),
(8 ,'Vasu', 50, 'Female'),
(9 ,'Ranga', 27, 'Male'),
(10 ,null, 27, 'Male');
Program:
-----------------------------------------------
SELECT * FROM (
SELECT @row := @row +1 AS Rownum, name as Name FROM (SELECT @row :=0) r, student
) students
WHERE rownum %3 = 1;
Output:
-----------------------------------------------
Rownum Name
1 Ranga
4 Ranga
7 Vinod
10 (null)