• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
Bartenders:
  • Himai Minh
  • Carey Brown
  • Frits Walraven

confusing strings .... help me out

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1]
String s9 = "addme";
String s10 = "add";
String s11 = "me";
System.out.println(s9 == (s10+s11));
System.out.println(s9 == "add" + "me");
in the above ex.
i get a false & true. i don't get the diff between the two statements. please explain
2]
String s3 = "java";
String s4 = s3+"";
String s5 = new String("java");
String s6 = s5+"";
System.out.println(s3 == s4);
System.out.println(s3.equals(s4));
System.out.println(s5 == s6);
System.out.println(s5.equals(s6));
here i get all four ans as true
but in the code below ans is false .
String str = "SCJP";
String str2 = ""; // There is no space
// between double quotes
System.out.println(str == str + str2);
THIS IS VERY CONFUSING . HELP ME OUT OF THIS PLEASE.
3]as per the rule of unary implicit conversions short,byte,char values are incremented to int.
then why
char c = 'a';
c = ++c;
OR
byte b = 10;
b = ++b;
does not give any errors.
4] khalid mughal on pafe 126 says transient variables can not be static.
i tried the following code and it works fine.
class abc{
transient int i = 10;
}

but if i change it to
class abc{
public static void main(String args[]){
transient int a = 10;
}
}
it does not compile.
why?
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shilpa.Well,those are very confusing and thought provoking questions.I'll try my best to answer those,so here goes....
1)The result of the String concatenation operator (+) is a reference to a newly created String object that is the concatenation of the two operand strings.

A compile-time constant expression is an expression denoting a value of a String that is composed using only the following:literals of type String.
String literals,or, more generally, strings that are the values of constant expressions are "interned" at compile time so as to share unique instances, using the method String.intern().
The String literals "add" and "me" are constants,hence are added to the String pool by the compiler.
The value of the expression (s10+s11) cannot be determined until runtime.When the concatenation is performed,a new String object is returned.When compared to s9 using ==,it gives FALSE.
Hence,in the above case,the new String object,"add"+"me",which gives "addme" ,when compared using == to s9, gives TRUE.
2)Same explanation as above.
3)For unary operators,if the operand is a byte,short or char,it is converted to an int except if the operator is ++ or --.
Please check the Errata of the book you are using.I used RHE,and i caught the error in the Errata.
4)Variables may be marked transient to indicate that they are not part of the persistent state of an object.
The state of the Object is it's data members.The methods are the Actions which may be done on that data in order to Modify the state of the Object.
Hence the following worked.

But the local variables of a method do not constitute the Object's state.Local variables will go out of scope as soon as method execution is over.Hence the following did not work.

Object Oriented Technology gives more importance to the Data,not to the Methods acting on that Data.
I hope that helps.Please feel free to correct me.

------------------
Udayan Naik
Sun Certified Java 2 Programmer
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Naik's explanation is very clear,but I want to summarize in short words(my english isn't good ,I try my best):
1) and 2) are the same questions:
They are treated as literal string when the codes are compiled.
3) ++ /-- /
The mathematic operation will not be got prompted.
Another notice , (operator)= ,the result will be casted to the original primitive type after getting prompted.
4)remember the golden rule :
Local(automatic)variable can only at most have one modifier which is final.
 
shilpa gupta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx guys
tel me another thing can transient var. then be static or not.
writing static transient int i = 2;(as instance var)
gives no errors.
and in Q 2]
am i not creating a new String when i write
s4 = s3 +"";
 
petfishlf
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
q 1) You can declare a static variable as transient, but in fact it's not any meaning.Because static variable (class variable) can guarantee to not being serialized without transient modifier.
q 2)
s4 = s3 +"";
that will not create a new String instance. This is a special concatenation when you + "" to a string,which return the original string reference. So s4=s3 is absolutely right.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, buddies,
i think sun should make some meaningfull improvement in sdk1.3,
String s3="java";
String s4=s3+"";
String s5=s3.concat("");
System.out.println(s3==s4); //print false!
System.out.println(s3==s5); //print true!
those things are meaningless, right? we all should make some
usefull things in java, otherwise, i choose C++.
can Duke sun give me an answer?
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
zhljgh,
Actually if you read and understand the API it makes perfect sense.
In the API it says that the concat() method looks for a zero length argument, and if it finds it - returns the original string.
SO
System.out.println(s3==s5); //Is just like asking is s3==s3 TRUE
In String s3="java";
The system resolves the string literal at compile time and loads it into the Constant Pool at class load time. s3 is pointing to that literal.
However in
String s4=s3+"";
The JVM will not resolve this until runtime (figure out what is in the s3 variable) so it does not use the compile time constant pool. Instead it creates another string that holds the results of the + operation. so s4 is POINTING at a diffent string that HAPPENS to .equal s3. The references that s3 and s4 hold are different.


concat
public String concat(String str)
Concatenates the specified string to the end of this string.
If the length of the argument string is 0, then this String object is returned. Otherwise, a new String object is
created, representing a character sequence that is the concatenation of the character sequence represented by this
String object and the character sequence represented by the argument string.
Examples:
"cares".concat("s") returns "caress"
"to".concat("get").concat("her") returns together"


 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh yeah zhljgh,
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please choose a new name which meets the requirements.
Thanks.

 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic