bongadi

Greenhorn
+ Follow
since Apr 17, 2000
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by bongadi

Prabhu,
It compiles for me.
could you post the compiler error you are getting.
Also please post the platform and JDK version you are using.

Originally posted by Prabhu:
Qstn I:
The answer is false. But I'm not sure why ?? I tried compiling but I get a compile error. But if I remove the transient, then it compiles just fine. So, others.. please lend me a hand
Qstn II:
Refer to <html> http://www.javaranch.com/ubb/Forum24/HTML/001428.html </html> . Same discussion.
Qstn III:
It's just Infinity. ( -ve * -ve = +ve). But if you change the statement as System.out.println(1 * Double.NEGATIVE_INFINITY); then it gives a -ve Infinity.
Prabhu.

[This message has been edited by Prabhu (edited May 17, 2000).]


If you have JDK1.2 installed then write a small program using
given options and compile it. You should get your answer.

Originally posted by Shafeeq Sheikh:
What is the correct declaration of an abstract method that is intended to be public:
1. public abstract void add();
2. public abstract void add() {}
3. public abstract add();
4. public virtual add();
Select the most appropriate answer.
The answer given is 1..... Isisn't this wrong??? I think the answer should be 2. (because of the curly braces)???/
From JB Kings mock exam....


Here's my contribution
- Classes derived from abstract and not providing body for all abstract methods.
- Classes implementing interface and not providing body for all methods.
- Derived classes from Base classes without no arg (default) c'tor and implicitly invoking super().
- Math.floor() and Math.ceil() with -ive numbers.
- >>> operations on -ive byte primitives.

Originally posted by maha anna:
I am posting this on behalf of Ajith.
Ajith's contribution


This is an small example to illustrate the verbose post by
Ajith
<pre>
public class StaticInnerEx {
StaticInnerEx() {
System.out.println( "C'tor StaticInnerEx ");
}
static class Inner{
Inner() {
System.out.println( "C'tor the static Inner ");
}
}
public static void main(String argv[]) {
StaticInnerEx out = new StaticInnerEx();
// The following expression gives the impression that Inner lives
// in pkg StaticInnerEx, dosen't it ?
StaticInnerEx.Inner in = new Inner();
}
}
</pre>

Originally posted by Ajith Kallambella:
Static inner classes usually serve as helpers to their enclosing classes. This way of combining the main entity representation ie., your main class with the helper class provides a tighter coupling. This way of marrying related classes together and restricting their scope and existence is a better alternative packaging where you simply classify classes together under one package label.
Am I making sense here??
Ajith


Hi!,
I am quoting from the language (JDK 1.2) api docs
<pre>

"toUpperCase
public String toUpperCase()
Converts all of the characters in this String to upper case using the rules of the default locale,
which is returned by Locale.getDefault.
If no character in this string has a different uppercase version, based on calling the
toUpperCase method defined by Character, then the original string is returned.

</pre>
So in this case ``astr == temp'' is true because your temp
is being set to aStr.toUpperCase() which is aStr.

Tom, I think you are running JDK1.1. I have found that on
JDK 1.1 (at leat on Solaris 2.6 x86) this test is false.
I have not checked the JDK1.1 api docs so I cannot comment on
the correctness of the behavior.
Try testing it on JDK1.2 (if you are not running it already) if
you can and let me know.

Originally posted by jayashree murugan:
I tried it again and it prints "astr == astr.toUpperCase". The mock exam also gives this answer as the right answer. That's where I got the doubt from. The exam does not put the result into temp but directly checks "if (astr == astr.toUpperCase())". I tried it both ways, directly and with a temp variable. In both cases the if evaluates to true.


Check out
(this)
for more info

[This message has been edited by bongadi (edited May 15, 2000).]
[This message has been edited by bongadi (edited May 15, 2000).]
[This message has been edited by bongadi (edited May 15, 2000).]
Hi!,
Thank you Edward for a great analysis.
However, I have tried answering the "size of a primitive"
question with the help of byte code but I could not go too far.
Check this out..
<pre>
class ByteCode {
public static void main (String av[]) {

boolean abool=true;
byteabyte=1;
shortashort=2;
char achar='a';
intanint=3;
/*
longalong=5L;
float afloat=4.0f;
doubleabouble=6.0;
*/
}
}
</pre>
Gets you the byte code (this is a relevant fragment)
<pre>
Method void main(java.lang.String[])
0 iconst_1
1 istore_1
2 iconst_1
3 istore_2
4 iconst_2
5 istore_3
6 bipush 97
8 istore 4
10 iconst_3
11 istore 5
13 return
</pre>
Here the primitives of type boolean, byte, short, integer
and even char are being stored as ints in the
local variable array of the frame.
IMHO it is better to stick with what the specs say. I would
say boolean is of size one bit.
Thoes who are interested in this kind of stuff there is a great
article at Java[tm] bytecode: Understanding bytecode makes you a better programmer
Enjoy
BonGadi

