wrapper class and normal string declaration gives same output? [duplicate] - java

This question already has answers here:
How do I compare strings in Java?
(23 answers)
What is the Java string pool and how is "s" different from new String("s")? [duplicate]
(5 answers)
Closed 7 years ago.
I tried below given examples
String s=new String("cat");
String s1=new String("cat");
System.out.println(s==s1);
System.out.println(s.equals(s1));
this gives me output false and true.
String s="cat";
String s1="cat";
System.out.println(s==s1);
System.out.println(s.equals(s1));
in this case the output of both is true
may i know the difference between both
i heard both wrapper class and normal string declaration gives same output

Related

How to check if a character is inside a string or number in java [duplicate]

This question already has answers here:
How can I check if a single character appears in a string?
(16 answers)
How do I convert from int to String?
(20 answers)
How to check if a number contains a certain digit?
(3 answers)
Closed 2 years ago.
I tried to make a function that takes one argument (a number) and returns true/false if it has 6 inside of it. I know how to do that in python.
In python it should look like this:
def six(num):
num=str(num)
return "6" in num
How do I do it in Java?
public boolean check(int x) {
return Integer.toString(x).contains("6");
}

Java augment strings to string [duplicate]

This question already has answers here:
Java Equivalent to .NET's String.Format
(6 answers)
String replacement in java, similar to a velocity template
(12 answers)
Closed 4 years ago.
I need to augment some string like it's done by logback/famous loggers or property accessors.
String str= "I can {0} give {1} back your horses or restore your {2} to life.";
String[] got=new String[]{"not","you","dead"};
Are they any utility functions provided by any library that can convert str to replace {i} with indices in array?
Its most probably a duplicate and please refer me to some similar question.

Extract/Trim part of string, Java [duplicate]

This question already has answers here:
Java - removing first character of a string
(14 answers)
Closed 5 years ago.
Say String x = MynameisMary;
I want String Y to be "Mary"
How do i remove the first 8 characters to make Y equal to Mary??
You could use the substring method:
String result = x.substring(8);

Practical use of Java String intern method [duplicate]

This question already has answers here:
Real Life, Practical Example of Using String.intern() in Java?
(5 answers)
When to use intern() on String literals
(4 answers)
When should we use intern method of String on String literals
(14 answers)
Closed 5 years ago.
I know how "intern" method of String works. But is there any practical / real life scenario where we will actually need to use intern method ? In that case is there any benefit of using intern over not using it ?
Or is it just a nice to have feature; only to be asked in interviews :)

String manipulation boolean [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
String a ="abc";
return (a.substring(1)=="bc");
I tried to print the result of
a.substring(1)
which is also
"bc"
Why the result is false?
I think it's true.
== compares references and the value of primitives (int, long etc), use a.substring(1).equals("bc") instead.
It should be like this:
String s = "abc";
System.out.println(s.substring(1).equals("bc"));

Categories