navneet shrivastava

Ranch Hand
+ Follow
since Jul 09, 2004
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 navneet shrivastava

=======================================================================
"waiting and notifying are means of communication between threads
that synch on the same object. These methods can only be executed
on an object whose lock the thread holds, otherwise it will result
in an IllegalMonitorStateException"
=======================================================================

Thr r two threads threadA & threadB, holding lock on the same object 'obj'.
Assume at this instant threadA is excuting obj, as soon it is done/ or its time slice completes; It need to intimate threadB to PROCEED with obj, for this threadA calls notify() on obj, in turn obj wll intimate its threads to proceed with further execution.

A thread not concerned with an object (ie not holding a lock on it), cant command the object for intimation. If this happens an 'IllegalMonitorStateException' is thrown.

" i cnt call my friends(sm othr thread) gal(sm other object) to do good things (to perform sm action) bcoz the THREAD is with my friend(the othr thread). "

HOPE THIS MAKE SENSE.



* "Strings are immutable objects" meaning that everytime u modify a String object a new Object is created in VM(i guess no longer in pool), and the ols String ref. will be set t this new object.

at line 1 s1 refers to Object obj1(say), in pool.
at line 1 after calling toUpperCase s1 refers to new Object obj2(say); remember Strings are immutable.

by now s1,s2 are no longer referening to same objects in pool/VM.
BUT s2 remains unmodified in pool and same goes for s.
Thus s==s2 returns TRUE
===========================================
1. String s1 = "hI".toUpperCase(); // line 1
2. String s2 = "HI".toUpperCase(); // line 2
3. String s = "HI"; // line 3

4. System.out.println(s == s1); // Prints False
5. System.out.println(s == s2); // Prints True

Can some body please explain why line 4 is printing false and
line 5 is printing true.
===========================================
///////////////////////////////////////////////
code:
----------------------------------------------------------------------------

Object obj = new Object(); line1
StackImpl stack = new StackImpl(); line2
obj = stack; // no compilation error because sub-type assignment to super-type is valid. line3
System.out.println(obj.getClass()); line4

----------------------------------------------------------------------------


I was expecting the o/p to be java.lang.Object but printed out class StackImpl.

In the assign statement how is the destination class representing obj (java.lang.Object) being modified to the source class(StackImpl)?

///////////////////////////////////////////////

from line3 onwards object 'obj' will point to the assigned 'StackImpl' object 'stack'.
In line4 "obj.getClass()" calls the method on 'stack' object (ie of type 'StackImpl'). so as output system gives classtype as 'StackImpl'

thanks,
Nav
---
i would select an answer which says behaviour is not guaranteed
---

adding to it: See we should make it clear SUN is going to conduct the exam to check ur proficiency for their JVM.

hope it makes sense
Thanks,
CODE BLOCK 1:
==========================================
public class Tux extends Thread{

static String sName = "Techno";

public static void main(String argv[]){
Tux t = new Tux();
t.name(sName);
System.out.println(sName);
}

public void name(String sName){
sName = sName + " park";
start();
}

public void run(){

for(int i=0;i < 4; i++){
sName = sName + " " + i;
}
}
}

==========================================

CODE BLOCK 2:
==========================================
public class Tux extends Thread{

static String sName = "Techno";

public static void main(String argv[]){
Tux t = new Tux();

// by now JVM will have 2 Threads running(as far as execution of Tux is
// concerned ), for 'main' & 't' respectively,
// as we know Thread by default have '5' as its Priority.
// now both thread has same priority (as 5), 't' is child of main and is
// dependent on 'main'. So as soon as 'main' exist ( with 't' still not
// having any higher prority, higher than 5) will kill 't' as the
// consequence.

// any priority higher than 5 for thread 't' will increase its probability
// to execute before 'main'

t.setPriority(6) ; // <inserted statement>
t.name(sName);
System.out.println(sName);
}

public void name(String sName){
sName = sName + " park";
start();
}

public void run(){

for(int i=0;i < 4; i++){
sName = sName + " " + i;
}
}
}
==========================================

hope comments makes the code self explanatory.

