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
Java Articles
Page 13 of 450
Retrieving 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 MoreVerification in Java (JVM)
Once the byte code is loaded by the JVM, (with the help of the .class file), the bytecode is checked to see the validity with the help of the verifier. The verifier checks the linking so as to perform operations efficiently. This way, the interpreter performs much efficiently. This process is known as verification.Examplepublic class Demo{ private float my_val; float my_function(int my_val){ int balance = my_val; this.my_val += balance; return this.my_val; } public static void main(String[] args){ Demo my_obj = new Demo(); ...
Read MoreObject Graph in Java Serialization
An object graph contains a set of objects that are automatically serialized given that the object that contains the reference is serialized too. Any object that is serialized and contains an object reference, the object reference will be serialized by the JVM.Exampleimport java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; class One implements Serializable{ Two s2 = new Two(); } class Two implements Serializable{ Three s3 = new Three(); } class Three implements Serializable{ int i = 34; int j = 67; } public class Demo_Serialize{ public static void main(String args[]) throws Exception{ ...
Read MoreHow to check if a string is a valid keyword in Java?
To check if a string is a valid keyword in Java, the code is as follows −Exampleimport java.util.*; public class Demo{ static boolean valid_identifier(String my_str, int n){ if (!((my_str.charAt(0) >= 'a' && my_str.charAt(0) = 'A' && my_str.charAt(1) = 'a' && my_str.charAt(i) = 'A' && my_str.charAt(i) = '0' && my_str.charAt(i)
Read MoreComplex Numbers in Java
Complex numbers are those that have an imaginary part and a real part associated with it. They can be added and subtracted like regular numbers. The real parts and imaginary parts are respectively added or subtracted or even multiplied and divided.Examplepublic class Demo{ double my_real; double my_imag; public Demo(double my_real, double my_imag){ this.my_real = my_real; this.my_imag = my_imag; } public static void main(String[] args){ Demo n1 = new Demo(76.8, 24.0), n2 = new Demo(65.9, 11.23), temp; temp ...
Read MorePrecision Handling in Java
Let us see how precisions are handled in Java −Exampleimport java.io.*; import java.lang.*; public class Demo{ public static void main(String[] args){ double my_val = 34.909; System.out.println("The formatted value of 34.909 is "); System.out.println(String.format("%.7f", my_val)); double my_val_2 = 12.56; System.out.println("The formatted value of 12.56 is "); System.out.println(String.format("%.9f", my_val_2)); } }OutputThe formatted value of 34.909 is 34.9090000 The formatted value of 12.56 is 12.560000000A class named Demo contains the main function, where a double valued integer is declared, and it is formatted ...
Read More