Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Articles - Page 132 of 745
210 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
862 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
179 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
305 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
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
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
253 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
238 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
279 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
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