Category Archives: CORE JAVA
reverse string in java
import java.util.Scanner;
public class StringReverse {
public static void main(String[] args) {
String original = “”;
String reverse = “”;
Scanner in = new Scanner(System.in);
System.out.println(“Enter a string to reverse”);
original = in.nextLine();
int length = original.length();
for (int i = length – 1; i >= 0; i–)
reverse = reverse + original.charAt(i);
System.out.println(“Reverse string is: ” + reverse);
}
}
Date class in java? (Or) Date class in java.util package? (Or) Date class in java.sql package?
In java, Date class is present in the following two packages.
-
java.util package
-
java.sql package
Case study about Date class:
Example:
publicclass DateDemo1 {
publicstaticvoid main(String[] args) {
Date date = new Date ();
System.out.println (date);
}
}
Output:
F:\NAG\COREJAVA>javac DateDemo1.java
DateDemo1.java:6: cannot find symbol
symbol : class Date
location: class DateDemo1
Date date = new Date();
^
DateDemo1.java:6: cannot find symbol
symbol : class Date
location: class DateDemo1
Date date = new Date();
^
2 errors
In the above example we will get compile time error.
To resolve this problem, we have two ways.
-
By using import statement
-
By using fully Qualified name
-
By using import statement:
In this case we need the following import statement.
import java.util.Date;
Or
import java.sql.Date;
Example1:
By using java.util package
import java.util.Date;
publicclass DateDemo1 {
publicstaticvoid main(String[] args) {
Date date = new Date ();
System.out.println (date);
}
}
Output:
F:\NAG\COREJAVA>javac DateDemo1.java
F:\NAG\COREJAVA>java DateDemo1
Tue Apr 29 20:16:03 IST 2014
Example2:
By using java.sql package
import java.sql.Date;
publicclass DateDemo1 {
publicstaticvoid main(String[] args) {
Date date = new Date(0);
System.out.println (date);
}
}
F:\NAG\COREJAVA>javac DateDemo1.java
F:\NAG\COREJAVA>java DateDemo1
1970-01-01
-
By using fully Qualified name
Example1:
publicclass DateDemo1 {
publicstaticvoid main(String[] args) {
java.util.Date date = new java.util.Date();
System.out.println (date);
}
}
Output:
F:\NAG\COREJAVA>javac DateDemo1.java
F:\NAG\COREJAVA>java DateDemo1
Tue Apr 29 20:39:52 IST 2014
Example:
publicclass DateDemo1 {
publicstaticvoid main (String [] args) {
java.sql.Date date = new java.sql.Date (0);
System.out.println (date);
}
}
Output:
F:\NAG\COREJAVA>javac DateDemo1.java
F:\NAG\COREJAVA>java DateDemo1
1970-01-01
About java.util.Date:
Date class is direct child class of Object class.
Date class implements Serializable, Cloneable, Comparable interfaces.
Date class direct known subclasses are Date, Time, and Timestamp
Syntax:
public class Date
extends Object
implements Serializable, Cloneable, Comparable
Date class represents a specific instant in time, with millisecond precision.
From JDK 1.1 on words, the Date class had two additional functions.
It allowed the interpretation of dates as year, month, day, hour, minute, and second values.
It also allowed the formatting and parsing of date strings.
Unfortunately, the API for these functions was not amenable to internationalization.
As of JDK 1.1, the Calendar class should be used to convert between dates and time fields and the DateFormat class should be used to format and parse date strings.
The corresponding methods in Date are deprecated.
Date class introduced in JDK1.0.
About java.sql.Date:
Date class is child class of java.util.Date class.
java.lang.Object
java.util.Date
java.sql.Date
Date class implemented interfaces are Serializable, Cloneable, Comparable
Syntax:
public class Date
extends Date
A thin wrapper around a millisecond value that allows JDBC to identify this as an SQL DATE value
A milliseconds value represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT.
To conform to the definition of SQL DATE, the millisecond values wrapped by a java.sql.Date instance must be ‘normalized’ by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated.
Explain about StringBuffer class ?
StringBuffer class is final class.
StringBuffer class is direct chaild class of Object class.
StringBuffer class implements Serializable, CharSequence.
StringBuffer class is mutable.
StringBuffer class is thread-safe.
StringBuffer class methods are synchronized.
StringBuffer class introduced in JDK1.0
Handler mappings
1. BeanNameUrlHandlerMapping
2. SimpleUrlHandlerMapping
3. ControllerClassNameHandlerMapping
4. CommansPathMapHandlerMapping
5. DefaultAnnotationHandlerMapping
6. RequestMappingHandlerMapping
7. AbstractControllerUrlHandlerMapping
8. AbstractDetectingUrlHandlerMapping
9. AbstractHandlerMapping
10. AbstractHandlerMethodMapping
11. AbstractUrlHandlerMapping
12. ControllerBeanNameHandlerMapping
13. RequestMappingInfoHandlerMapping
Are primitive data type’s objects in Java?
Primitive data types are not objects.
ArrayList in java example using iterator
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListIterator {
public static void main(String args[]) {
ArrayList al = new ArrayList();
al.add(“Santosh”);
al.add(“Nag”);
al.add(“Rajesh”);
al.add(“Santosh”);
Iterator it = al.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
Object Class
java.lang
Class Object
java.lang.Object
*public class Object:
-
Class
Object
is the root of the class hierarchy. -
Every class has
Object
as a superclass.