ArrayStoreException in java

ArrayStoreException is a run time exception which is thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects. Class Hierarchy of ArrayStoreException: java.lang.Object ↳ java.lang.Throwable ↳ java.lang.Exception ↳ java.lang.RuntimeException ↳ java.lang.ArrayStoreException Constructors of ArrayStoreException: public ArrayStoreException() It is used to construct an ArrayStoreException with … Read more

Call string class methods using string literals

Yes, we can call string class methods using string literals. Example package com.w3schools;   public class Test { public static void main(String args[]){ System.out.println("w3schools".charAt(3)); System.out.println("w3schools".indexOf(’i’)); } }package com.w3schools; public class Test { public static void main(String args[]){ System.out.println("w3schools".charAt(3)); System.out.println("w3schools".indexOf(‘i’)); } } Output p 5p 5 Java interview questions on String Handling Why string objects are … Read more

Mutable and immutable objects in java

Mutable objects Immutable objects are like variables. We not modify them once they are created. They are not final in nature. Examples: StringBuffer, StringBuilder, java.util.Date etc. Example class MutableStudent{ private String name;   MutableStudent(String name) { this.name = name; }   public String getName() { return name; }   public void setName(String name) { this.name … Read more

String constant pool in java

String literal/constant pool: represents a special heap memory part which is used to store string constants or string literals. JVM checks the string constant pool every time whenever a string literal is created. JVM will create a new object if string literal is not present in the string pool but no new object will be created … Read more

is string a primitive type or derived type?

String is not a primitive type, it is a derived type. Example public class Main {   public static void main(String[] args) { String dataBase = "Oracle"; System.out.println("DataBase: " + dataBase); String myWebsite = new String("w3schools.com"); System.out.println("Website: " + myWebsite); } }public class Main { public static void main(String[] args) { String dataBase = "Oracle"; … Read more

is string a keyword in java?

No, String is not a keyword in java. String is an object which is created by using String class. String objects are immutable and they can’t modified i.e. any change in the existing object will result into a new object. Example public class Main {   public static void main(String[] args) { String language = … Read more

String vs StringBuffer vs StringBuilder in java

String objects are immutable whereas StringBuffer and StringBuilder objects are mutable. StringBuffer and StringBuilder are same only difference is that StringBuilder is not synchronized whereas StringBuffer is. As StringBuilder is not synchronized, it is not thread safe but faster than StringBuffer. Use String, for a string which will be constant through out the application. Use … Read more

String literal in java

Literal: A literal represents any constant value of boolean, character, numeric, or string type. Example: “Jai”, 11 etc. String Literal Syntax: String stringReference= “Value”; String Literal Example: public class Main {   public static void main(String[] args) { String myName= "Jai"; System.out.println(myName); } }public class Main { public static void main(String[] args) { String myName= … Read more

Create string object in Java

How to create string object? Using String literal. Using new keyword. Using character array. 1. Using String literal: Syntax String website = "w3schools.com";String website = "w3schools.com"; Example public class Main {   public static void main(String[] args) { String website = "w3schools.com"; System.out.println(website); } }public class Main { public static void main(String[] args) { String … Read more

Immutable String Java

Dictionary meaning of Immutable: Unchanging over time or unable to be changed. Immutable String represents a unmodifiable or unchangeable object. I.e. State of the String object can not be changed. A new string will be created, if we try to change the string value. Let us take the below code. String str1 = "w3schools"; String … Read more