I actually enjoyed quite well reading this small little book.
Below are few key points i wanted to share from this book.
Please note that we may be aware of many which author wanted to share with us but i thought to address them also just for sake of any readers who have started learning java.
1) Bytecode is Java's platform-independent representation of a program's instructions
2) We may retrieve Environment Variables using
String envPath = System.getenv("PATH");
But please note that Some Java platforms, most notably the Macintosh, do not even have the concept of environment variables; thus in these environments, your code would not behave as expected.
3) Please note that we can always get and set properties externally.
Example:
class SystemProperties {
public static void main(String[] args) {
Properties props = System.getProperties();
Enumeration propertyNames = props.propertyNames();
String key = "";
while (propertyNames.hasMoreElements()) {
key = (String) propertyNames.nextElement();
System.out.println(key + "=" + props.getProperty(key).equalsIgnoreCase(" "));
}
}
}
4) When user wants to print Date/Time in given format we may use SimpleDateFormat class.
Example:
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateDemo {
public static void main(String[] args) {
Date todaysDate = new java.util.Date();
SimpleDateFormat formatter =new SimpleDateFormat("E");
String formattedDate = formatter.format(todaysDate);
System.out.println("Today's Date and Time is: " + formattedDate);
}
}
5) Below program tries to replace matched text when using regular expressions.
Example:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ReplacePatternDemo {
public static void main(String[] args) {
Pattern p = Pattern.compile("My");
Matcher m = p.matcher("My dad and My mom");
StringBuffer sb = new StringBuffer();
boolean found = m.find();
while(found) {
m.appendReplacement(sb, "Our");
found = m.find();
}
m.appendTail(sb);
System.out.println(sb);
}
}
Prints Our dad and Our mom
6) Below example tries to find All Occurrences of a Pattern
String pattern = "\\st(\\w)*o(\\w)*";
Pattern regPat = Pattern.compile(pattern);
String text =
"The words are town tom ton toon house.";
Matcher matcher = regPat.matcher(text);
while (matcher.find()) {
String matchedText = matcher.group();
System.out.println("match - " + matchedText);
}
Prints:
town
tom
ton
toon
This regular expression would not match the first word of the string even if it started with a t and contained an o. This is because the first piece of the regular expression matches on a whitespace character, and typically the string will not start with a whitespace character.
7) If you want to convert a floating point number to an integer, you have to be careful. If you simply cast the floating point number to an int or long, Java will convert it to an int or long by just truncating the decimal portion. So even if you had a value such as 20.99999, you'd end up with 20 after casting it to an int or long value. The proper way to perform floating point number to integer conversion is to use the Math.round() method.
Example:
// rounding a double value
long longResult = Math.round(doubleValue);
// rounding a float value
int intResult = Math.round(floatValue);
8) If we wanted to Calculate a Logarithm use Math.log() method
9) With a thread pool, rather than creating a new thread for each incoming request, a pool of threads is created at application start time. The thread pool has a fixed number of threads that execute tasks. Using a thread pool will prevent the application from creating an excessive number of threads which could impede performance. A very good article describing thread pooling is available from http://www.informit.com/articles/article.asp?p=30483&seqNum=3&rl=1
Hope this helps if any.
No comments:
Post a Comment