[This message has been edited by bongadi (edited May 14, 2000).]
[This message has been edited by bongadi (edited May 14, 2000).]

up2late , you shouldn't have. . Get some sleep
now.

Originally posted by up2late:
Thanks for the reply, but the
value of -(2^31) is -2147483647.(Check on a calculator)


Thank you thank you thank you Ihor
(1) There are more problems in my code (like
I have used ``\'' escapes where I should
not have.
(2) I found why <code> "STRING".toUpperCase()=="STRING" </code>
is false.
See at work we have to use older JDK (for some compatibility
reasons). I was using java version "1.1.8" this caused the
following...
<pre>
"STRING".toUpperCase()== "STRING":> Not Equal
------------------------------------------------------------------------
"string".toLowerCase()=="string":> Not Equal
------------------------------------------------------------------------
</pre>
After changing my JDK to 1.2 (I have a small script which
switches the links) [ Solaris VM (build Solaris_JDK_1.2.1_03, native threads, sunwjit) ] I got the results that you are getting.
I am still a bit zoned by the discovery though. Will do more
investigation later.

Originally posted by Ihor Strutynskyj:
Bongadi, sorry I misspelled your name.


No problems
Thanks and regards.
BonGadi

[This message has been edited by bongadi (edited May 12, 2000).]
Aaaarrrgghhh....
I got it thanks . I missed greater than \u0020 .
One rant....
(" String ".trim() == "String")
and
("String".trim() == "String")
look quite indistinguishable ... so people please use
proper tags like ..
<code>
(" String ".trim() == "String") </code>

Originally posted by Ajith Kallambella:
Bogandi,
Read it again.
[b]
If this String object represents an empty character sequence, or the first and last characters of character
sequence represented by this String object both have codes greater than \u0020 (the space character),
then a reference to this String object is returned.

If the string you are operating on has no leading/trailing white space character, you get the reference to the original string itself. This is true for other methods ie., calling toUpperCase() on an uppercase string, or calling toLowerCase on a lower case string.
Does this answer your question?

Ajith[/B]


My test program is similar to Maha's co check it out
<pre>
public class AQuestion
{
public static void printline() {
System.out.print("-----------------------------------------------------");
System.out.println("-------------------");
}
public static void main(String args[])
{
System.out.print("\"STRING\""+".toUpperCase()"+"==" +" \"STRING\""+":>");
if("STRING".toUpperCase() == "STRING")
System.out.println("\t\t\t Equal");
else
System.out.println("\t\t\t Not Equal");
printline();
System.out.print("\"string\""+".toLowerCase()" +"==" +"\"string\""+":>");
if("\"string\"".toUpperCase() == "\"string\"")
System.out.println("\t\t\t Equal");
else
System.out.println("\t\t\t Not Equal");
printline();
System.out.print("\"STRING\""+".concat(\"\")"+" == "+"\"STRING\""+":>");
if ( "\"STRING\"".concat("") == "\"STRING\"")
System.out.println("\t\t\t Equal");
else
System.out.println("\t\t\t Not Equal");
printline();
System.out.print("\"STRING\"" + ".replace(\'I\'"+ "," + "\'I\')" + " == "
+ "\"STRING\"" + ":>");
if ( "STRING".replace('I','I') == "STRING")
System.out.println("\t\t\t Equal");
else
System.out.println("\t\t\t Not Equal");
printline();
System.out.print("\"STRING\"" + ".substring(0)"+" == "+"\"STRING\""+":>");
if ( "STRING".substring(0) == "STRING") {
System.out.println("\t\t\t Equal");
} else {
System.out.println("\t\t\t Not Equal");
}
printline();
System.out.print("\"STRING\"" + ".toString() "+ " == \"STRING\""+":>");
if ( "STRING".toString() == "STRING") {
System.out.println("\t\t\t Equal");
} else {
System.out.println("\t\t\t Not Equal");
}
printline();
System.out.print("\"STRING\".toString() " + "== \"STRING\".toString()" +":>");
if ( "STRING".toString() == "STRING".toString()) {
System.out.println("\t\t\t Equal");
} else {
System.out.println("\t\t Not Equal");
}
printline();
System.out.print("\"STRING\".trim() " + "== \"STRING\"" +":>");
if ( "STRING".trim() == "STRING") {
System.out.println("\t\t\t Equal");
} else {
System.out.println("\t\t Not Equal");
}
printline();
}
}
</pre>
OUTPUT
<pre>
"STRING".toUpperCase()== "STRING":> Not Equal
------------------------------------------------------------------------
"string".toLowerCase()=="string":> Not Equal
------------------------------------------------------------------------
"STRING".concat("") == "STRING":> Equal
------------------------------------------------------------------------
"STRING".replace('I','I') == "STRING":> Equal
------------------------------------------------------------------------
"STRING".substring(0) == "STRING":> Equal
------------------------------------------------------------------------
"STRING".toString() == "STRING":> Equal
------------------------------------------------------------------------
"STRING".toString() == "STRING".toString():> Equal
------------------------------------------------------------------------
"STRING".trim() == "STRING":> Equal
------------------------------------------------------------------------

[This message has been edited by bongadi (edited May 12, 2000).]
Hi!,
I will come back to Rahul's question
<pre>
("String".toString() == "String")
</pre>
it is true because ...

20.12.8 public String toString()
A reference to this object (which is, after all, already a String) is returned.

BUT
<pre>
(" String ".trim() == "String")
</pre>
it is false because ...

20.12.37 public String trim()
If this String object represents an empty character sequence, or the first and last characters of character
sequence represented by this String object both have codes greater than \u0020 (the space character),
then a reference to this String object is returned.
Otherwise, if there is no character with a code greater than \u0020 in the string, then a new String
object representing an empty string is created and returned.
Otherwise, let k be the index of the first character in the string whose code is greater than \u0020, and let
m be the index of the last character in the string whose code is greater than \u0020. A new String object
is created, representing the substring of this string that begins with the character at index k and ends with
the character at index m-that is, the result of this.substring(k, m+1).
This method may be used to trim whitespace (�20.5.19) from the beginning and end of a string; in fact, it
trims all ASCII control characters as well.

HOWEVER
<pre>

if("you".trim() == "you")
System.out.println(" true ");
else
System.out.println(" false ");
</pre>
PRINTS ``true''
Why is that

[This message has been edited by bongadi (edited May 12, 2000).]

<pre>
Given a reference called t to to a class which extends Thread, which of the following will cause it to give up cycles
to allow another thread to execute.
1) t.yield();
2) yield()
3) yield(100) //Or some other suitable amount in milliseconds
4) yield(t);
Given answer is 2). I thought 1) should be correct answer (I know, yield() is static method of Thread class, but still
you can access it using reference, right? Correct me if I am wrong. Also the description says yield() is static method of
Object class.)
</pre>
Just how would you go about doing it ?
When yield() is invoked it is invoked on the thread executing
that particular piece of code. So it is
"runningthread".yield() .
If you have an instance of Thread t in your code and
you do t.yield() even then the yield() would be
"runningthread".yield() rather than t.yield()

Originally posted by Sunny:

By the way, any explanation for the yield() and last question?
Thanks,


And you are right. A thread without the lock can run
unsynchronized method "and change the data".

So, all the methods which are modifying and accessing the data in
critical section should be synchronized to prevent
data corruption.

Originally posted by Sushma:
Hi,
Can u pl throw more light on 'locking on the monitor of the object'?
A synchronised method can call a non synchronised method, so if this is true, what if some other thread which could access thsi method changed the data in this and this was called by the synchronized method???
I'm really confused....Pl help me.
Regards,
Sushma
[This message has been edited by Sushma (edited May 11, 2000).]
[This message has been edited by Sushma (edited May 11, 2000).]



[This message has been edited by bongadi (edited May 11, 2000).]
Check the errata of RHE.
I think it is in the errata.

Originally posted by udayan:
Can somebody tell me what is the exact size of the primitive type boolean(in bits)? RHE says that it is 8 bits and Bruce Eckel's TIJ gives it as 1 bit.
Thanks.