Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Java Articles
Page 16 of 450
Count occurrences of a substring recursively in Java
Given two strings str_1 and str_2. The goal is to count the number of occurrences of substring str2 in string str1 using a recursive process.A recursive function is the one which has its own call inside it’s definition.If str1 is “I know that you know that i know” str2=”know”Count of occurences is − 3Let us understand with examples.For ExampleInputstr1 = "TPisTPareTPamTP", str2 = "TP";OutputCount of occurrences of a substring recursively are: 4ExplanationThe substring TP occurs 4 times in str1.Inputstr1 = "HiHOwAReyouHiHi" str2 = "Hi"OutputCount of occurrences of a substring recursively are: 3ExplanationThe substring Hi occurs 3 times in str1.Approach used ...
Read MoreParent and Child classes having same data member in Java
The parent class can hold reference to both the parent and child objects. If a parent class variable holds reference of the child class, and the value is present in both the classes, in general, the reference belongs to the parent class variable. This is due to the run-time polymorphism characteristic in Java.ExampleFollowing is an example −class Demo_base { int value = 1000; Demo_base() { System.out.println("This is the base class constructor"); } } class Demo_inherits extends Demo_base { int value = 10; Demo_inherits() { System.out.println("This is the inherited class ...
Read MoreQuantifiers in Java
Quantifier is a concept that allows the programmer to specify the number of occurrences of a specific type of value in the regular expression. There are different types of quantifiers, some of them include ‘?’ (Reluctant quantifier), ‘+’ (Possessive quantifier). In this post, we will see how reluctant quantifier works.ExampleFollowing is an example −import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo { public static void main(String[] args) { Pattern my_pattern = Pattern.compile("sam+?"); Matcher my_match = my_pattern.matcher("samp"); while (my_match.find()) System.out.println("The pattern has been found - " + my_match.start() + ...
Read MoreRetrieving Elements from Collection in Java- For-each loop
The ‘for-each’ loop is used to iterate over a set of elements that is stored in a data structure.Syntaxfor (element e: collection) { System.out.println(e); }ExampleFollowing is an example −public class Demo { public static void main(String[] args) { int[] my_vals = {5, 67, 89, 31, -1, 2, 0}; int sum = 0; for (int number: my_vals) { sum += number; } System.out.println("The sum is " + sum); } }OutputThe sum is 193A class named Demo contains the main ...
Read MoreRetrieving Elements from Collection in Java- Iterator
Following is an example to retrieve elements −Exampleimport java.util.*; public class Demo { public static void main(String args[]) { HashSet my_hs = new HashSet() ; my_hs.add("Joe"); my_hs.add ("Rob"); Iterator my_it = my_hs.iterator(); System.out.println("The elements are : "); while (my_it.hasNext()) System.out.println(my_it.next()); } }OutputThe elements are : Joe RobA class named Demo contains the main function, which defines a HashSet collection. Elements are added to this collection using the ‘add’ function. An iterator is defined, and the elements ...
Read MoreRetrieving Elements from Collection in Java- EnumerationIterator
EnumerationIterator doesn’t have the option of eliminating elements from the collection, whereas an iterator has this facility. An additional disadvantage of using EnumerationIterator is that the name of methods associated with EnumerationIterator is difficult to remember.ExampleFollowing is an example −import java.util.Vector; import java.util.Enumeration; public class Demo { public static void main(String args[]) { Vector day_name = new Vector(); day_name.add("Tuesday"); day_name.add("Thursday"); day_name.add("Saturday"); day_name.add("Sunday"); Enumeration my_days = day_name.elements(); System.out.println("The values are "); while (my_days.hasMoreElements()) ...
Read MoreWhat is the maximum possible value of an integer in Java ?
The MAX_VALUE is used to find the maximum possible value for an integer in Java. Let us see an example −Examplepublic 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 and ...
Read MoreUnreachable statement using final variable in Java
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 −Exampleclass 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 is ...
Read MoreJava Program for Longest Palindromic Subsequence
For longest Palindromic subsequence, the Java code is as follows −Examplepublic 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 MoreExamples of soft references and phantom references?
Soft references are often used to implement memory-sensitive caches. Let us see an example of soft references in Java −Exampleimport java.lang.ref.SoftReference; class Demo{ public void display_msg(){ System.out.println("Hello there"); } } public class Demo_example{ public static void main(String[] args){ Demo my_instance = new Demo(); my_instance.display_msg(); SoftReference my_softref = new SoftReference(my_instance); my_instance = null; my_instance = my_softref.get(); my_instance.display_msg(); } }OutputHello there Hello thereA class named Demo contains a function named ‘display_msg’, that displays the relevant message. Another ...
Read More