Whatz wrong with this code ?
What is wrong with this below code... ? I am trying to find the adress locations .. i am trying to know what are the values of s1 and s2 that is the references of the String objects that JVM creates.
The output of teh code is partly right that s1 and s2 are not equal by means of teh operator "==", because the values of the String objects dont carry the objects coz, s1 and s2 are not equal.
But I practically want to know what are the values, i.e, the adress locations of the references. I know that the equals() method would tell me that s1 and s2 are equal.. i think i am being clear. But i wanna know the address values and wanna prove myself that s1 and s2 are not equal using "==" operator, but equal using equals(s1,s2) as is obvious...but how do i do it..? what part of code is throwing the exception?
Also how do i get rid off the exceptions that is shown in the output ?
MY CODE IS:
import java.lang.*;
import java.io.*;
class One{
public static void main(String a[]) throws IOException{
String s1="hello";
String s2= new String("hello");
System.out.println(s1);
System.out.println(s2);
if(s1==s2)
{System.out.println("Both s1 and s2 are equal");
}
else System.out.println("s1 and s2 are not equal");
int x = Integer.parseInt(s1);
int y = Integer.parseInt(s2);
System.out.println("s1 is:\t"+x);
System.out.println("s2 is:\t"+y);
}
}
OUTPUT IS:
C:\Program Files\Java\jdk1.6.0_02\bin>javac One.java
C:\Program Files\Java\jdk1.6.0_02\bin>java One
hello
hello
s1 and s2 are not equal
Exception in thread "main" java.lang.NumberFormatException: For input string: "h
ello"
at java.lang.NumberFormatException.forInputString(NumberFormatException.
java:48)
at java.lang.Integer.parseInt(Integer.java:447)
at java.lang.Integer.parseInt(Integer.java:497)
at One.main(One.java:20)
C:\Program Files\Java\jdk1.6.0_02\bin>
Kindly help me out !
I am infact trying to understand the concept of Mutable and Immutable objects ! I was trying to understand through this code !
Message was edited by:
aron_phil
[2272 byte] By [
aron_phila] at [2007-11-27 9:59:18]

> What is wrong with this below code... ? I am trying
> to find the adress locations
You can't do that in Java.
>.. i am trying to know
> what are the values of s1 and s2 that is the
> references of the String objects that JVM creates.
You can't do that in Java.
> But I practically want to know what are the values,
> i.e, the adress locations of the references.
You can't do that in Java.
> wanna prove myself that
> s1 and s2 are not equal using "==" operator,
Then just test s1 == s2 or s1 != s2.
> what part of code is throwing the
> exception?
Whatever line the error message tells you.
> Also how do i get rid off the exceptions that is
> shown in the output ?
Since it's a NumberFormatException, you get rid of it by passing a string that's of a valid format to be parsed by the method you're calling. For instance "123" instead of "x1y2z3" to Integer.parseInt.
> I am infact trying to understand the concept of
> Mutable and Immutable objects !
You can change the state (contents, member variables, etc.) of a mutable object. You can't change the state of an immutable object.
[code]int x = Integer.parseInt(s1);[code]What would you expect the int value of "hello" to be? "hello" does not represent a base-10 integer.
infact i was trying to understand the fact practically that when an object is created it doesnt store the data in itself , but the object name carries the address location that infact carries the data that this object name is pointing to ! so i was trying to get a value of s1.
> infact i was trying to understand the fact
> practically that when an object is created it doesnt
> store the data in itself , but the object name
> carries the address location
Not exactly, but it's generally safe to view it that way.
> that infact carries the
> data that this object name is pointing to ! so i was
> trying to get a value of s1.
Okay. But do the docs for Integer.parseInt say that it returns the integer value of the address?
To take a reductio ad absurdum approach, if you think Integer.parseInt(s1) gives you the int value of the "address" of the object, then you must think that s1 stores that address as a string.
Furthermore, if you ever wanted to parse the string "123" and your s1 variable pointed to it, how would parseInt know that it should be parsing the String object "123" and not the "address" that's stored (presumably as a string) in s1?
ya true, but if i print just s1 it prints the objects value"hello", but since the value of s1 itself is and adress in string form, i was trying to parse it i.e, convert the string variable s1 into integer... i used the pareseInt method mainly to convert from String type into int type.. how far i am right ?
1. s1,s2 are objects. if you give s1 ==s2 then it will compare the memory locations. it should be different. so it will return false.2. s1="hello"this string can't be convert to number.but if s1="100", then it will work.
> but since the value of s1 itself is and
> adress in string form,
No, it is not.
It is not, strictly speaking, an address, and it is certainly not in string form. Where'd you get the idea that it was?
> i was trying to parse it i.e,
> convert the string variable s1 into integer... i used
> the pareseInt method mainly to convert from String
> type into int type.. how far i am right ?
You're right in that parseInt converts from String to int. But did you read any of my last few posts? You're making it more complicated than it is.
s1's value is a reference--roughly an address. But that value is opaque to you. You cannot know that value in Java code. All you can do with s1 is use it to get to the object it points to, compare it for equality with other refernce values, and assign other reference values or null to it.
I'm sure much more can be said about the "address locations of the references"... But:
> I am infact trying to understand the concept of Mutable and Immutable objects ! I
> was trying to understand through this code !
From Wikipedia - "In object-oriented and functional programming, an immutable object is an object whose state cannot be modified after it is created."
Now the state of a String is its text: that is, the sequence of letters that comprise the string. So to observe, through code, that a String is immutable you would:
(1) Look at the state (the text) for example with System.out.println()
(2) Invoke some of the string's methods in an attempt to change the text
(3) Look at the state once again and observe that it has not changed.
Your code doesn't seem to do anything like this.
Of course to prove the immutability of a string you will have to consult the API documentation and verify
(1) That its state really is its text - ie the sequence of characters really does determine the behaviour of all of its methods. (String has no fields to worry about).
(2) That this text is not altered by calling any of the string's methods.
> 1. s1,s2 are objects.No, they're reference variables. > if you give s1 ==s2 then it> will compare the memory locations.No, it will compare reference values.