Piyush Sam

Greenhorn
+ Follow
since Jun 02, 2005
Merit badge: grant badges
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 Piyush Sam



now try this code, your dout will be clear, j will print 0 and i will 1

and yor are right Timmy in ferlin() method i is paassed by value so any changes made inside this method will not have any effect outside of this method.

Regards,
[ June 22, 2005: Message edited by: Piyush Sam ]
What will be output of the following program?

System.out.println('A');
System.out.println('A' + 'B');

1 A AB
2 65 131
3 A 131

Anser is A 131

The problem is Java's design blunder of using + to mean both addition and concatenation. Addition also promotes to int, which println displays differently from char.

Plz somebody explain in more detail.


Regards,
Hi Ramu,


-------------------------------------------------------------------------------
Every constructor(argument or no argument) the first line is super().so it calls the no arg constructor of the super class.
-------------------------------------------------------------------------------

I dont think so, bcoz if u explicitly call any of super class's constructor other than the defualt one then its not always the defualt constructer super() gets called, but the one you are calling explictly will get called

consider the following piece of code

class Base {
public Base() {
System.out.println("base default constructor");
}

public Base(int i) {
System.out.println("base constructor:" + i);
}
}

class Derived extends Base {
Derived() {
this((int) 9);
System.out.println("derived default constructor");
}

Derived(int j) {
super(j);
System.out.println("derived constructor" + j);
}

}

public class Q01 {
public static void main(String args[]) {
Derived obj = new Derived();
}

}

O/P of the code is:

base constructor:9
derived constructor9
derived default constructor

Regards,
In your derived class defualt constructor you have this((int) 9),
that will invoke Derived(int j) and since inside this method you are calling super(j) it causes base class integer arugment constructor base(int) to be invoked, so is your o/p

System.out.println("base constructor:" + i);

then your

System.out.println("derived constructor" + j);

and lastly

System.out.println("derived default constructor");

and in your first code u didnt call any super class constructor explictly so base class defualt contructor got called but here you are calling explictly super(int) it will invoke the appropriate base class constructor.

Regards,
[ June 21, 2005: Message edited by: Piyush Sam ]
Hi,

Both your base and derived class have two constructors one defualt and other which takes integer as an argument.

Now have a look at this line
Derived obj = new Derived();


Since you are creating a derived class object without giving any arguments and in your dervied defualt constructor you have not called base class defualt constructor so while intializing your base class, your base class's defualt constructor gets called now you have this line:

this((int) 9);

this refers to the current object and in this program our current object is derived object, which consist of base object + derived object and since we have not called super class's constructor Base(int) explictly so defualt constructor gets called, now base part has been initialized

this((int) 9); statement causes Derived(int j) to be invoked and not the base(int) bcoz "this" will invoke the method of the object being created and in our case Dervied class's ojbect is being created thats why Dervied(int) gets called.

Regards,
Quote
------------------------------------------------------------------------------
When a subclass's constructor calls the super class's constructor...Why it always invokes the default constructor(i.e which takes no arguments.) of super class, Even if there is the constructor of super class with same arg.
of calling constructor ???
------------------------------------------------------------------------------

Whenever you create an object of child class, compiler first try to initialize your parent class's object. If you dont call your parent class constructor explicitly which take some arguments then compiler call the default constructor to initialize the parent object and if you want to initialize your child Object with some special value then you have to create its object by passing the value according to the constructor you have defined in your child class and if you have defined any constructor other than the defualt constructor to inialize your parent class object with some specific state, then you have to call the parents class constructor in your child class constructor, if you dont call any of the parents class constructor in your child class constructor then compile calls the defualt constructor of parents class to initiaze its object.


Regards,
Thanks Barry for such a wonderful explaination.

Regards,
Hello everybody,

What will be the output of the following program:

1 System.out.println("string is: " + "emptiness".substring( 9 ));
2 System.out.println("string is: " + "emptiness".substring( 10 ));

2 should throw java.lang.StringIndexOutOfBoundsException but first one return an empty string,

Can anybody explain this why is it giving this?

Regards,
Hi,
i tried to run the following piece of code in my program, but it turns out to be a infine loop, can anybody throw some light why this is happening.

