Tuesday, 8 May 2012

Core Java Interview Questions -Language Fundamentals

Question:What is the out of below program? 


package com.lac.faq;

class A {
 public int i;
}

public class ParamPassDemo {
 public static void main(String[] args) {
  A a =new A();//Create an object of A
  a.i=10;
  change(a);
  a=null;
  System.out.println(a.i); //Guess result of print statement
 }
 private static void change(A a){
  a=null; //assign null
 }
 
}
Answer: The out put of the above program is 10. Most of us think the out put can be "java.lang.NullPointerException" but it is not because Objects in java are passed by copy of reference. When 'a' is passed to function 'change' the copy of reference 'a' is passed, so making the copied reference 'null' doesn't have any impact.


Question: Determine the Output of below program?

package com.lac.faq;

public class MyString extends String {
 public static void main(String[] args) {
  MyString s1="abc",s2="xyz";
  System.out.println(s1+s2);
  
 }

}
Answer: Compilation Error "MyString.java:3: cannot inherit from final java.lang.String"
Explanation: String class is a final class and it is not possible to inherit from a final class.

Table creation time MySQL

Problem: To find table creation/modified time in a given database.

Solution: MySQL maintains table level audit information in data base 'information_schema' and in table 'TABLES' 

Below Query fetches information about table 'foo'

select * from information_schema.TABLES where TABLE_NAME='foo'



Wednesday, 2 May 2012

Disabling portlet icons in Liferay

Problem:  Want to remove portlet icons like minimize, maximize, delete ..etc for all the portlets in liferay.  In some cases we dont want end user to delete or resize the deployed portlets. These operations should be done by only super admin/Omni admin/portal admin. In such case modify the portlet.vm to restrict these operations to only portal admin.

 Default icons for the tag cloud portlet.

 
 

Solution:  Edit portlet.vm and provide access to only portal admin.
 
 

   #if ($portlet_display.isShowBackIcon())
    #language("return-to-full-page")
   #else
   #if ($permissionChecker.isOmniadmin())
    $theme.iconOptions()
    $theme.iconClose()
    $theme.iconMinimize()
    $theme.iconMaximize()
   #end
   #end
  

Tuesday, 1 May 2012

Reading environment variable in Java

Following example helps to read environment variable.
 
 
package com.lac.demo;

public class ReadEnv {
 public static void main(String[] args) {
  //Reads environment variable PATH
  String value=System.getenv("PATH");
  System.out.println(value);
 }
}

Click here to download source from www.luckyacademy.com

Tuesday, 17 April 2012

Reading file in classpath

Problem: Below code shows how to read a file in classpath. The file can be in jar file or in physical file system.

Solution: We can use "java.lang.Class.getResourceAsStream(String name)" to read file
 
 
package com.lac;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Reads file in the class path
 * 
 * @author lac
 */
public class ReadFile {
 public static void main(String[] args) throws IOException {

  //Read Demo.txt from class path. The file Demo.txt is in package com.lac
  InputStream is = ReadFile.class.getResourceAsStream("/com/lac/Demo.txt");
  InputStreamReader isr = new InputStreamReader(is);
  BufferedReader br = new BufferedReader(isr);
  String fileContent = "";
  StringBuilder data = new StringBuilder();

  while ((fileContent = br.readLine()) != null) {
   data = data.append(fileContent);
  }
  is.close();

  System.out.println(data);
 }

}

Wednesday, 11 April 2012

Jersey 1.8 and Spring 3.0 Integration Problem

When I am trying to integrate Jersey 1.8 with Spring 3.0.5. The integration use to fail with below exception
 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.scheduling.annotation.internalAsyncAnnotationProcessor': Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/aop/interceptor/AsyncExecutionInterceptor

java.lang.ClassNotFoundException: org.springframework.aop.interceptor.AsyncExecutionInterceptor
 
Reason: The reason for this problem is. Jersey spring integration library "jersey-spring" is downloading older version of aop "spring-aop-2.5.6.SEC03.jar". If you exclude it. The problem should be solved.
Solution: Update the pom.xml to exclude the dependency "spring-aop"

  org.springframework
  spring-aop


Now Dependency section should look like.

   com.sun.jersey.contribs
   jersey-spring
   1.8
   
    
     org.springframework
     spring
    
    
     org.springframework
     spring-core
    
    
     org.springframework
     spring-web
    
    
     org.springframework
     spring-beans
    
    
     org.springframework
     spring-context
    
    
     org.springframework
     spring-aop
     
   
  

Thursday, 5 April 2012

Generating Random Bytes using Java

 Below example explains to generate Random bytes.  We can use "java.util.Random" or "java.security.SecureRandom" to generate Random Bytes.

package demo;

import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Random;

public class RandomByte {

 public static void main(String[] args) {
  System.out.println(Arrays.toString(getRandomByteArray(256)));
  
  //SecureRandom is a  bit costlier object. 
  SecureRandom random= new SecureRandom();
  System.out.println(Arrays.toString(random.generateSeed(256)));
  
 }
 public static byte[] getRandomByteArray(int size){
  byte[] result= new byte[size];
  Random random= new Random();
  random.nextBytes(result);
  return result;
 }
}