Found 7442 Articles for Java

Java program to accept the strings which contain all vowels

George John
Updated on 25-Jun-2020 14:11:08

462 Views

In order to find that a given string contains all vowels we have to first convert given string into character array so that we can simplify the comparison of each character of given string.After this put each character into a hash map so that we can check whether our map created from given string contain all vowels or not.We took help of hash map here because there is no concrete method in character array class which could check that it contains all vowels or not.Only way to check is to iterate whole array and compare each character with each vowel ... Read More

Writing a CSV file in Java using OpenCSV

Chandu yadav
Updated on 25-Jun-2020 14:11:46

859 Views

In Java there is no delegate library or API in order to write or read comma separated value(csv) file.So a 3rd party APIs are available for such requirements.Most popular 3rd party API is OpenCSV which is provide methods to deal with CSV file such as read csv file, write csv file, parse values of csv file, mapping of csv file values to java beans and java beans to csv file etc.In order to use/import this tool in Java project there are following approaches −Download the binaries/jars from http://sourceforge.net/projects/opencsv/Download through maven by updating pom.xml as    net.sf.opencsv    opencsv    2.3 ... Read More

strictfp Keyword in Java programming

Ankith Reddy
Updated on 25-Jun-2020 14:12:11

284 Views

strictfp is used to ensure that floating points operations give the same result on any platform. As floating points precision may vary from one platform to another. strictfp keyword ensures the consistency across the platforms.strictfp can be applied to class, method or on interfaces but cannot be applied to abstract methods, variable or on constructors. Following are the valid examples of strictfp usage.Examplestrictfp class Test { } strictfp interface Test { } class A {    strictfp void Test() {    } }ExampleFollowing are the invalid strictfp usage.class A {    strictfp float a; } class A {    strictfp abstract void test(); } class A {    strictfp A() {    } }

static Keyword in Java programming

Arjun Thakur
Updated on 25-Jun-2020 14:12:49

383 Views

The Static ModifierStatic VariablesThe static keyword is used to create variables that will exist independently of any instances created for the class. Only one copy of the static variable exists regardless of the number of instances of the class.Static variables are also known as class variables. Local variables cannot be declared static.Static MethodsThe static keyword is used to create methods that will exist independently of any instances created for the class.Static methods do not use any instance variables of any object of the class they are defined in. Static methods take all the data from parameters and compute something from ... Read More

Sorting a HashMap according to keys in Java

George John
Updated on 25-Jun-2020 14:13:27

2K+ Views

As we know that Hash map in Java does not maintain insertion order either by key or by order.Also it does not maintain any other order while adding entries to it.But Java provide another API named as TreeMap which maintain its insertion order sorted according to the natural ordering of its keys.So in order to sort our given Hash map according to keys we would insert our map into a tree map which by default insert according to key sorting.After insertion we would transverse same tree map which is sorted and is our resultant sorted map.Example Live Demoimport java.util.HashMap; import java.util.TreeMap; ... Read More

Referencing Subclass objects with Subclass vs Superclass reference

Ankith Reddy
Updated on 25-Jun-2020 14:15:01

3K+ Views

In java inheritance some of the basic rules includes −Object relation of Superclass (parent) to Subclass (child) exists while child to parent object relation never exists.This means that reference of parent class could hold the child object while child reference could not hold the parent object.In case of overriding of non static method the runtime object would evaluate that which method would be executed of subclass or of superclass.While execution of static method depends on the type of reference that object holds.Other basic rule of inheritance is related to static and non static method overriding that static method in java ... Read More

Random vs Secure Random numbers in Java

Arjun Thakur
Updated on 25-Jun-2020 14:15:38

4K+ Views

Java provides two classes for having random numbers generation - SecureRandom.java and Random.java.The random numbers can be used generally for encryption key or session key or simply password on web server.SecureRandom is under java.security package while Random.java comes under java.util package.The basic and important difference between both is SecureRandom generate more non predictable random numbers as it implements Cryptographically Secure Pseudo-Random Number Generator (CSPRNG) as compare to Random class which uses Linear Congruential Generator (LCG).A important point to mention here is SecureRandom is subclass of Random class and inherits its all method such as nextBoolean(), nextDouble(), nextFloat(), nextGaussian(), nextInt() and ... Read More

Java program to print all unique words of a string

George John
Updated on 16-Aug-2024 23:25:13

2K+ Views

In this article, we will learn to find unique words in a string using the Map utility of Java because of its property that it does not contain duplicate keys. To find unique words first get all words in the array to compare each word, for this split string based on space/s. If other characters such as comma(, ) or full stop (.) are present then using required regex first replace these characters from the string. Insert each word of string as the key of Map and provide the initial value corresponding to each key as is unique if this ... Read More

Java program to check occurence of each vowel in String

Chandu yadav
Updated on 25-Jun-2020 14:16:42

745 Views

To count occurence of vowels in a string again use Map utility of java as used in calculating occurence of each character in string.Put each vowel as key in Map and put initial value as 1 for each key.Now compare each character with key of map if a character matches with a key then increase its corresponding value by 1.Examplepublic class OccurenceVowel {    public static void main(String[] args) {       String str = "AEAIOG";       LinkedHashMap hMap = new LinkedHashMap();       hMap.put('A', 0);       hMap.put('E', 0);       hMap.put('I', 0);       hMap.put('O', 0);       hMap.put('U', 0);       for (int i = 0; i

Print a 2D Array or Matrix in Java

George John
Updated on 14-Sep-2023 01:39:23

32K+ Views

In this post we will try to print an array or matrix of numbers at console in same manner as we generally write on paper.For this the logic is to access each element of array one by one and make them print separated by a space and when row get to end in matrix then we will also change the row.Example Live Demopublic class Print2DArray {    public static void main(String[] args) {       final int[][] matrix = {          { 1, 2, 3 },          { 4, 5, 6 },          { 7, 8, 9 }       };       for (int i = 0; i

Advertisements