What are new features in Java 1.5?

•June 30, 2009 • Leave a Comment

1. Generics
2. For/in loop
3. Autoboxing/Unboxing
4. Typesafe Enums
5. Varargs
6. Static Import
7. Annotations (Metadata)

Here is the original post and here is the official document.

What are differences between ‘WHERE’ clause and ‘HAVING’ clause in SQL?

•June 30, 2009 • Leave a Comment

Having clause is usually used with Group By clause although it can be used without it too. Having is just an additional filter to Where clause.

Where clause applies to the individual rows whereas Having clause is used to test some condition on the group(usually aggregate methods) rather than on individual rows.

Example of HAVING and WHERE in one query:

SELECT titles.pub_id, AVG(titles.price)
FROM titles INNER JOIN publishers
ON titles.pub_id = publishers.pub_id
WHERE publishers.state = ‘CA’
GROUP BY titles.pub_id
HAVING AVG(titles.price) > 10

Thanks to S.Vinothkumar.

What are three princerples of OOP?

•June 30, 2009 • Leave a Comment

Inheritance
“Subclasses” are more specialized versions of a class, which inherit attributes and behaviors from their parent classes, and can introduce their own.

Encapsulation
Encapsulation conceals the functional details (functions - methods and data - instance variables or method variables) of a class from objects that send messages to it.

Polymorphism
Polymorphism allows the programmer to treat derived class members just like their parent class members.  In other words, multiple instances of same class share behavior but not state or memory.

Is that possible for Java to have memery leak?

•June 30, 2009 • Leave a Comment

Garbage collection in the Java™ programming language simplifies memory management and eliminates typical memory problems. However, contrary to popular belief, garbage collection can not take care of all memory problems. One such problem is of Java memory leaks, which are harder to detect because they usually result from design and implementation errors (for example, a reference to an object kept beyond its useful life).

In Java, you will never have a dangling pointer, because the GC will not reclaim a heap chunk if there are any references to it. Nor will you have a C/C++ type memory leak because GC will reclaim the heap chunk only if there is no reference to it. Then, what are Java memory leaks? If a program holds a reference to a heap chunk that is not used during the rest of its life, it is considered a memory leak because the memory could have been freed and reused. GC won’t reclaim it due to the reference being held by the program. A Java program could run out of memory due to such leaks. Java memory leaks are mostly a result of non-obvious programming errors.

Read the original post for more information.

What are differences between ArrayList and Vector?

•June 30, 2009 • Leave a Comment

From an API perspective, the two classes are very similar.

Vectors are synchronized. Any method that touches the Vector’s contents is thread safe. ArrayList, on the other hand, is unsynchronized, making them, therefore, not thread safe. With that difference in mind, using synchronization will incur a performance hit. So if you don’t need a thread-safe collection, use the ArrayList.

Internally, both the ArrayList and Vector hold onto their contents using an Array. You need to keep this fact in mind while using either in your programs. When you insert an element into an ArrayList or a Vector, the object will need to expand its internal array if it runs out of room. A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent. Depending on how you use these classes, you could end up taking a large performance hit while adding new elements. It’s always best to set the object’s initial capacity to the largest capacity that your program will need. By carefully setting the capacity, you can avoid paying the penalty needed to resize the internal array later. If you don’t know how much data you’ll have, but you do know the rate at which it grows, Vector does possess a slight advantage since you can set the increment value.

Unless you have strong reason to use a Vector, the suggestion is to use the ArrayList.

How Java handle synchronization between threads?

•June 30, 2009 • Leave a Comment

Java provides a few simple structures for synchronizing the activities of threads. They are all based on the concept of monitors, a widely used synchronization scheme developed by C.A.R. Hoare.

A monitor is essentially a lock. The lock is attached to a resource that many threads may need to access, but that should be accessed by only one thread at a time.

In Java, every object has a lock associated with it. To be more specific, every class and every instance of a class has its own lock. The synchronized keyword marks places where a thread must acquire the lock before proceeding.

Often, you want to synchronize multiple methods of the same class, so that only one of the methods modifies or examines parts of the class at a time. All static synchronized methods in a class use the same class object lock. By the same token, all instance methods in a class use the same instance object lock. In this way, Java can guarantee that only one of a set of synchronized methods is running at a time.

In addition to synchronizing entire methods, the synchronized keyword can be used in a special construct to guard arbitrary blocks of code. In this form it also takes an explicit argument that specifies the object for which it is to acquire a lock.

Look at the original post for more information.

Some Oracle Questions

•June 18, 2009 • Leave a Comment

Q: What is the difference between an Oracle instance and an Oracle database?
A: An instance is comprised of the SGA and background processes; the database is made up of the physical data files. A database can have multiple instances accessing it, as in a RAC configuration.

