The thinking about String class's API - java

The following codes are based on Java.
Here, the famous method "toUpperCase()", can be invoked like this:
String s = new String("abc");
String ss = s.toUpperCase();
//result: ss = "ABC"
For this: s.toUpperCase() We didn't pass a value to the method, so this method cannot has a meaningful return value.
To explain what is called a not-meaningful return value, here is an example:
//not pass a value to the method
String toUpperCase(){
...
return ???;
}
As above, the return value has no relationship with that object called "s". (the invoking of a non-parameter method is often independent from the objects). If I invoke this non-parameter method: String ss = s.toUpperCase(); how can that return a value that has any relationship with the object "s"
And the following is called meaningful:
//pass a value to the method
static String toUpperCase(String str){
.... //to change the "str" into uppercase
return str;
}
Now I can invoke the method toUpperCase() like this:String ss = String. toUpperCase(s);
Since I pass the "s" (its address) to the method, I can do anything I wish to change the object and return a meaningful return value.
Based on the above, I have a reasonable doubt about this method s.toUpperCase(). since it cannot return a meaningful return value.

First, terminology. s is an instance.
how can that return a value that has any relationship with the object "s"
Do you understand this? For example, how does dog.getName() return a "meaningful value" of the name of that dog instance?
The char[] of the String object named s is stored within that instance, much like the String name of a Dog object.
class Dog {
String name;
public String getName() { return name; } // or 'return this.name;'
}
Bonus: Source code of toUpperCase() implementation, which does make a call to Character.toUpperCase(c) because char primitives to not have a toUpperCase method themselves.

Related

After override hashCode, toString() return the same value for the same object but after using "==" return false

