Showing posts with label corejava. Show all posts
Showing posts with label corejava. Show all posts

December 23, 2015

Generating Dynamic Password or Session key or any other values for based on the requirement

Please choose below values are including pattern based on the  your requirement:

  •  Do you want to password only allow numbers, please isNumeric should be true and all other attributes false
  • Do you want to password only allow alpha numbers, please isAlphanum should be true and all other attributes false
  • Do you want to password only allow alpha letters, please isAlpha should be true and all other attributes false
  •  Do you want to password only allow special characters, please "allowSpecialCharacters" should be true
  • Do you want to create specific length password, please "randomStringLength" value should be change.


Example: 
// TODO: Auto-generated Javadoc
/**
 * The Class RandomStringGenerator.
 */
public class RandomStringGenerator {

/** The random string length. */
private static int randomStringLength = 8;

/** The allow special characters. */
private static boolean allowSpecialCharacters = true;

/** The special characters. */
private static String specialCharacters = "!@$%*-_+:";

/** The allow duplicates. */
private static boolean allowDuplicates = false;

/** The is alphanum. */
private static boolean isAlphanum = true;

/** The is numeric. */
private static boolean isNumeric = false;

/** The is alpha. */
private static boolean isAlpha = false;

/** The Constant alphabet. */
private static final String alphabet = "abcdefghijklmnopqrstuvwxyz";

/** The mix case. */
private static boolean mixCase = true;

/** The Constant capAlpha. */
private static final String capAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

/** The Constant num. */
private static final String num = "0123456789";

/**
* Gets the random string.
*
* @return the random string
*/
public static String getRandomString() {

String returnVal = "";
int specialCharactersCount = 0;
int maxspecialCharacters = randomStringLength / 4;

try {
StringBuffer values = buildList();
for (int inx = 0; inx < randomStringLength; inx++) {
int selChar = (int) (Math.random() * (values.length() - 1));
if (allowSpecialCharacters) {
if (specialCharacters.indexOf("" + values.charAt(selChar)) > -1) {
specialCharactersCount++;
if (specialCharactersCount > maxspecialCharacters) {
while (specialCharacters.indexOf("" + values.charAt(selChar)) != -1) {
selChar = (int) (Math.random() * (values.length() - 1));
}
}
}
}
returnVal += values.charAt(selChar);
if (!allowDuplicates) {
values.deleteCharAt(selChar);
}
}
} catch (Exception e) {
returnVal = "Error While Processing Values";
}
return returnVal;
}

/**
* Builds the list.
*
* @return the string buffer
*/
private static StringBuffer buildList() {

StringBuffer list = new StringBuffer(0);
if (isNumeric || isAlphanum) {
list.append(num);
}
if (isAlpha || isAlphanum) {
list.append(alphabet);
if (mixCase) {
list.append(capAlpha);
}
}
if (allowSpecialCharacters) {
list.append(specialCharacters);
}
int currLen = list.length();
String returnVal = "";
for (int inx = 0; inx < currLen; inx++) {
int selChar = (int) (Math.random() * (list.length() - 1));
returnVal += list.charAt(selChar);
list.deleteCharAt(selChar);
}
list = new StringBuffer(returnVal);
return list;
}

public static void main(String[] args) {

//do you want to password only allow numbers, please isNumeric should be true and all other attributes false
//do you want to password only allow alpha numbers, please isAlphanum should be true and all other attributes false
//do you want to password only allow alpha letters, please isAlpha should be true and all other attributes false
//do you want to password only allow special characters, please "allowSpecialCharacters" should be true
System.out.println("Ramdom Password: " + getRandomString());
}

}
Read more ...

June 30, 2014

User defined or customized unchecked and checked exceptions in java

"An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions"


When do you require custom checked exception & How

Java platform provides a lot of exception classes you can use or you can write one of your own. You should write your own exception classes.
  • This establishes a set of exceptions that must be formally acknowledged in the program one way or another.
  • A checked exception is an exception that must be either caught and handled or listed in the throws clause of any method that may throw or propagate it.
  • When you want to handle exception in central place for each layer or for entire application, You need to write custom exception.
  • Now exception can be again type of checked and unchecked.
  • You need to write custom checked exception when you want to propagate certain exception handling responsibility to the callee method rather than handling it to the place where exception is generated.
  • For that you need to write throws clause hence callee method enforced to handle it or it again can pass on this responsibility to its callee method and so on.
  • This kind of enforcement is checked by JVM at compile time only when thrown exception is of type checked, Otherwise for unchecked exception JVM don't bother.
  • If JVM found that callee method is not handling or passing on the checked exception, It will throw compile time error for that.
  • Custom checked exception usually used ot indicate critical error signal, normally application need to terminated after that. For example, FileConfigurationException(fileConfig file not found scenario).

Create custom checked or compile-time exception

// custom checked exception class
public class FileConfigurationException extends Exception {
   FileConfigurationException(String errorMsg) {
       super(errorMsg);
   } 
}

 

When do you require custom unchecked exception & How

Java programming language does not require methods to catch or to specify unchecked exceptions (RuntimeException, Error, and their subclasses), programmers may be tempted to write code that throws only unchecked exceptions or to make all their exception subclasses inherit from RuntimeException. 
  • When you want to handle exception in central place for each layer or for entire application, you need to write custom exceptionnow exception can be again type of checked and unchecked.
  • A java exception that does not need to be caught or dealt with if the programmer so chooses.
  • Unchecked exceptions can be ignored completely in the code if desired.
  • Custom unchecked exception is usually used to indicate normal or acceptable error signal. For example: FileValidationException, FileServiceException(Invalid emalid, employee already exists etc…scenarios)
  • You need to write custom unchecked exception when you want to propagate certain exception handling responsibility to the callee method rather than handling it to the place where exception is generated.
  • For that you don't need to write throws clause as Unchecked exception are not checked by JVM at compile time.

