Blog Enterprise Java
A Tallan Initiative
 

Pass by reference

March 6th, 2009

In C++ and C#, developers have freedom to modify variables by directly having access to memory location.

In C++,

#include <stdio.h>

void swapnum(int &i, int &j) {
int temp = i;
i = j;
j = temp;
}

int main(void) {
int a = 10;
int b = 20;

swapnum(a, b);
printf(”A is %d and B is %d\n”, a, b);
return 0;
}

In C#,

int a = 1;

modify(ref a); //now a=2

void modify(ref int a)

{

a = 2;

}

In Java, however, there’s no such thing as pass by reference.  Even the so-called pointers (created by ‘new’ operator) are passed by copy of the reference.

Thus, if you do the following,

String a = “a”;

modify(a); //a doesn’t change, since a is being passed as a copy of the pointer a.

void modify(String a)

{

a = “b” //this a is a different pointer, thus does not affect the real ‘a’ pointer outside of the method.

}

As you can see, there’s no direct way of modifying the variable.  This has become a problem for me in many cases, since from time to time, it is necessary to have direct access to the variables.

The answer is by using a wrapper.

Here’s an example.

public class Test {
public static void main(String[] args) {
String a = “a”;
System.out.println(a); // ‘a’ gets printed
String[] array = new String[]{a}; //adds a copy of pointer ‘a’ to String array
modify(array);
System.out.println(array[0]);
}
public static void modify(String[] array) {
array[0] = “b”;  //instantiating a new object ‘b’ to the copy of variable a
}
}

This is not the most elegant way of doing it, but you get the idea. :)

One thing to remember is that String[] array contains a copy of pointer ‘a’.  When you assign, or add, items to any collections such as array, you’re passing a copy of a pointer.  So the variable ‘a’ in the example above still is pointing to the value ‘a’, whereas array[0] points to ‘b’.  In order to finalize the pointer modification, you need to assign

a = array[0].

So, the basic idea is this:  In Java, when you pass variables around, whether they are pointers or primitive values, you’re always dealing with a copy of the variables, not the variables themselves.  If you understand the basic idea of it, you can take advantage of the true “Pass by Reference”.

I hope this sort of ‘trick/hack’ can be of help to you all. :)

-Seung Kim (SK)

Cache as a cross-cutting concern

December 11th, 2008

I have been recently asked to look into the cache feature within Springmodule in Java community.

Since my current project utilizes spring and aop, I thought it would be a great idea to utilize AOP to cache data.

I actually wrote a prototype file and it was a very rudimentary yet working prototype.  However, I soon realized that Springmodules already created such feature, and it toally is a great feature to use.

Using simple AOP concept, you intercept the method call and using method parameters as HashKeyGenerator input values, the Springmodules’s cache simply get/put data from/into cache storage.  Currently, I configured it to using OSCache for its native support by Springmodule Cache Provider.

https://springmodules.dev.java.net/docs/reference/0.8/html/cache.html

I believe as the frameworks such as Spring evolves, developers can truly focus on their logic and development without worries of logging, security, cache, and any other cross-cutting concerns.  AOP is truly revolutionlizing the software development industry.  I am very glad to see it happening.

Good news for .NET community is that spring.net also supports AOP based Cache.

http://springframework.net/doc-latest/reference/html/aop-aspect-library.html

I think more and more developers should know of these great features that tremendously can help build great applications without any of the boiler-plate code.  AOP, IoC, ORM and other evolving industry standards truly the great assets that every developer should be aware of when developing/architecturing applications.

-Seung Kim (SK)

Hibernate: Merge vs SaveOrUpdate

September 18th, 2008

In my previous and current project, I have run into cases where I needed to save an object to database using hibernate, and from time to time, I run into some sort of Hibernate Session Exception.

 I did a bit of google search, and it turns out that when saving an object, I needed to make sure the object was attached to Hibernate session.

This can be very tedious issue, since that means if you need to save an object that is not attached to a session, you need to do these.

  1. Retrieve the object using the Id value found within the object passed to you.
  2. Update the property values one by one
  3. Save the object back within the same Hibernate Session

Here’s a quick summary that I found out about MERGE and SaveOrUpdate.

