Found 2617 Articles for Java

Type Erasure in Java

AmitDiwan
Updated on 17-Aug-2020 08:59:55

103 Views

To support generic programming, as well as perform a stricter type check, Java implements type erasure.All type parameters in generic types are replaced with the bound (if unbounded) or object type. This way, the bytecode will only contain classes, methods, and interfaces.Type casts to preserve the type.Bridge methods are generated so as to preserve the polymorphism concept in extended generic types.Example Live Demoimport java.io.PrintStream; import java.util.*; public class Demo{    public Demo(){    }    public static void main(String args[]){       List my_list = new ArrayList();       my_list.add("Hi there");       String my_str;       ... Read More

Shuffle or Randomize a list in Java

AmitDiwan
Updated on 17-Aug-2020 08:56:23

196 Views

To shuffle a list in Java, the code is as follows −Example Live Demoimport java.util.*; public class Demo{    public static void main(String[] args){       ArrayList my_list = new ArrayList();       my_list.add("Hello");       my_list.add(", ");       my_list.add("this");       my_list.add("is");       my_list.add("a");       my_list.add("sample");       System.out.println("The original list is : " + my_list);       Collections.shuffle(my_list);       System.out.println(" The shuffled list is : " + my_list);    } }OutputThe original list is : [Hello, ,, this, is, a, sample] The shuffled list is ... Read More

What is the maximum possible value of an integer in Java ?

AmitDiwan
Updated on 17-Aug-2020 08:50:41

2K+ Views

The MAX_VALUE is used to find the maximum possible value for an integer in Java. Let us see an example −Example Live Demopublic class Demo{    public static void main(String[] args){       System.out.println("The type is");       System.out.println(Integer.TYPE);       System.out.println("The size is");       System.out.println(Integer.SIZE);       System.out.println("The max value of integer is");       System.out.println(Integer.MAX_VALUE);    } }OutputThe type is int The size is 32 The max value of integer is 2147483647A class named Demo uses the Integer class and gives the various characteristics of the Integer class such as type, size ... Read More

Java Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!

AmitDiwan
Updated on 17-Aug-2020 08:44:33

961 Views

Following is the Java program to find the sum of the series −1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!Example Live Demoimport java.io.*; import java.lang.*; public class Demo{    public static double pattern_sum(double val){       double residual = 0, factorial_val = 1;       for (int i = 1; i

Java Program for Longest Palindromic Subsequence

AmitDiwan
Updated on 17-Aug-2020 08:42:15

157 Views

For longest Palindromic subsequence, the Java code is as follows −Example Live Demopublic class Demo{    static String longest_seq(String str_1, String str_2){       int str_1_len = str_1.length();       int str_2_len = str_2.length();       char str_1_arr[] = str_1.toCharArray();       char str_2_arr[] = str_2.toCharArray();       int L[][] = new int[str_1_len + 1][str_2_len + 1];       for (int i = 0; i 0){          if (str_1_arr[i - 1] == str_2_arr[j - 1]){             longest_seq[my_index - 1] = str_1_arr[i - 1];       ... Read More

Find a pair with given sum in a Balanced BST in Java

Arnab Chakraborty
Updated on 23-Jul-2020 08:32:23

141 Views

ConceptWith respect of a given Balanced Binary Search Tree and a target sum, we write a function that returns true if there is a pair with sum equals to target sum, otherwise return false. In this case, expected time complexity is O(n) and only O(Logn) extra space can beimplemented. Here, any modification to Binary Search Tree is not permitted.We have to note that height of a Balanced BST is always O(Logn).ExampleMethodAccording to the Brute Force Solution, we consider each pair in BST and verify whether the sum equals to X. The time complexity of this solution will be O(n^2).Now a ... Read More

Preconditions - Java

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

194 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 14-Jul-2020 07:04:40

494 Views

To print matrix in Z form, the Java code is as follows −Example Live Demoimport java.lang.*; import java.io.*; public class Demo{    public static void z_shape(int my_arr[][], int n){       int i = 0, j, k;       for (j = 0; j < n - 1; j++){          System.out.print(my_arr[i][j] + " ");       }       k = 1;       for (i = 0; i < n - 1; i++){          for (j = 0; j < n; j++){             if ... Read More

Java Program to print distinct permutations of a string

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

582 Views

To print distinct permutations of a string, the Java program is as follows −Example Live Demoimport java.util.ArrayList; public class Demo{    static boolean is_present(String my_str, ArrayList rem){       for (String str : rem){          if (str.equals(my_str))          return true;       }       return false;    }    static ArrayList distinct_pattern(String str){       if (str.length() == 0){          ArrayList base_Val = new ArrayList();          base_Val.add("");          return base_Val;       }       char ch = str.charAt(0);       String rem_str = str.substring(1);       ArrayList prev_str = distinct_pattern(rem_str);       ArrayList rem = new ArrayList();       for (String my_str : prev_str){          for (int i = 0; i

Killing threads in Java

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

260 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

Advertisements