Found 593 Articles for Java Programming

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

Arjun Thakur
Updated on 30-Jul-2019 22:30:23
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
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;    } }

transient Keyword in Java

George John
Updated on 30-Jul-2019 22:30:23
An instance variable is marked transient to indicate the JVM to skip the particular variable when serializing the object containing it. This modifier is included in the statement that creates the variable, preceding the class or data type of the variable. Example public transient int limit = 55; // will not persist public int b; // will persist

synchronized Keyword in Java

Ankith Reddy
Updated on 23-Jun-2020 15:32:55
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

static Keyword in Java

Arjun Thakur
Updated on 23-Jun-2020 15:33:28
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

Public Keyword in Java

George John
Updated on 23-Jun-2020 15:20:56
Public Access Modifier - PublicA class, method, constructor, interface, etc. declared public can be accessed from any other class. Therefore, fields, methods, blocks declared inside a public class can be accessed from any class belonging to the Java Universe.However, if the public class we are trying to access is in a different package, then the public class still needs to be imported. Because of class inheritance, all public methods and variables of a class are inherited by its subclasses.ExampleThe following function uses public access control −public static void main(String[] arguments) {    // ... }The main() method of an application ... Read More

Protected Keyword in Java

Chandu yadav
Updated on 23-Jun-2020 15:34:02
Protected Access Modifier - ProtectedVariables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.The protected access modifier cannot be applied to class and interfaces. Methods, fields can be declared protected, however methods and fields in a interface cannot be declared protected.Protected access gives the subclass a chance to use the helper method or variable, while preventing a nonrelated class from trying to use it.ExampleThe following parent class uses protected access control, to allow its child class override openSpeaker() ... Read More

Java program to check string as palindrome

Arjun Thakur
Updated on 23-Jun-2020 15:21:47
A string is Palindrome if position of each character remain same in case even string is reversed.For example 'MADAM' is a palidrome string as position of each character remain same even if string 'MADAM' is reversed.Now in order to identify a string as palindrome or not we can use library method approach and also without library method approach.But if we want to check if "Madam" is palindrome or not , it will show us that it is not a palindrome because of the upper case of first letter.Example - Without library method. Live Demopublic class Palindrome {    public static void ... Read More

Java program to check occurrence of each vowel in String

Chandu yadav
Updated on 23-Jun-2020 15:22:32
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

Private Keyword in Java

Ankith Reddy
Updated on 23-Jun-2020 15:23:06
Private Access Modifier - PrivateMethods, variables, and constructors that are declared private can only be accessed within the declared class itself.Private access modifier is the most restrictive access level. Class and interfaces cannot be private.Variables that are declared private can be accessed outside the class, if public getter methods are present in the class.Using the private modifier is the main way that an object encapsulates itself and hides data from the outside world.ExampleThe following class uses private access control −public class Logger {    private String format;    public String getFormat() {       return this.format;    } ... Read More