Found 34494 Articles for Programming

static Keyword in Java programming

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

184 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

1K+ 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

3K+ 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 25-Jun-2020 14:16:09

1K+ Views

To find unique words in a string use Map utility of java because of its property that it does not contain duplicate keys.In order to find unique words first get all words in array so that compare each word, for this split string on the basis of space/s.If other characters such as comma(, ) or fullstop (.) are present then using required regex first replace these characters from the string.Insert each word of string as key of Map and provide initial value corresponding to each key as 'is unique' if this word does not inserted in map before.Now when a ... Read More

Java program to check occurence of each vowel in String

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

536 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

28K+ 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

Primitive Wrapper Classes are Immutable in Java

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

1K+ Views

In Java Immutable class is a class which once created and it's contents can not be changed.On same concept Immutable objects are the objects whose state can not be changed once constructed.Wrapper classes are made to be immutable due to following advantages −Since the state of the immutable objects can not be changed once they are created they are automatically synchronized.Immutable objects are automatically thread-safe, the overhead caused due to use of synchronisation is avoided.Once created the state of the wrapper class immutable object can not be changed so there is no possibility of them getting into an inconsistent state.The ... Read More

POJO vs Java Beans

George John
Updated on 25-Jun-2020 14:07:32

4K+ Views

As we know that in Java POJO refers to the Plain old Java object.POJO and Bean class in Java shares some common features which are as follows −Both classes must be public i.e accessible to all.Properties or variables defined in both classes must be private i.e. can't be accessed directly.Both classes must have default constructor i.e no argument constructor.Public Getter and Setter must be present in both the classes in order to access the variables/properties.The only difference between both the classes is Java make java beans objects serialized so that the state of a bean class could be preserved in ... Read More

POJI in Java

Ankith Reddy
Updated on 25-Jun-2020 14:07:52

353 Views

POJI is an acronym for Plain Old Java Interface which corresponds to a Java standard interface which means that these interfaces are in the context of providing services in JEE. For example, OSGI service is offered through POJI in JEEIn other terms we can say that POJI is an ordinary interface without any specialty that is not inherited from any of the technology API specific interfaces or framework interfaces.Exampleinterface myCustomInterface {    public void myMethod(); } interface mySecondCustomInterface extends myCustomInterface {    public void mySecondMethod(); }Both interfaces would be called as POJI as they do not inherit any technology specific ... Read More

Advertisements