Found 7442 Articles for Java

Unreachable statement using the non-final variable in Java

AmitDiwan
Updated on 17-Aug-2020 09:06:35

130 Views

Following is an example, wherein we will see unreachable statement using the non-final variable −Exampleclass Demo_example {    int a = 2, b = 3;    void display_msg(){       while (a < b){          System.out.println("The first variable is greater than the second");       }       System.out.println("This is an unreachable statement");    } } public class Demo{    public static void main(String args[]){       Demo_example my_instance = new Demo_example();       my_instance.display_msg();    } }Output“The first variable is greater than the second” displayed infinitelyA class named Demo_example, that defines ... Read More

Unreachable statement using final variable in Java

AmitDiwan
Updated on 17-Aug-2020 09:04:48

199 Views

Unreachable statements are those that don’t get executed when the code is being executed. This could be so because −There is a return statement before the code.There is an infinite loop in the code.The execution of the code is terminated forcibly before it executes.Here, we will see how the unreachable statement can be used with the ‘final’ keyword −Example Live Democlass Demo_example{    final int a = 56, b = 99;    void func_sample(){       while (a < b){          System.out.println("The first value is less than the second.");       }       System.out.println("This ... Read More

Types of References in Java

AmitDiwan
Updated on 17-Aug-2020 09:02:34

837 Views

There are four different kinds of references based on the way in which the data is garbage collected.Strong referencesWeak referencesSoft referencesPhantom referencesStrong referenceIt is the default type of reference object. An object that has active strong reference can’t be garbage collected. It is possible only if the variable that is strongly referenced points to null. Let us see an example −Exampleclass Demo {    //Some functionality } public class Demo_example{    public static void main(String[] args){       Demo my_inst = new Demo();       my_inst = null;    } }Weak referenceThey are not default class of reference ... Read More

Type Erasure in Java

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

170 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

297 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

3K+ 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 30-Aug-2024 11:42:26

1K+ Views

In this program, we'll compute the sum of the series 1/1! + 2/2! + 3/3! + ... + n/n! using Java. This involves calculating factorials and summing the results of each term divided by its factorial. We'll use Java's basic arithmetic, loop control, and built-in classes to achieve this.Problem StatementWrite a Java program to calculate the sum of the series 1/1! + 2/2! + 3/3! + ... + n/n! and print the result.Steps to find the sum of a seriesFollowing are the steps to find the sum of a series −Import necessary classes from java.io and java.lang package.Initialize variables for the sum and ... Read More

Java Program for Longest Palindromic Subsequence

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

237 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

232 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

270 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

Advertisements