Q: What is the difference between DML, DDL and DCL? Give an example for each.
A: DML: Data Manipulation Language is the category of statements which manipulate the data or either grants or revokes privileges on it. i.e.  SELECT, INSERT, UPDATE, DELETE, GRANT, REVOKE…

DDL: Data Definition Language is that category which defines the structure of objects. i.e. CREATE, DROP, CHANGE, REPLACE…

DCL: Data Control Language is that category which used to control transactions. i.e. COMMIT, ROLLBACK, SAVEPOINT…

Q: You issue the following statement: TRUNCATE TABLE myTable;
What happens if you forget to issue a COMMIT command before exiting the session? Why?
A: All records in the table will be deleted. The statement is DDL and is committed on excution.

Q: What is the difference between a SYSTEM Privilege and an OBJECT Privilege?
A: A SP allows the user to perform tasks on a class of database objects; An OP allows a user ccess to specific objects, tables, synonyms, etc.

Q: What is a role?
A: A collection of system privileges or both.

Q: Table: cust_purchases; columns: name, purch_amount. Write a query retuen only the rows with the three highest $ amount.
A: SELECT * FROM (SELECT * FROM cust_purchases ORDER BY urch_amount DESC) WHERE ROWNUM < 4;

SQL query, join two set of data from the same table

•June 15, 2009 • Leave a Comment

This is an example of find pairs of samples from two data set that use the same name:

select s1.name,s2.name from simsorder o1,limssample s1,simsorder o2,limssample s2
where o1.ordernumber = s1.ordernumber
and o1.utn != o2.utn
and o2.ordernumber= s2.ordernumber
and s1.name = s2.name
and o1.utn = 2051738
and o2.utn = 2052689

How to creating executable jar files

•June 8, 2009 • Leave a Comment
  • Make sure your Java project is ready to be delivered.
  • In the directory in which the psae is located, created a file called “mainClass”. This file contains a single line specifying where the main Class is to be found in the jar file. Note that I use the package pecification. Here is the single line:

Main-Class: psae.HelloWorld

Note: make sure you type a carriage return after this line; some windows systems need it and will report a “Failed to load Main-Class manifest attribute” error.

  • Next, I create a jar file called psae.jar using the “jar” command in Java2. I use the “m” command line argument to specify the manifest file mainClass, which adds information to the jar file on where the main class will be found. Here is the jar command:

bertha:~ > jar cmf mainClass psae.jar psae

  • Just for fun, and to check what’s happened, I print the table of contents for the jar file I just created. Here’s the command and its result:

bertha:~ > jar tf psae.jar
META-INF/
META-INF/MANIFEST.MF
psae/
psae/HelloWorld.java
psae/HelloWorld.class

  • Having successfully created the jar file, I can now invoke java2 on it with the command line argument:

bertha:~ > java -jar psae.jar Philip
Hello World Philip

Build Java 1.6 projects using Maven on Mac OS X

•June 5, 2009 • Leave a Comment

A problem encountered at work today …

Hi Ying

I made changes to the POM files to compile the project using 1.6. You will get this error if you are trying to build the project outside of STS.

You need to set up JAVA_HOME in your MAC to point to 1.6.

Please follow the steps in this article to do it:

Let me know if you are still having issues.

Thanks
Swetha

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Hi Swetha,

Here is the error I got when I tried to build the whole databus project:
[ERROR]
Mojo:

org.apache.maven.plugins:maven-compiler-plugin:2.0.2:compile

FAILED for project:

org.stjude.srm:srm-broker:jar:1.0.0

Reason:

Failure executing javac, but could not parse the error:
javac: invalid target release: 1.6
Usage: javac
where possible options include:
-g Generate all debugging info
-g:none Generate no debugging info
-g:{lines,vars,source} Generate only some debugging info
-nowarn Generate no warnings
-verbose Output messages about what the compiler is doing
-deprecation Output source locations where deprecated APIs are used
-classpath Specify where to find user class files
-cp Specify where to find user class files
-sourcepath Specify where to find input source files
-bootclasspath Override location of bootstrap class files
-extdirs Override location of installed extensions
-endorseddirs Override location of endorsed standards path
-d Specify where to place generated class files
-encoding Specify character encoding used by source files
-source Provide source compatibility with specified release
-target Generate class files for specific VM version
-version Version information
-help Print a synopsis of standard options
-X Print a synopsis of nonstandard options
-J Pass directly to the runtime system

[INFO] ————————————————————————
[INFO] For more information, run with the -e flag
[INFO] ————————————————————————
[INFO] BUILD FAILED
[INFO] ————————————————————————
[INFO] Total time: 3 seconds
[INFO] Finished at: Wed Aug 05 12:23:53 CDT 2009
[INFO] Final Memory: 4M/20M

Thanks,
Ying