Create custom unchecked or runtime exception

// custom unchecked or runtime exception class
public class FileServiceException extends RuntimeException {
   FileServiceException(String errorMsg) {
       super(errorMsg);
   } 
}
Read more ...

March 05, 2014

Java JDK (Java Development Kit) Releases Dates and Release Differences

A popular interview question in Java is “what is new in Java version X?”. Is that an intelligent question is debatable. I have summarized below important new features added in each major java release till now. I target to highlight important features added in respective release. Apart from below list of features, every release has enhancements and lots of bug fixes.

The JDK, described here, is for people who want to write their own Java programs. If all you want to do is run them, or browse Applets on the web, you should use the much smaller JRE (Java Runtime Environment) instead. You don’t need both. The JDK includes the JRE.
The JRE, described here, is for people who just want to run Java programs or browse Applets on the web. You must use the much larger JDK if you want to write your own Java programs. You don’t need both.

Future Versions:

Year
Version
Plan to include and enhance concepts
2015?
9.0
better support for multi-gigabyte heaps, better native code integration, and a self-tuning JVM
2017?
10.0
Primitives behave identically to objects. 64-bit arrays and Collections?

Java Version SE 8

Code named Lambda and released on Sep, 2013.
2013-09
1.8
Lambda

New features in Java SE 8
  • Closures aka λ lambda expressions.
  • Unsigned literals.
  • Annotations on Java types.
  • Date and time API (To unify Date and Calendar use 1-based months, deal with multihour DST) tight integration with JavaFX.

Java Version SE 7

Code named Dolphin and released on July 28, 2011.
2011-07-28
1.7
Dolphin

New features in Java SE 7
  • Strings in switch Statement
  • Type Inference for Generic Instance Creation
  • Multiple Exception Handling
  • Support for Dynamic Languages
  • Try with Resources
  • Java nio Package
  • Binary Literals, underscore in literals
  • Diamond Syntax
  • Automatic null Handling

Java Version SE 6

Code named Mustang and released on December 11, 2006.
2006-12-12
1.6
Mustang

New features in Java SE 6
  • Scripting Language Support
  • JDBC 4.0 API
  • Java Compiler API
  • Pluggable Annotations
  • Native PKI, Java GSS, Kerberos and LDAP support.
  • Integrated Web Services.
  • Lot more enhancements.

J2SE Version 5.0


Code named Tiger and released on September 30, 2004.

2004-09-29
1.5
Tiger


New features in J2SE 5.0
  • Generics
  • Enhanced for Loop
  • Autoboxing/Unboxing
  • Typesafe Enums
  • Varargs
  • Static Import
  • Metadata (Annotations)
  • Instrumentation

J2SE Version 1.4

Code named Merlin and released on February 6, 2002 (first release under JCP).

2002-02-13
1.4
Merlin
2002-09-16
1.4.1
Hopper (Grasshopper)
2003-06-26
1.4.2
Mantis


New features in J2SE 1.4
  • XML Processing
  • Java Print Service
  • Logging API
  • Java Web Start
  • JDBC 3.0 API
  • Assertions
  • Preferences API
  • Chained Exception
  • IPv6 Support
  • Regular Expressions
  • Image I/O API

J2SE Version 1.3

Code named Kestrel and released on May 8, 2000.
2000-05-08
1.3
Kestrel
2001-05-17
1.3.1
Ladybird

New features in J2SE 1.3
  • Java Sound
  • Jar Indexing
  • A huge list of enhancements in almost all the java area.

J2SE Version 1.2

Code named Playground and released on December 8, 1998.
1998-12-04
1.2
Playground
1999-03-30
1.2.1
(none)
1999-07-08
1.2.2
Cricket

New features in J2SE 1.2
  • Collections framework.
  • Java String memory map for constants.
  • Just In Time (JIT) compiler.
  • Jar Signer for signing Java ARchive (JAR) files.
  • Policy Tool for granting access to system resources.
  • Java Foundation Classes (JFC) which consists of Swing 1.0, Drag and Drop, and Java 2D class libraries.
  • Java Plug-in
  • Scrollable result sets, BLOB, CLOB, batch update, user-defined types in JDBC.
  • Audio support in Applets.

JDK Version 1.1

Released on February 19, 1997
1997-02-18
1.1
Sparkler
1997-09-12
1.1.4
Sparkler
1997-12-03
1.1.5
Pumpkin
1998-04-24
1.1.6
Abigail
1998-09-28
1.1.7
Brutus
1999-04-08
1.1.8
Chelsea

New features in JDK 1.1
  • JDBC (Java Database Connectivity)
  • Inner Classes
  • Java Beans
  • RMI (Remote Method Invocation)
  • Reflection (introspection only)

JDK Version 1.0

Codenamed Oak and released on January 23, 1996.

1996-01-23
1.0
Oak?


Oak was a programming language created by James Gosling in 1991, initially for Sun Micro-systems set-top box project. The language later evolved to become Java. The name Oak was used by Gosling after an oak tree that stood outside his office.
Oak was the basis for what Java 1.0 became later, but there were also some differences.
Read more ...

Highlights of Technology Changes in Java SE 7

Read more ...

My Favorite Site's List

#update below script more than 500 posts