Category: Java Examples

python tutorials and learn python

Created with Sketch.

Java Program to Check if a String is Empty or Null

Java Program to Check if a String is Empty or Null In this program, you’ll learn to check if a string is empty or null using if-else statement and functions in Java. Example 1: Check if String is Empty or Null public class Null { public static void main(String[] args) { String str1 = null;…
Read more

Java Program to Check if An Array Contains a Given Value

Java Program to Check if An Array Contains a Given Value In this program, you’ll learn to check if an array contains a given value in Java. Example 1: Check if Int Array contains a given value public class Contains { public static void main(String[] args) { int[] num = {1, 2, 3, 4, 5};…
Read more

Java Program to Convert Character to String and Vice-Versa

Java Program to Convert Character to String and Vice-Versa In this program, you’ll learn to convert a character (char) to a string and vice-versa in Java. Example 1: Convert char to String public class CharString { public static void main(String[] args) { char ch = ‘c’; String st = Character.toString(ch); // Alternatively // st =…
Read more

Java Program to Concatenate Two Arrays

Java Program to Concatenate Two Arrays In this program, you’ll learn to concatenate two arrays in Java using arraycopy and without it.   Example 1: Concatenate Two Arrays using arraycopy import java.util.Arrays; public class Concat { public static void main(String[] args) { int[] array1 = {1, 2, 3}; int[] array2 = {4, 5, 6}; int…
Read more

Java Program to Round a Number to n Decimal Places

Java Program to Round a Number to n Decimal Places In this program, you’ll learn to round a given number to n decimal places in Java. Example 1: Round a Number using format public class Decimal { public static void main(String[] args) { double num = 1.34567; System.out.format(“%.4f”, num); } } When you run the…
Read more

Java Program to Convert String to Date

Java Program to Convert String to Date In this program, you’ll learn to convert string to date in Java using formatter. Example 1: Convert String to Date using predefined formatters import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class TimeString { public static void main(String[] args) { // Format y-M-d or yyyy-MM-d String string = “2017-07-25”; LocalDate date…
Read more

Java Program to Print an Array

Java Program to Print an Array In this program, you’ll learn different techniques to print the elements of a given array in Java. Example 1: Print an Array using For loop public class Array { public static void main(String[] args) { int[] array = {1, 2, 3, 4, 5}; for (int element: array) { System.out.println(element);…
Read more

Java Program to Remove All Whitespaces from a String

Java Program to Remove All Whitespaces from a String In this program, you’ll learn to remove all whitespaces in a given string using regular expressions in Java. Example: Program to Remove All Whitespaces public class Whitespaces { public static void main(String[] args) { String sentence = “T his is b ett er.”; System.out.println(“Original sentence: “…
Read more

Java Code To Create Pyramid and Pattern

Java Code To Create Pyramid and Pattern In this program, you’ll learn to create pyramid, half pyramid, inverted pyramid, Pascal’s triangle and Floyd’s triangle sing control statements in Java. Programs to print triangles using *, numbers and characters Example 1: Program to print half pyramid using * * * * * * * * *…
Read more

Java Program to Calculate Difference Between Two Time Periods

Java Program to Calculate Difference Between Two Time Periods In this program, you’ll learn to calculate the difference between two time periods in Java. Example: Calculate Difference Between Two Time Periods public class Time { int seconds; int minutes; int hours; public Time(int hours, int minutes, int seconds) { this.hours = hours; this.minutes = minutes;…
Read more