Naveen Kumar G

Greenhorn
+ Follow
since Jul 14, 2008
Merit badge: grant badges
For More
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 Naveen Kumar G

thanks all ...

all the best for your SCJP

A detailed explanation on type conversion & numeric promotions are given in the book written by Khalid Mughal.. I am not sure whether it is in K&B..

regards,
naveen

"It's the attitude and not the aptitude that determines the altitude!"
hi saurabh,
I will give you some more examples where the compiler will show error.
Program 1 :
----------

public class SCJP{

public static void main(String[] args) {
final int x = 10;
short y = x ;

System.out.print(x);
System.out.println(y);
}
}



This is a case of implicit narrowing conversion...Generally, narrowing conversion requires explicit casting. But narrowing conversion can also be done implicitly provided the following 3 conditions are satisfied :
1. The source (in this case, it is x) is a constant expression of either byte, short, char, or int type

2. The destination type (in this case, it is y) is either byte, short, or char type

3. The value of the source is determined to be in the range of the destination type at compile time

Coming back to program 1 : First look at the statement
Here you are assigning a integer value to a short variable. So, this is narrowing conversion. Now, you need to find out whether you need an explicit casting or not.. here, final variable x is assigned a value of 10 during the declaration stage itself..so, x becomes a compile time constant & thus satisfying condition # 3. As you can see, conditions 1 & 2 are already satisfied.. thus it becomes implicit narrowing conversion..

Program 2 :
-----------


public class SCJP{

public static void main(String[] args) {
final int x;
x = 10;
short y = x ;

System.out.print(x);
System.out.println(y);
}
}



In this case, you are NOT assigning the value of 10 to the final variable x during the declaration stage itself.. so, now, the value of x can be changed elsewhere in the program..in other words, you can say that the compiler will treat x as just another normal variable.. so, x is not a COMPILE TIME CONSTANT anymore and thus not satisfying condition # 3..
Because of this reason, you need to explicitly cast the narrowing conversion in order to make program # 2 work..
i.e. short y = (short) x ;

Hope this can solve your doubt..
Do let me know if you have any queries, I will try to explain it in a better way..

regards,
naveen
" It's the attitude and not the aptitude that determines the altitude"
SCJP 1.6 is not available yet in India...I have checked it with lot of book stores(in Chennai)..

I think it's better going in for SCJP 1.5 book rather than waiting for SCJP 1.6..
Hi Balakrishnan,

As far as I know, you will not be able to compile a code which has something like this( irrespective of final or non-final specifiers.)
int a;
a=10;



I dont think there is any problem with this piece of code. It will be treated just like a normal variable. It wont result in a compiler error.

As for as I know, only when we try to implicitly do a narrowing conversion on a variable(or anything which is NOT a compile time constant), it will result in a compiler error.

Please let me know if i'm wrong..

Ralph Juas,
Your explanation was really nice.. thanks for that..
[ July 21, 2008: Message edited by: Naveen Kumar G ]

with constructors you can use private.
It is used for generally Singleton classes. This class should have
a static class which return the object of that class.


Madhukar,
Thanks for your explanation. It was very helpful..

Sagar,
okay, thanks for your reply..
all, thanks a lot for your reply...


Raphael, sorry to disturb you again.. I understood the point that an implicit call to the base class constructor will be made by the sub class contructor during the compile time (using super()).


code:
--------------------------------------------------------------------------------

// BLBeck constructorBLBeck() { super(); // this line will be add implicitly.System.out.print("BLBeck");} // Vict consctructorprivate Vict() { super(); // Here we also have a implicit call to the super class constructor, Object's one.System.out.print("Vict");}

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

But what is the purpose behind one more implicit call to the super class constructor, using super(), inside the base class constructor Vict() also?
In the comments, you have mentioned "...,Object's one".
Does it mean that this call is made by the compiler in order to initialize the instance variables,if any, of the Vict class?


Also, regarding the modified code provided by Milan, will the compiler implicitly insert the code "super();" in both the constructors of the Vict class?

Again, I thank everyone for answering my post with clear explanations!!!

regards,
naveen

"Its the attitude and not the aptitude that determines the altitude!"
hi,
this is a question that i came across in a mock test (source: www.jchq.net)..i am not able to understand why this code results in a compiler error..it states that since the constructor of the class Vict() is marked private, that couldnt be instantiated from outside classes. But then,I couldnt see any instantiation of class Vict in any outside classes..
according to me the answer should be "BLBeck".. Can anyone please advice and also let me know if my understanding is wrong?






Incorrect options are shown by strikethrough, any options you chose are marked by a tick
1 Compilation and output of "BLBeck"
2 Compilation and output of "BLBeckVict"
3 Compilation and output of "VictBLBeck"
4 Compile time error
You selected 1 option
The Correct Answer is
4) Compile time error
Note that the constructor for Vict is marked as private. A constructor can be marked as private, however if this is done an instance cannot be created from outside of that class via that constructor

Thanks in advance,
naveen

"Its the attitude and not the aptitude that determines the altitude!"

[Jesper Young: Fixed code tags]
[ July 14, 2008: Message edited by: Jesper Young ]
hi all,
can anyone tell whether this new book "SCJP 6" by K&B is out in the indian market?

thanks in adv,
naveen



"Its the attitude and not the aptitude that determines the altitude!"
[ July 14, 2008: Message edited by: Naveen Kumar G ]