|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

06-30-2008, 09:37 AM
|
 |
Senior Member
|
|
Join Date: Mar 2008
Location: Delhi, India
Posts: 187
|
|
|
correct!
long variable cannot be used as array index, only integer is accepted
__________________
i am the future
|
|

06-30-2008, 09:40 AM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Delhi(India)
Posts: 252
|
|
|
Will not compile....i think so
__________________
sanjeev,संजीव
|
|

06-30-2008, 09:42 AM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Delhi(India)
Posts: 252
|
|
One from my side
Will this compile..??if yes...Output
public class TestQue
{
public static void main(String[] args)
{
int j;
for(int i=0;i<14;i++) {
if(i<10) {
j = 2 + i;
}
}
System.out.println("j: " + j + " i: " + i);
}
}
__________________
sanjeev,संजीव
|
|

06-30-2008, 09:42 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 5,075
|
|
Originally Posted by rjuyal
correct!
long variable cannot be used as array index, only integer is accepted
 
I have to use the correct word too. INDEX.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Someone helped you? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post. Help: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Resources: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Web: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Tips: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-30-2008, 09:45 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 5,075
|
|
Originally Posted by sanjeevtarar
One from my side
Will this compile..??if yes...Output
public class TestQue
{
public static void main(String[] args)
{
int j;
for(int i=0;i<14;i++) {
if(i<10) {
j = 2 + i;
}
}
System.out.println("j: " + j + " i: " + i);
}
}
Out of bound  
You have exceed the variable scope. How lucky I'm to find it. You can't use variable 'i' in println(). It's valid only within the for loop.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Someone helped you? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post. Help: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Resources: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Web: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Tips: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-30-2008, 09:50 AM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Delhi(India)
Posts: 252
|
|
|
Very good.....if we remove i from SOP....??
__________________
sanjeev,संजीव
|
|

06-30-2008, 10:15 AM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Delhi(India)
Posts: 252
|
|
Output..??..or..??
public static void main(String args[]) {
int total = 0;
int[] i = new int[3];
for(int j=1;j<= i.length; j++)
total += (i[j] = j);
System.out.println(total);
}
__________________
sanjeev,संजीव
|
|

06-30-2008, 10:25 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 5,075
|
|
<= 
ArrayOutOfBoundException at run time. Compiled is ok, runtime failed. Right?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Someone helped you? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post. Help: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Resources: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Web: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Tips: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-30-2008, 10:34 AM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Delhi(India)
Posts: 252
|
|
|
yes.............
__________________
sanjeev,संजीव
|
|

06-30-2008, 10:52 AM
|
 |
Senior Member
|
|
Join Date: May 2008
Posts: 299
|
|
public class TestQue
{
public static void main(String[] args)
{
int j;
for(int i=0;i<14;i++) {
if(i<10) {
j = 2 + i;
}
}
System.out.println("j: " + j + " i: " + i);
}
}
Very good.....if we remove i from SOP....??
Compilation Error: "j" not initialized correct?
__________________
To finish sooner, take your own time....
Nivedithaaaa
|
|

06-30-2008, 11:03 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 5,075
|
|
|
Yep, that's right. But compile and try, that error get if the scope error is fix.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Someone helped you? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post. Help: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Resources: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Web: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Tips: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

07-21-2008, 01:09 PM
|
 |
Senior Member
|
|
Join Date: Mar 2008
Location: Delhi, India
Posts: 187
|
|
|
once more!!!
xcuse to wake you again..
will it compile, if yes then what will be the output.
--
public class InheritanceTest {
public static void main(String[] args) {
Base base = new Child();
base.fun();
base.fun2();
}
}
class Base{
public void fun(){
System.out.println("Fun in Base");
}
public static void fun2(){
System.out.println("Fun2 in Base");
}
}
class Child extends Base{
public void fun(){
System.out.println("Fun in Child");
}
public static void fun2(){
System.out.println("Fun2 in Child");
}
}
---------
__________________
i am the future
|
|

07-22-2008, 02:08 AM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 7
|
|
|
When the same is different...
If you like Java puzzles, this'll kill you ;-)
What's the output ?
public static void main(String[] args) {
Integer i1a = Integer.valueOf(124);
Integer i1b = Integer.valueOf(124);
Integer i2a = Integer.valueOf(133);
Integer i2b = Integer.valueOf(133);
System.out.println("i1a == i1b : " + (i1a == i1b));
System.out.println("i2a == i2b : " + (i2a == i2b));
}
|
|

07-22-2008, 02:12 AM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 7
|
|
|
rjuyal, static methods belong to the reference type, non-static methods belong to the instance type.
|
|

07-22-2008, 06:40 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 5,075
|
|
Originally Posted by rjuyal
xcuse to wake you again..
will it compile, if yes then what will be the output.
--
public class InheritanceTest {
public static void main(String[] args) {
Base base = new Child();
base.fun();
base.fun2();
}
}
class Base{
public void fun(){
System.out.println("Fun in Base");
}
public static void fun2(){
System.out.println("Fun2 in Base");
}
}
class Child extends Base{
public void fun(){
System.out.println("Fun in Child");
}
public static void fun2(){
System.out.println("Fun2 in Child");
}
}
---------
Nice to see that we are moving with this again after a long time. 
Ok, for me this code segment is compiling and the output is,
Fun in child
Fun2 in base
What you say pal, 
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Someone helped you? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post. Help: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Resources: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Web: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Tips: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

07-22-2008, 06:46 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 5,075
|
|
Originally Posted by dlorde
If you like Java puzzles, this'll kill you ;-)
What's the output ?
public static void main(String[] args) {
Integer i1a = Integer.valueOf(124);
Integer i1b = Integer.valueOf(124);
Integer i2a = Integer.valueOf(133);
Integer i2b = Integer.valueOf(133);
System.out.println("i1a == i1b : " + (i1a == i1b));
System.out.println("i2a == i2b : " + (i2a == i2b));
}
True/false validation with wrapper classes. So the output is,
i1a == i1b : true
i2a == i2b : false
Because you exceed the NUMBER 127 there. Hops you got the point.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Someone helped you? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post. Help: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Resources: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Web: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Tips: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

07-22-2008, 09:37 AM
|
 |
Senior Member
|
|
Join Date: Mar 2008
Location: Delhi, India
Posts: 187
|
|
@Eranga ,
correct answer of my question.
can you elaborate the answer of previous question
Because you exceed the NUMBER 127 there. Hops you got the point.
__________________
i am the future
|
|

07-22-2008, 08:05 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 32
|
|
|
After reading the thread i'm itching to write my own test quiz and test if anyone can solve it =) Anyway, you could also read the book "java puzzlers" it contains many questions and some of the answers might be what you wouldn't expect.
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
<-- To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Java developer To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

07-23-2008, 01:32 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 5,075
|
|
|
Nice code to have a fun to all of us......
Originally Posted by rjuyal
@Eranga ,
correct answer of my question.
can you elaborate the answer of previous question
First of all sorry for the late replay. So here we got a nice thread to discuss. Lets have a fun pal.
At the time, change 124 to 127 and, 133 to 128 and run the code. What you get? Confusing?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Someone helped you? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post. Help: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Resources: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or imag | | |