Samual Sam

Samual Sam

1,507 Articles Published

Articles by Samual Sam

Page 73 of 151

Overflow of DataTypes in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 1K+ Views

Overflow occurs when the given value is more than the maximum prescribed size of a data type. The overflow condition can result to an error or now the implementation of the programming language handles it on its own.To display overflow of datatypes, I have taken an example of float datatype. Float data type is a single-precision 32-bit IEEE 754 floating point.The range of a float datatype is −approximately ±3.40282347E+38FThe following program display overflow of datatypes in Java.Examplepublic class Demo {    public static void main(String[] args) {       System.out.println("Displaying Overflow... ");       float val1 = 3.3976835E38f; ...

Read More

Check if a String is whitespace, empty ("") or null in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 2K+ Views

Let’s say the following is our string.String myStr = "";Now, we will check whether the above string is whitespace, empty ("") or null.if(myStr != null && !myStr.isEmpty() && !myStr.trim().isEmpty()) {    System.out.println("String is not null or not empty or not whitespace"); } else {    System.out.println("String is null or empty or whitespace"); }The following is an example that checks for an empty string.Examplepublic class Demo {    public static void main(String[] args) {       String myStr = "";       if(myStr != null && !myStr.isEmpty() && !myStr.trim().isEmpty()) {          System.out.println("String is not null or ...

Read More

Display time zone with SimpleDateFormat("z") in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 3K+ Views

You can display timezone easily in Java using SimpleDateFormat(“z”).Firstly, to work with SimpleDateFormat class in Java, import the following package −import java.text.SimpleDateFormat;Now, set the format with SimpleDateFormat(“z”) to display timezone −Format f = new SimpleDateFormat(”z”);Now, get the timezone in a string −String strTimeZone = f.format(new Date());The following is an example −Exampleimport java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; public class Demo { public static void main(String[] args) throws Exception { // displaying current date and time Calendar cal = Calendar.getInstance(); ...

Read More

Java Program to format date time with Join

Samual Sam
Samual Sam
Updated on 11-Mar-2026 393 Views

To format date time with Join, set the date as a string and do not forget to add the delimeter.For delimeter “/” in the dateString.join("/","11","11","2018");For delimeter “:” in the date.String.join(":", "10","20","20");The following is an example.Examplepublic class Demo {    public static void main(String[] args) {       String d = String.join("/","11","11","2018");       System.out.print("Date: "+d);       String t = String.join(":", "10","20","20");       System.out.println("Time: "+t);    } }OutputDate: 11/11/2018 Time: 10:20:20

Read More

Java Program to subtract long integers and check for overflow

Samual Sam
Samual Sam
Updated on 11-Mar-2026 865 Views

To check for Long overflow, we need to check the Long.MAX_VALUE with the subtracted long result. Here, Long.MAX_VALUE is the maximum value of Long type in Java.Let us see an example wherein long integers are subtracted and if the result is still more than the Long.MAX_VALUE, then an exception is thrown −The following is an example showing how to check for Long overflow −Examplepublic class Demo { public static void main(String[] args) { long val1 = 70123; long val2 = 10567; ...

Read More

Java Program to convert an int to a boolean specifying the conversion values

Samual Sam
Samual Sam
Updated on 11-Mar-2026 215 Views

To convert int to boolean, let us first take the following int.int one = 1; int two = 1; int three = 0;We have nested if-else statement to display the true or false values. Here, the value “one” is the same as “two” i.e. 1; therefore, the following works −else if (one.equals(two)) {    System.out.println(true); }The above display “true” and in this way we converted int to boolean.Let us now see the complete example to learn how to convert int to Boolean.Examplepublic class Demo {    public static void main(String[] args) {       int one = 1;   ...

Read More

Count how many times the substring appears in the larger String in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 413 Views

Let’s say we have the following string.String str = "Learning never ends! Learning never stops!";In the above string, we need to find out how many times the substring “Learning” appears.For this, loop until the index is not equal to 1 and calculate.while ((index = str.indexOf(subString, index)) != -1) { subStrCount++; index = index + subString.length(); }The following is an example.Examplepublic class Demo {    public static void main(String[] args) {       String str = "Learning never ends! Learning never stops!";       System.out.println("String: "+str);       int subStrCount = 0;       String subString = ...

Read More

Java Program to display double and single quote in a string

Samual Sam
Samual Sam
Updated on 11-Mar-2026 6K+ Views

The following are our strings with single and double quote.String str1 = "This is Jack's mobile"; String str2 = ""This is it"!";Above, for single quote, we have to mention it normally like.This is Jack's mobileHowever, for double quotes, use the following and add a slash in the beginning as well as at the end.String str2 = ""This is it"!";The following is an example.Examplepublic class Demo {    public static void main(String[] args) {       String str1 = "This is Jack's mobile";       String str2 = ""This is it"!";       System.out.println("Displaying Single Quote: "+str1);   ...

Read More

Java Program to split a string with dot

Samual Sam
Samual Sam
Updated on 11-Mar-2026 684 Views

Let’s say the following is our string.String str = "Java is a programming language. James Gosling developed it.";We will now see how to split a string using the split() method. Include the delimiter as the parameter.String[] strSplit = str.split("\.");Above, we have split the string with dot as you can see under the split methods parameter.The following is an example.Examplepublic class Demo {    public static void main(String[] args) {       String str = "Java is a programming language. James Gosling developed it.";       System.out.println("String: "+str);       String[] strSplit = str.split("\.");       System.out.println("Splitting ...

Read More

Display hour in KK (00-11) format in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 275 Views

The “KK” format in Java Date is used to display hour in 00-11.. Use SimpleDateFormat("KK") to get the same format −// displaying hour in KK format SimpleDateFormat simpleformat = new SimpleDateFormat("KK"); String strHour = simpleformat.format(new Date()); System.out.println("Hour in KK format = "+strHour);Above, we have used the SimpleDateFormat class, therefore the following package is imported −import java.text.SimpleDateFormat;The following is an example −Exampleimport java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; public class Demo { public static void main(String[] args) throws Exception { // displaying current date and time ...

Read More
Showing 721–730 of 1,507 articles
« Prev 1 71 72 73 74 75 151 Next »
Advertisements