for ( long i=Long.MAX_VALUE -2; i<=Long.MAX_VALUE; i++ )
{
/* ...*/
}

There are similar problems with Integer.MAX_VALUE, Long.MIN_VALUE and Integer.MIN_VALUE.

Regards,
Refer to this link, this explains in detail what instanceOf is

http://mindprod.com/jgloss/instanceof.html


Regards,
m also not able to find the sane course in http://in.sun.com/education/certification/java/

it seems that they have updated their site, they have this exam in sun(us) site, but its not there in sun(in) site.

Plz provide some info, if anybody has placed an order for this exam from india.


Regards,
Hi,

Both have different objective, appear for the first one if you want to be a java5.0 certified programmer and appear for second if you have already cleared any of scj1.1, scj1.2 or sj1.4 exam and want to be a java5.0 certified programmer.

So it upto you to decide what exactly you want.

Regards,
Neither Interface or Abstract classes can be instantiated. An interface has all public members and no implementation.

Abstract Classes:
Abstract classes are used as a basis for making other concrete objects.
Abstract class is more used for polymorphism,and the upcast/downcast is used very often.
An abstract class has the usual flavors of class members (private, protected, etc.), but has some abstract methods.
These are used when you have to provide, un-instantiable classes, with some methods carriying method bodies too.
These are to be used when some classes in an application should conform to certain design for better maintainability of
the application.


Interfaces:
An interface is only a contract to flag up what methods are provided by a class, these are used for providing
plug-n-play, components in an application, i.e. to implement Component Based Technique.
But interface is more used for multi-inheritance and very method you have inherited must be rewritton.

Use interface as much as possible and only use an abstract class in the case where you want to provide some
(but not all, of course) implementation. In practice, you want to prefer using, passing and returning interfaces,
not abstract class references. I think even in the case where you have an abstract class to share some implementation,
you still want to have an interface represent what will be used by the client (for an example look at the
new Collections -- you often pass around Collection, List, Set or Map references, but not a AbstractList, AbstractSet
or AbstractMap, references).

Also refer to this article

http://mindprod.com/jgloss/interfacevsabstract.html


Use singleton when you want to have only one object of your class.

Being happy doesn't mean everything's perfect. It means you've decided to see beyond the imperfections.

Regards,
Hi friends,

I modified the above program and introduce one more method which takes StringBugffer as an agrument and one method call which passes null, m getting compile time error that "The method a(Object) is ambiguous for the type Test"

I can understand that this is bcoz compile is not able to decide which method to call but if comment the method which takes StringBugffer as an agrument then it doest give any error, guys plz help me out to clear this

public class Test {
public void a(Object o) {
System.out.println("object");
}

public void a(String s) {
System.out.println(s);
}

public void a(StringBuffer buffer) {
System.out.println(buffer.toString());
}

public static void main(String args[]) {
String testStr = null;
new Test().a(testStr);
new Test().a(null); // compile time error
new Test().a(new Test());
}
}


Being happy doesn't mean everything's perfect. It means you've decided to see beyond the imperfections.

Regards,
Hi,

Try this

Computation of expression n>>s

For nonnegative values of n, this is equivalent to truncating integer division, as computed by the integer division operator /, by two to the power s,

and for negative values of n, this is equivalent to truncating integer division, as computed by the integer division operator /, by two to the power s and -1,

for example -10>>2 = -3 is compuatated as [ (-10 / (2 * 2) ) - 1 ] = -3
and 10>>2 = 2 is computated as [ 10 / (2 * 2) ] = 2

and Computation of expression n>>>s

The value of n>>>s is n right-shifted s bit positions with zero-extension. If n is positive, then the result is the same as that of n>>s; if n is negative, the result is equal to that of the expression (n>>s)+(2<<~s) if the type of the left-hand operand is int, and to the result of the expression (n>>s)+(2L<<~s) if the type of the left-hand operand is long. The added term (2<<~s) or (2L<<~s) cancels out the propagated sign bit. (Note that, because of the implicit masking of the right-hand operand of a shift operator, ~s as a shift distance is equivalent to 31-s when shifting an int value and to 63-s when shifting a long value.)

Regards,