Found 9326 Articles for Object Oriented Programming

Java program to check occurence of each vowel in String

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

528 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

351 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

Passing and Returning Objects in Java

Chandu yadav
Updated on 25-Jun-2020 14:08:38

4K+ Views

As we know it is core concept that in Java there is always pass by value and not by pass by reference.So in this post we will focus on that how this concept get validated in case of passing primitive and passing reference to a method.In case when a primitive type is passed to a method as argument then the value assigned to this primitive is get passed to the method and that value becomes local to that method, which means that any change to that value by the method would not change the value of primitive that you have ... Read More

Constructor Chaining In Java programming

Rudradev Das
Updated on 29-Dec-2023 18:36:32

638 Views

The constructor chaining is a particular sequence of injecting constructors when an user initialize an object in a particular method. This process can be used when we invoke a bulk number of constructors one by one only on the basis of the instance class. This process is an another method linked with the inheritance where the task of a sub-class constructor to call a super class constructor. Constructor chaining can be performed in two ways in Java − Within same class − The process can be done by the using of this() keyword for the constructors present in the same class. From base class − by using the ... Read More

Where objects, methods and variables are stored in memory in Java?

Arjun Thakur
Updated on 30-Jul-2019 22:30:23

3K+ Views

There are five main memory areas which are used to various Java elements. Following is the list of the same. Class Area - This area contains the static members of the class. Method Area - This area contains the method definition and executable code. Heap Area - This area contains the objects which are dynamically allocated/deallocated. if an object is no more referenced by any live reference it is deallocated. Stack Area - This area contains the local variables. Pool Area - Contains immutable objects like string.

volatile Keyword in Java

Chandu yadav
Updated on 23-Jun-2020 15:30:10

5K+ Views

The volatile modifier is used to let the JVM know that a thread accessing the variable must always merge its own private copy of the variable with the master copy in the memory.Accessing a volatile variable synchronizes all the cached copied of the variables in the main memory. Volatile can only be applied to instance variables, which are of type object or private. A volatile object reference can be null.Examplepublic class MyRunnable implements Runnable {    private volatile boolean active;    public void run() {       active = true;       while (active) {       }    }    public void stop() {       active = false;    } }

synchronized Keyword in Java

Ankith Reddy
Updated on 23-Jun-2020 15:32:55

2K+ Views

When we start two or more threads within a program, there may be a situation when multiple threads try to access the same resource and finally they can produce unforeseen result due to concurrency issues. For example, if multiple threads try to write within a same file then they may corrupt the data because one of the threads can override data or while one thread is opening the same file at the same time another thread might be closing the same file.So there is a need to synchronize the action of multiple threads and make sure that only one thread ... Read More

Advertisements