As you may know Object has some function,
For example we have toString() from oracle Documentacion we can know by default it's return HexValue of hashCode()
https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString()
We also can use hashCode() for checking of equality object (of course implementation depends on you)
So I made implementation for my Class Projekt :
public class Projekt {
int i;
public Projekt(int i) {
this.i=i;
// TODO Auto-generated constructor stub
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + i;
return result;
}
}
Test Code:
Projekt projekt = new Projekt(1);
System.out.println(projekt.toString());
System.out.println(projekt.toString() == projekt.toString());
Output:
Projekt#20
false
Also i try to inside value from projekt.toString() in StringPool by writing:
String string = projekt.toString();
String stringABC = projekt.toString();
System.out.println(string == stringABC);
According to PoolString i should have the same reference but outPrint is false.
So this method return different reference value but i can't understand why?
From your comment:
I copied this from diffrent comenatry: But to save memory we're using StringPool. If we have the same value, refereance is also the same it's working just for creating String not by constructor. If i had String s = new String("a"); String s2 = new String("a"); I used 2 space in memory, but when i use String s = "a"; String s2 = "a" I use 1 space in memory. So that's mean toString() return "new String()"?
Your source should have said:
...it's working just for creating String using string literals.
That way it might have been clearer, because there are many ways to create new String objects without directly calling the constructor. String literals are those things surrounded by " (including the quotes), such as "a" or "b" or "Welcome to Stack Overflow".
Only string literals1 are pooled automatically. You can manually put a string into the pool by calling intern().
When you concatenate two strings (e.g. stringA + stringB), a new string is generally created, as described here.
Now let's look at what Object.toString does:
The toString method for class Object returns a string consisting of
the name of the class of which the object is an instance, the at-sign
character `#', and the unsigned hexadecimal representation of the hash
code of the object. In other words, this method returns a string equal
to the value of:
getClass().getName() + '#' + Integer.toHexString(hashCode())
Note that strings are being concatenated here, so a new string object is being created here, hence the output.
And to answer the question of:
So that's mean toString() return "new String()"?
Yes, but not directly. The compiler will turn the + operators in Object.toString into some code using StringBuilder (or rather, "the compiler has turned..." since the Object class has already been compiled). At the end, it will call StringBuilder.toString, and that will create a new String object.
1Constant expressions of strings, to be more accurate.
This is because "toString()" method returns a string. So everytime you call toString() it returns a new reference because Strings are immutable (even if they're the same text)
String class is just a pointer to string (char) data somewhere in the memory. It is a class, it is not a primitive type like boolean, byte, char, short, int, long, float and double.
So, comparing two strings you compare pointers.
You generate two strings in:
String string = projekt.toString();
String stringABC = projekt.toString();
So you have 2 different pointers now. They have the same text inside, but they are in different places in memory.
But you can use equals() method of object:
System.out.println(string.equals(stringABC));

String concat() return type

while doing brush up my knowledge seems the below code & surprised how it works. If anybody know, explain it.
class test1 {
public static void main(String[] ar) {
String s1 = "abc";
s1.concat("ef");
System.out.println(s1);
}
}
Actual Output:
abc
Expected Output:
compile time error since concat() return type is string & it is not returned.
The String object type is immutable. For getting the concatenated value you must do this:
String s1 = "abc";
s1 = s1.concat("ef");
You don't get a compile time error because it is not necessary to store into a variable the returned value of the concatenation.
When you call a method, you don't specify any return type, you can specify the variable in which you want to store the returned value specified by the method declaration basicly like :
public String concat(String s){
return this.value + s;
}
When you call a method :
"foo".concat("bar"); //return "foobar"
The String instance with the value "foobar" will be returned, but you are not attributing it to any variable, it is allowed even if in your case, it doesn't make sense since the result of that concatenation will be lost.
Now, if you are trying to explain that you can't not have a method like :
public String concat(String s){
this.value + s;
}
You are correct, you can't implement a method declared with a return type but not returning anything. It will not compile.

How come printing a string(an Object) in java prints it?

When we do:
String string = new String("Ralph");
//This creates a reference called string that points to a sequence of
//characters in memory
This is the same as:
String string = "Ralph";
When we print both, we get the actual value of the string.
If we print any other object in Java, we get an address for that object.
My question is, is there any dereferencing that is taking place behind the scenes?
When you pass an object reference to the System.out.println() method, for
example, the object's toString() method is called, and the returned value of toString() is shown in the following example:
public class HardToRead {
public static void main (String [] args) {
HardToRead h = new HardToRead();
System.out.println(h);
}
}
Running the HardToRead class gives us the lovely and meaningful,
% java HardToRead
HardToRead#a47e0
Now,
Trying to read this output might motivate you to override the toString()
method in your classes, for example,
public class BobTest {
public static void main (String[] args) {
Bob f = new Bob("GoBobGo", 19);
System.out.println(f);
}
}
class Bob {
int shoeSize;
String nickName;
Bob(String nickName, int shoeSize) {
this.shoeSize = shoeSize;
this.nickName = nickName;
}
public String toString() {
return ("I am a Bob, but you can call me " + nickName +". My shoe size is " + shoeSize);
}
}
This ought to be a bit more readable:
% java BobTest
I am a Bob, but you can call me GoBobGo. My shoe size is 19
The class String is a special class in Java.
But it gets out printed the same way every other class does.
If we call System.out.println("Ralph") the function println takes that String and then displays it.
The class Objects toString() method is implemented, so it displays the hash code of the Object, by calling the hashCode() function. If you overwrite the toString() method, it will display something else.
If you take any object other than a String and give it to a method that takes a String (or in fact cast it to a String) java will call the toString()method of that Object, to convert it to a String.
So 'printing' always does the same thing, it's just implemented in different ways, using the toString() method.
new String("Ralph") copies the character data array of the literal string and stores it in the new String instance.
However, you only get the address of an object when you print it because printing uses the toString() method of that object. If that method is not implemented, the default implementation defined in Object is used, which returns the class name plus the hash code (that seems like an address if hashCode() is not implemented).
I believe the primary types are printed with auto dereferencingString, int, float, etc., while the objects other than primary types, only with de-referencing function object.toString() that are implemented on the object level.

What happens when printing an object in java

class Data {
int a = 5;
}
class Main {
public static void main(String[] args) {
int b=5;
Data dObj = new Data();
System.out.println(dObj);
System.out.println(b);
}
}
I want to know what's happening when printing a object or number or string.
I ran the above code, I'm getting the result as "data#1ae73783" for System.out.println(dObj); and "5" for System.out.println(b);
Then I did debug to check whats really happening when printing a object, there was lot of parameter called in a debug mode(like classloader,theards)
I know for the first print the value represent class name followed by address. But don't know what's really happening in debug mode, for the 2nd print only variable assignment happened in the debug mode i.e b=5.
Please explain whats really happening?
You don't need a debugger to know what's happening. System.out is of type PrintStream. The javadoc of PrintStream.println(Object) says:
Prints an Object and then terminate the line. This method calls at first String.valueOf(x) to get the printed object's string value, then behaves as though it invokes print(String) and then println().
The javadoc of String.valueOf(Object) says:
if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.
And the javadoc of Object.toString() says:
Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.
The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `#', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:
getClass().getName() + '#' + Integer.toHexString(hashCode())
Please explain whats really happening?
As other have told you, using System.out.println with an object will call to toString method on that object. If the class doesn't have it's own toString method, then it's a call to the super class's toString. If the super class call goes all the way back to java.lang.Object, the default toString method prints the name of the object's type (what class it is), followed by an # sign, and the memory location of the object--the hexidecimal address of where that object is stored in memory.
ClassName#MemoryLocation
when we print object of any class System.out.print() gives string of class name along with memory address of object (ClassName#MemoryAdress)
All objects inherit from java.lang.Object which has a default implementation of toString. If an object overrides this method then out.print (obj) will put something useful on the screen.
Primitive data types are handled by a different, much simpler implementation of println. The println method is overridden for every data type in addition to Object.
First, int isn't an Object. It's primitive type.
Second, when Class haven't overrived toString() method, toString() from Object class is invoked.
data dObj = new data() does not exist in the source code;
you want to print the string value of the object (Data), you have to override the toString method;
try this
public class Program {
public static void main(String[] args) {
Data data = new Data();
System.out.println(data);
}
}
class Data {
int a = 5;
#Override
public String toString() {
return String.valueOf(a);
}
}
In Addition to #JB Nizet answer,
To Provide our own string representation, we have to override toString() in our class which is highly recommended because
There are some classes in which toString() is overridden already to get the proper string representation.
Examle: String, StringBuffer, StringBuilder and all wrapper classes
Integer ob1 = new Integer("10");
String ob2 = new String("Doltan Roy");
StringBuffer ob3 = new StringBuffer("The Rock");
StringBuilder ob4 = new StringBuilder("The Joshua");
System.out.println(ob1);
System.out.println(ob2);
System.out.println(ob3);
System.out.println(ob4);
Output:
10
Doltan Roy
The Rock
The Joshua
Hope this would help!

toString method for varargs constructor

I have a varargs constructor like this :
public class Sentence {
public String[] str;
public Sentence(Object... text){
StringBuilder sb = new StringBuilder();
for (Object o : text) {
sb.append(o.toString())
.append(" ");
}
System.out.println(sb.toString());
}
}
The class can can accept various types of data via this constructor (ints, strings, and Sentence objects as well). How do I create a proper toString method for such a class?
Not sure exactly what you're trying to achieve, but as the toString() method...
Returns a string representation of the
object
...I guess I would store the result of your sb.toString() call as a member String variable (say private String asString), and return that in a toString() method overriding the method in Object:
public String toString() {
return this.asString;
}
You can't print Sentence in toString because you can get infinite loop if you initialize 2 Senence objects by each other.
So, print all strings and objects and if it's possible some data from Sentence object, or only "1 sentence object" to point that it's also passed as an init param.

Categories

Resources