devxlogo

Classes

Using Calendar.getInstance()

Calendar c = Calendar.getInstance(); c.set(2017, Calendar.OCTOBER, 29);  This code behave as a Gregorian calendar, but if the returned Calendar subclass is a Islamic, Julian, Buddistic or Hebrew calendar, then the month called October or the year 2017, doesn’t exist. Calendar.getInstance() uses the current default locale to select an appropiate implementation. The utility of Calendar.getInstance() is very limited  and it should be avoided because it’s results is not properly defined. Calendar c = new GregorianCalendar (timeZone); c.set(2017, Calendar.OCTOBER, 29); 

Random Number Generation Using Java

We can use the Random class available in the java.util package to generate random numbers. The code sample below demonstrates generation of a random number with an upper limit. Listing

Clearing the Contents of a StringBuffer

StringBuffer is a class that facilitates string operations and also helps reduce memory usage. A method defined on StringBuffer named setLength(int newLength) also serves as a shortcut to clear the

Underscore Numerals in Java

Using underscores in numerals is allowed in Java. This is for better readability and representation. The example below illustrates the same value being written in two different forms and results

Understanding Optional

Optional makes it easier in cases where the result is not really needed. This example can be fine-tuned as needed to explore all of the methods supported. import java.util.Optional;public class

Using String.join to Concatenate Strings

String.join is an easy-to-use utility method in String class. public class StringJoin{ public static void main(String args[]){ //The first argument (refered as joinString) is used to join the subsequent argument(s)

A Simple String Class for Beginners

The code in String.cpp can be used to do basic operations based on character array. It can also be used to see some of the extra flavors of Object Oriented