thanks,
[ January 27, 2005: Message edited by: navneet shrivastava ]
hi,

nice question... we can call it a issue rather.
still hunting for explanation.

thanks,
=====
This compiler check is needed to avoid issues during runtime. In the
following example, if the child class method, getFile() is allowed to
throw a more broader IOException, then during runtime: the catch block in
MyTestClass could not handle that exception. The catch block could handle
only either FileNotFoundException or its children exceptions, if any.
=====
One of the reason being, in Java Exceptions are OBJECTS; thus the rule of
overloading & reference assignment is implemented for exceptions.

The code will work fine for overloaded methods; by overloading a method
we create a new identity(method) itself.

hope 'am clear

thanks
[ January 27, 2005: Message edited by: navneet shrivastava ]
Nicholas is straightforwardly right.
intValue() & charValue() returns primitive datatype.

thanks
class F
^
|
|
class G
^
|
|
class H

code:
====
F[] f= new F[2];
G[] g = new G[7];

g=(G[])f;
====

rules for casting:
@compile time : compiler checks that cast type and object to be casted are in same (inheritance) hierarchy

@runtime : JRE checks for downcasting and restricts it at runtime and throws ClassCastException.

thanks,
nav
question is very straighforward.
We shud avoid making any assumption is such Qs.

so i go with L. Keshav

thanks,
nav
hi all,

A creative problem solver who takes pride in getting the job done correctly and on time. Has 2 years of significant experience in design, development, integration and testing of distributed enterprise application with Java, J2EE, SQL, XML.
A Sun Certified Java Programmer with excellent skills in Object Oriented Analysis and Design. Domain areas on which I expertise includes CRM, Banking Audit System, Stock Management System, Credit Management System.

I am looking for a freelance project on Java/J2EE platform.

contact : n_shrivastava2001@yahoo.com
hope to grab one soon.


thanks,
nav / India
15 years ago
one more thanks to mike for such a simple & straight description
--------------------

"Access is now allowed because class B extends A and i is protected."

In what way do you disagree?
--------------------

By the defination of "protected" , a protected memeber is accessible to a subclass in the same/different package.

please correct me if u think am wrong
hi Ranchers,

one good link: http://www.4tests.com/exams/examdetail.asp?eid=61

sm thngs to remember its from a Certification gp ... we jotted down all these during our preparation; .....hope u all will find it helpful:

JavaTechnicals 7/6/2004 4:34 AM
hi All,
i think it wud be better from my side to create a new thread for Technical discussion/dbouts on Java(a no for JCP).
happy Javing edit

Navneet Java Basics/Tips 7/6/2004 6:13 AM
1. all FLOATING pt var are assumed to be Double
2. all ARRAY elements are ALWAYS initialized with default values, whether its class,
reference, or local var.
3. we cannaot convert a CHAR to SHORT implicitly/automatically.
although they are of same size, but since chars are unsigned it may hold a higher
+ve value than a SHORT can hold.
4. Floating pt./ Double numbers dont give divide by ZERO exception on compile/run time, on being divided by Zero.
INSTEAD they give NaN(Not a Number) as result.

===Navneet edit
delete

Ravinder Some More Basic Tips 7/6/2004 7:25 AM
1. Private member of super class can not be overridden in subclass, but it can br redeclared/redefined in subclass. It aslo means that polymorphism does not apply.

2. The result of an airthmatic is always at least 'int'.

3. The == operator look ONLY for a match of bits. For two different objects, the only way == can be true is if both the reference variables point to same object.

4. You cannot compare a boolean to an integer in 'JAVA' but in 'C' it is possible.

5. Class defined within an parameter to a method is 'anonymous'.
delete

Navneet SwitchCase 7/7/2004 3:31 AM
in SwitchCase, statement execution continues from the line
where matching case is found.
Execution continues for all cases untill & unless a break is encounterd. edit
delete

Ravinder Tips 7/12/2004 3:33 AM
Bitwise
When you shift a bit by a numeric value greater than the size in bits, Java does a modulas shift.

For Example:-

result = 5 << 9 ;