void saveOrUpdate(object)->object must be attached to a hibernate session (including all sub objects within the object), and once save/update is done, the object reflects the updated changes (e.g. primary key if saving a new object)

Object merge(object)-> object does not have to be attached to a hibernate session.  Once save/update is done, the object DOES NOT reflect the change.  The returned object reflects the changes, and it is attached to hibernate session.

MERGE method offers greater flexibility when it comes to saving data objects, since you need not worry about attaching object to Session.
You can create an object, set Id, and other properties on your own, and save it without worrying about the Hibernate Session exception.
If you’re using saveOrUpdate, the object saved MUST be attached to session.  I believe many people have experienced this issue.

This means you can create a copy of object from Service layer and just pass the object to your DAO.  Hibernate takes care of MERGING the data to appropriate hibernate session attached object and saves the data.

The only downside of using MERGE is that the object passed does not reflect the changed information.  So, if you need to use the updated object, you must get it from the returned object, not the parameter object.

I hope this helps. :)

-SK

Java: Too many choices?

May 15th, 2008

During my time with Tallan I have worked in both the MS camp and the Java camp. I prefer the Java side but I acknowledge that neither side is perfect. One of the main issues I see on the Java side is the huge variety choices that an architect has when starting a new project. I saw this article on DZone that seemed to summarize the situation well:

http://icedjava.blogspot.com/2007/12/dear-java-thanks-for-complexity-of.html

There is an interesting link from this blog posting. I have not read it in depth. It looked good on an initial skim. The author seems to be trying to be objective, rather than selling one framework over another. (Go past the pictures of his family… would be interesting to hear the presentation that goes along with this PDF):

Comparison of frameworks:
https://equinox.dev.java.net/framework-comparison/WebFrameworks.pdf

I am interested in hearing others point of view on this (not looking to start a flame war but a skirmish maybe interesting :) ).

Thanks,

Kim Stevens

(Originally posted: 12/20/07)

mySQL –i-am-a-dummy

March 25th, 2008

Not Java related, but since most apps deal with databases, this is something new about mySQL I learned today!

(from the mySQL manual)

For beginners, a useful startup option is --safe-updates (or --i-am-a-dummy, which has the same effect). This option was introduced in MySQL 3.23.11. It is helpful for cases when you might have issued a DELETE FROM tbl_name statement but forgotten the WHERE clause. Normally, such a statement deletes all rows from the table. With --safe-updates, you can delete rows only by specifying the key values that identify them. This helps prevent accidents.

When you use the --safe-updates option, mysql issues the following statement when it connects to the MySQL server:

SET SQL_SAFE_UPDATES=1,SQL_SELECT_LIMIT=1000, SQL_MAX_JOIN_SIZE=1000000;

See Section 12.5.3, “SET Syntax”.

The SET statement has the following effects:

 

  • You are not allowed to execute an UPDATE or DELETE statement unless you specify a key constraint in the WHERE clause or provide a LIMIT clause (or both). For example:
    UPDATE tbl_name SET not_key_column=val WHERE key_column=val;
    
    UPDATE tbl_name SET not_key_column=val LIMIT 1;
  • The server limits all large SELECT results to 1,000 rows unless the statement includes a LIMIT clause.
  • The server aborts multiple-table SELECT statements that probably need to examine more than 1,000,000 row combinations.

To specify limits different from 1,000 and 1,000,000, you can override the defaults by using the --select_limit and --max_join_size options:

EclEmma - Unit Test Coverage Analysis in Eclipse

March 24th, 2008

I was recently looking for a unit test coverage analyzer such as JCoverage for a project I’m working on. After reviewing several tools I discovered an amazing free tool called EclEmma.

I’ve used JCoverage in the past and while it’s HTML based reports are useful for project managers they are less so for developers. This tool essentially gives you the same information, but directly in Eclipse. You simply run the analyzer on either a single class or a set of classes and viola… you have your coverage report.

It even color codes each line of code within your editor window.

Use the http://update.eclemma.org/ update site to install EclEmma.

Welcome to Blog Enterprise Java!

March 24th, 2008

Welcome to Blog Enterprise Java! This blog was create and is owned by a group Java developers within Tallan. Within here we will post articles, techniques, reviews, etc… related to the field of enterprise Java.

 
� 2008 BlogEnterpriseJava.com All rights reserved. Tallan, Inc.