Programming Articles - Page 1854 of 3366

Preconditions - Java

AmitDiwan
Updated on 14-Jul-2020 07:08:28

280 Views

Precondition to check if the list passed as parameter is empty or not. Let us see an example −Examplepublic void my_fun(List myList){    if (myList == null){       throw new IllegalArgumentException("List is null");    }    if (myList.isEmpty()){       throw new IllegalArgumentException("List is empty");    }    my_fun(myList); }A void function named ‘my_fun’ is defined that takes a list of objects as its parameters. If the list is null, it prints the relevant message. If the list has no elements in it, a specific message is displayed. The function is called by passing the list as ... Read More

Java program to print matrix in Z form

AmitDiwan
Updated on 29-Oct-2024 18:51:04

733 Views

In this article, we will learn to print a matrix in a "Z" pattern in Java. It starts from the top-left corner, moves horizontally across the top row, then diagonally down through the center, and finally across the bottom row. This pattern creates a "Z" shape with selected elements from the matrix. Problem Statement Write a program in Java to print a matrix in Z form. Input my_arr[][] = { { 34, 67, 89, 0}, { 0, 1, 0, 1 }, { 56, 99, 102, 21 }, {78, 61, 40, 99}} Output The matrix is34 67 89 0 0 99 ... Read More

Java program to print distinct permutations of a string

AmitDiwan
Updated on 19-Sep-2024 21:57:13

937 Views

In this article, we will learn how to generate and print all distinct permutations of a given string using Java. Permutations are rearrangements of characters in a string, and this program will ensure that no duplicate permutations are printed. The method works by checking each rearrangement and avoiding any repeats. Problem Statement Write a program in Java to print distinct permutations of a given string − Input my_str = "mnqm" Output The distinct permutations of the string are[mnqm, nmqm, nqmm, mqnm, qmnm, qnmm, mqmn, qmmn, mnmq, nmmq, mmnq, mmqn] Steps to print distinct permutations of a string Following are the ... Read More

Killing threads in Java

AmitDiwan
Updated on 14-Jul-2020 07:00:12

377 Views

Example Live Demopublic class Main{    static volatile boolean exit = false;    public static void main(String[] args){       System.out.println("Starting the main thread");       new Thread(){          public void run(){             System.out.println("Starting the inner thread");             while (!exit){             }             System.out.println("Exiting the inner thread");          }       }.start();       try{          Thread.sleep(100);       }       catch (InterruptedException e){   ... Read More

Joiner class Guava Java

AmitDiwan
Updated on 14-Jul-2020 06:58:05

122 Views

Joiner provides various methods to handle joining operations on string, objects, etc. Let us see an example −Exampleimport com.google.common.base.Joiner; import java.util.*; public class Demo{    public static void main(String[] args){       String[] my_arr = { "hel", null, "lo", "wo", "r", null, "ld" };       System.out.println("The original array is : "+ Arrays.toString(my_arr));       String my_result = Joiner.on('+').skipNulls().join(my_arr);       System.out.println("The joined string is : " + my_result);    } }OutputThe original array is [hel, null, lo, wo, r, null, ld] The joined string is hel+lo+wo+r+ldA class named Demo contains the main function, which defines ... Read More

Print Single and Multiple variables in Java

AmitDiwan
Updated on 14-Jul-2020 06:57:00

3K+ Views

To print single and multiple variables in Java, the code is as follows −Example Live Demopublic class Demo {    public static void main(String args[]){       String name_1 = "Hello";       String name_2 = "World";       System.out.println("Printing single variable");       System.out.printf("%s", name_1);       System.out.println("Printing multiple variables");       System.out.printf("First Name: %sLast Name: %s",name_1, name_2);    } }OutputPrinting single variable Hello Printing multiple variables First Name: Hello Last Name: WorldA class named Demo contains the main function, which defines two strings. These strings are displayed using the ‘println’ function and using the ‘printf’ function.

LinkedHashMap and LinkedHashSet in Java

Alshifa Hasnain
Updated on 21-Feb-2025 17:31:59

681 Views

In this article, we will learn the LinkedHashMap and LinkedHashSet in Java. LinkedHashMap Hash table and linked list implementation of the Map interface, with predictable iteration order. Example Let us see an example − import java.util.*; public class Demo { public static void main(String args[]){ LinkedHashMap my_set; my_set = new LinkedHashMap(); my_set.put(67, "Joe"); my_set.put(90, "Dev"); my_set.put(null, "Nate"); my_set.put(68, "Sara"); ... Read More

Essential Java Tips and Tricks for Programmers

Deepti S
Updated on 29-Aug-2023 15:34:08

353 Views

Java is a powerful and versatile programming language. It is used in various applications. It's known for its reliability, portability, and safety, which makes it a famous desire for developers. Java is likewise exceptionally easy to analyze, which makes it a terrific desire for novices. However, it's critical to remember that simplicity might operate as an obstacle. If you're not mindful, you could be caught by Java's accessibility and neglect to explore the unique opportunities the language offers. Let's have a look at some tips to assist you develop as a Java developer and improve your language proficiency. Tip 1: ... Read More

Java program to convert floating to binary

AmitDiwan
Updated on 14-Jul-2020 06:52:56

1K+ Views

To convert floating to binary, the Java code is as follows −Example Live Demoimport java.io.*; public class Demo {    static void decimal_to_bin(int n){       int[] bin_num = new int[50];       int i = 0;       while (n > 0){          bin_num[i] = n % 2;          n = n / 2;          i++;       }       for (int j = i - 1; j >= 0; j--)       System.out.print(bin_num[j]);    }    public static void main (String[] args){   ... Read More

Widening Primitive Conversion in Java

AmitDiwan
Updated on 14-Jul-2020 06:51:24

279 Views

Following is an example showing widening primitive conversion −Example Live Demopublic class Demo {    public static void main(String[] args) {       System.out.print("H" + "E");       System.out.print('L');       System.out.print('L');       System.out.print('O');    } }OutputHELLOA class named Demo contains the main function. Here, the ‘print’ function is used to print specific characters in double quotes and then in single quotes. When the process of widening primitive conversion happens, the presence of ‘+’ operator is a must. This ‘+’ operator expects integer on both the left hand and right hand sides.

Advertisements