Emilia Ipate

Greenhorn
+ Follow
since Aug 09, 2005
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 Emilia Ipate

Hello, AnuragSudha!

First of all, I think you wrote wrong your code.
This must have been your intention:


The result is: 11111111111111111111111111100110;

Now, here is the explination:
i = -13, its represantation: 11111111111111111111111111110011
i << 1, means add 1 zero starting with the lowest bit:
11111111111111111111111111100110 (= -26).

By the way, x<<n means x*2^n (x mutiplied with 2 at the power n).
In this case -13<<1 = -13*2^1 = -13*2 = -26.
Yes, you are all right, it does not compile.
I have mistaken when I first entered the reply. SO, I HAVE CORRECTED my previous answer.
Hi Jagan!

I've tested AGAIN the code and I MUST THAT THE FIRST TIME I WAS WRONG (thanks Liviu) and here are my results:

In the first case:

there is a compile error because when getting to the line b * 2, the compiler promotes the byte b to int (because 2 is int). and so b * 2 is an int and it cannot be converted to byte.

In the second case:

there is a compile error at line : c = c * 4 because the int (c * 4 is an int) cannot be converted to a byte number. You must do a cast, so the correct line is : c = (byte) (c * 4).

Do let me know if you find more problems.
[ August 11, 2005: Message edited by: Emilia Ipate ]
Hi Areca!

It's normal not to give a compile error because 100 can be converted to a byte.
Bytes numbers are represented on 8 bits=> means a byte number is between -2^7..2^7 - 1 (x^y means x multiplied with x for y times) = -128..127.
Because 100 can be represented on a byte (-128<100<127) the compiler gives no error and converts the int number 100 to the byte number 100.
If you tried to convert an int number greater than 127 or less than -128 (let's say 144) to a byte number, the compiler will give you an error.


The code example above gives you an error compile.
So, the final modifier has nothing to do with the conversion.

I hope this answer lets you.
Do let me know if you have troubles understanding it.
Techie,

Java specifies that methods may not be overridden to be more private.
For example, if you have a base class A and a derived class B declared like this:


the compiler will give you an error at line 6: "Cannot reduce the visibility of the inherited method from A"
18 years ago
Hi Kayalvizhi,

I must say I definitely agree with Pavel.
The question assumes you have a line �obj = new B()�; where obj is declared as B or A.
So, if obj is of type B, then
  • the test obj instanceof A will always return true (as B extends A),
  • obj instanceof B will return true (obj is of type B),
  • obj instanceof D will return false(B has nothing to do with D)
  • obj instanceof C will return false(as C extends B and obj is of type B)

  • => obj instanceof B && !(obj instance of C)
    The situation you are referring to involves closures = Anonymous Local Classes. Local classes can only reference local variables and parameters that are declared final.
    And here is the reason: An anonymous local class can use local variables because the compiler automatically gives the class a private instance field to hold a copy of each local variable the class uses. The compiler also adds hidden parameters to each constructor to initialize these automatically created private fields. Thus, a local class does not actually access local variables, but merely its own private copies of them. The only way this can work correctly is if the local variables are declared final, so that they are guaranteed not to change. With this guarantee in place, the local class is assured that its internal copies of the variables accurately reflect the actual local variables.

    Hope this helps you.
    18 years ago