Java shift this by 9%8 or 1 i.e now result = 5 << 1.

boolean
The language specification does not state the actual size of the boolean. It take one bit to represent the two values, but the JVM can use whatever size it wants to store the value.
delete

Ravinder 7/20/2004 1:18 AM
java.lang.Math
java.lang.Math is a final class and thus cannot be subclassed. Hence following declartion is invalid.

public class abc extends java.lang.Math {}

Garbage Collection
The developer cannnot force garbage collection by calling System.gc();
delete

Navneet JavaTechnicals 8/14/2004 8:33 AM
1) >>> always give a positive output
2) we can have a private constructor
3) we can have a any no. of top level classes within a source file, BUT only one can be declared as PUBLIC.
4) default constructor is NOT aways public. It is public if class is also public else it is given a default constr.
5) OVERLOADED methods are allowed to change the modifier as these are not being taken into account while checked for signatures.
6) PROTECTED access modifier is only for member variables, not for class.
7) Math.round() always returns a int value
8) An ABSTRACT class can have a constr. Since it cant be instantiated we cant make a direct call to the same.
However it will be called implicitly by constr of the class extending it
9) >> always give a negative output
10) I feel rest shud be left for others... cm on dropin sm tips... knowledge shared is growth edit
delete

Ravinder Some important facts about Inner classes 8/27/2004 4:04 AM
TOP-LEVEL Class/Normal Class

1. Top-Level class can be declared as abstract, final and public. Private, protected and static are prohibited.

2. A source code can have more than one top-level class, but only have a single 'public' top-level class.

3. An abstract class can have a constructor. It's implementation should be provided to it.

NESTED TOP-LEVEL Classes/Interface

1. These class/interfaces are defined inside a Top-Level class.

2. Are always defined with 'static' access keyword.

3. As they are static, hence can be accessed and intiated without any instance of the top-level class(enclosing class).

4. Since declared static, it can only access static members of the top-level class.

5. Nested top level class cannot have the same name as top-level class.

6. There is no such thing as Inner interface. As the interfaces are 'static' hence are always top-level one.

INNER Classes

1. An Inner class is always defined inside a outer class.

2. These classes are always instantiated in context with outer class i.e requires an instance of outer class.

3. Inner classes can have public, protected, public and no access specifer, but it cannot have 'static' modifier.
4. These classes can extend or implement any class or interface.

5. Member classes(inner or non-static classes) require an instance of enclosing class and every instance of member class holds an implicit reference of enclosing class.

6. Member classes cannot have static members unless it has compile time constant.

7. Compile Time constant - defined as 'static final'. Ex: static final int x = 10;

8. Member class can be declared as public, protected, private, abstract, final or static.

9. Member class can access all the members,including private, of its enclosing class.

Continue....
delete

Ravinder 8/27/2004 4:05 AM
LOCAL Classes

1. A local class can be defined inside a method, constructor, static initializer and instance initializer.

2. A local class cannot have a static modifier.

3. You cannot use public, protected, private and static inside local class.

4. A local class can access all the variable and methods from its enclosing class, but can only access 'final' modifiers declared inside a method.

5. Local class cannot be access from outside the method.

6. A local class cannot have static modifier, but can be declared inside a static context i.e static method.
delete

Navneet Mistakes I made 9/8/2004 1:50 AM
hi all .. i jot down few mistakes i made during my preparation for JCP... will add more to it. hope u all may find it helpful

==== Mistakes I Made ----

volatile is a keywrd in JAVA
Q was askeds for BYTES not BITS; 64bits = 8 bytes
asked for correct options NOT incorrect
Read Q/ options carefully
This way we can overload the method// during method over-load/rid-ing, we can change the args list... it is exactly OVERLOADING
Brush Up with Threads, and note down what is OS dependent behaviours
Math.round() returns int & long ; int for int/float & long for long/double
The method size() returns the number of characters.
Please note that strictfp is a new keyword in Java 2

Best of luck & hardship to all aspirants!
Thanks
nav
oOPS! i dont agree on this ..or may be its incomplete :

"Access is now allowed because class B extends A and i is protected."