Skip to main content

Posts

Showing posts from September, 2020

String, StringBuffer and StringBuilder in Java | Convert String to StringBuffer or StringBuilder and vice versa

What is String, StringBuilder and StrinfBuffer in java? In this article we talk about String, StringBuffer and StringBuilder in java. String :-  String is class in java that represents sequence of characters, it can be word, number, special characters. String represents in Double Quotes (" ") . String is immutable in java. What is immutable? In simple word immutable means can not changable. it con not be replacable. So String is immutable in java. we can not alter String object.  So there is one question is pop up in your mind. We can still concatenate or changed String object in java so how it is immutable in Java? Yes, we can concatenate or changed String object but every time we perform these kind of operations it creates new Object of that. See below image you can easily understand. Lets see example of immutability of String. Example 1 :- String demo with immutability public class StringDemo {     public static void main(String[] args) {                  String str = &qu

What is equals() and hashCode() Method in Java? | What are use of equals() and hashCode() Method in Java

Java hashCode() and equals() Methods with Examples | When to use | How to Use You already known that Object is root of all classes in Java. So whenever we create any class in java then our class implicitly extends the Object class. There are so many methods available in Object. clone() equals() finalize() getClass() hashCode() notify() notifyAll() toString() wait() Check out this Java doc above methods https://docs.oracle.com/javase/10/docs/api/java/lang/Object.html Now in this article we talk about equals() and hashCode() method. so lets start. So first question in your mind pop up that why i use equals and hashCode method so lets see answer of that first. Why and in which condition we have to use equals() and hashCode() method in Java? When we want to use Object as key in hashTable then we should use equals() and hashCode() methods in java. equals() Method In simple term, equals() method checks that one object is 'equals to' another object. equals() method used to simply v

Super Keyword In Java With Examples - Programming Blog

Super Keyword In Java With Examples What is Super keyword in Java? | Super keyword with example in java In java, Super keyword is used to refer parent class object .  You are already familiar with inheritance in java. So super keyword is used when we extends any class and want to refer parent's class variable, method or constructor. You can use super keyword with following condition :- Variables - refer parent class instant variable Methods - refer parent class methods Constructors - refer parent class constructors  So lets discuss each of one by one with example. 1. Super keyword with parent class instant variable You can only refer instant variable directly using super keyword in java.  When you extends any class and want to use parent class instant variable then super keyword is used. Syntax :- super.parentClassInstantVariable So lets see example for better understanding. Example 1:- Call parent variable with super keyword public class SuperClass {     String instanceVariable