Programming Articles - Page 3295 of 3366

What is the difference between String s1 = "Hello" and String s1= new String("Hello") in java?

Rishi Raj
Updated on 19-Feb-2020 12:48:04

3K+ Views

When you store a String asString str1 = "Hello";directly, then JVM creates a String object with the given value in a separate block of memory known as String constant pool.And whenever we try to create another String asString str2 = "Hello";JVM verifies whether any String object with the same value exists in the String constant pool, if so, instead of creating a new object JVM assigns the reference of the existing object to the new variable.And when we store String asString str = new String("Hello");using the new keyword, a new object with the given value is created irrespective of the ... Read More

How many ways a String object can be created in java?

George John
Updated on 30-Jul-2019 22:30:20

1K+ Views

You can create a String by − Step 1 − Assigning a string value wrapped in " " to a String type variable. String message = "Hello Welcome to Tutorialspoint"; Step 2 − Creating an object of the String class using the new keyword by passing the string value as a parameter of its constructor. String message = new String ("Hello Welcome to Tutorialspoint"); Step 3 − Passing a character array to the String constructor. char arr[] = {'H','e','l','l','o'}; String message = new String(arr);

What is the meaning of immutable in term of String in java?

Arushi
Updated on 30-Jul-2019 22:30:20

287 Views

In Java, immutable objects are those whose data can’t be changed or modified (once modified). String class is immutable i.e. once we create a String object its data cannot be modified.

Which package is used for pattern matching with regular expressions in java?

Paul Richard
Updated on 30-Jul-2019 22:30:20

301 Views

Java provides the java.util.regex package for pattern matching with regular expressions.

What is the difference between transient and volatile in Java?

Rishi Raj
Updated on 26-Feb-2020 10:11:11

758 Views

transient: 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.Examplepublic transient int limit = 55;   // will not persist public int b;   // will persistvolatile: 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 ... Read More

What does the modifier volatile in Java do?

Jai Janardhan
Updated on 19-Feb-2020 12:41:26

636 Views

The volatile modifier is used to let the JVM understand that a thread accessing the variable should always merge its own personal copy of the variable with the original in the memory.Accessing a volatile variable synchronizes all the cached copy 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;      } }

What does the modifier transient in Java do?

George John
Updated on 30-Jul-2019 22:30:20

255 Views

An instance variable is marked transient to point the JVM to skip the actual variable once serializing the thing containing it. This modifier is included in the statement that creates the variable, preceding the class or data type of the variable. Example public class Employee implements java.io.Serializable { public String name; public String address; public transient int SSN; public int number; public void mailCheck() { System.out.println("Mailing a check to " + name + " ... Read More

Can access modifiers be used for local variables in Java?

Arushi
Updated on 30-Jul-2019 22:30:20

814 Views

Yes, a local variable can be public, private, protected or default.

How to use the final modifier in Java?

Moumita
Updated on 25-Oct-2024 23:40:58

1K+ Views

In this article, we will learn to use the final modifier in Java. The final modifier can be associated with methods, classes, and variables. Once we declare it final − A final class cannot be instantiated. A final method cannot be overridden. A final variable cannot be reassigned. These restrictions make code more predictable and can prevent unintentional modifications. Steps to use the final modifierFollowing are the steps to use the final modifier −First, we will define a class TestExample and declare a final variable value ... Read More

What is the difference between ++i and i++ in c?

Jayashree
Updated on 12-Sep-2023 02:57:07

38K+ Views

In C, ++ and -- operators are called increment and decrement operators. They are unary operators needing only one operand. Hence ++ as well as -- operator can appear before or after the operand with same effect. That means both i++ and ++i will be equivalent.i=5; i++; printf("%d", i);and i=5 ++i; printf("%d", i);both will make i=6.However, when increment expression is used along with assignment operator, then operator precedence will come into picture. i=5; j=i++;In this case, precedence of = is higher than postfix ++. So, value of i is assigned to i before incrementing i. Here j becomes 5 and i becomes 6.i=5; ... Read More

Advertisements