Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Expressions!

 
Surya B
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you think gets printed out(Don't compile and run :-))
1)
class Test
{
public static void main(String[] args)
{
int i=3;
print3(i,++i,++i + ++i);
}
static void print3(int a, int b, int c)
{
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
2)
class Test
{
public static void main(String[] args)
{
int i=3;
print3(i,++i,i+++i++);
}
static void print3(int a, int b, int c)
{
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
 
Anil Kollur
Ranch Hand
Posts: 101
Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! Surya,
I didn't compiled code but IMHO
1) 3,4,11
2)3,4,8
will be printed, correct me if I am wrong.
Anil.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Surya B:
What do you think gets printed out(Don't compile and run :-))
1)
class Test
{
public static void main(String[] args)
{
int i=3;
print3(i,++i,++i + ++i);
}
static void print3(int a, int b, int c)
{
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
2)
class Test
{
public static void main(String[] args)
{
int i=3;
print3(i,++i,i+++i++);
}
static void print3(int a, int b, int c)
{
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}

Answer of (1) is 3,4,11 and for (2) is 3,4,9
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer of (1) is 3,4,11 and for (2) is 3,4,9
 
Surya B
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All
Let me be frank,i was thoroughly confused with the question myself,i was goint through JLS and it had one such example,i just tweaked it a little bit to include here,the answers as you already have given is 3,4,11 for the first and 3,4,9 for the second..by the way one more funny question(atleast according to me)..what do u think is the answer to this silly and easy question???
class Test
{
public static void main(String[] args)
{
long l=3;
long [] a=new long[l];
System.out.println(a.length);
}
}
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it 1 ?
 
Surya B
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Why do you think its one,i think you have considered variable 'l(el)' as 1(one)..it is alphabetl(el).What do you think is the answer now???
Surya
 
Uvnik Gupta
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This should give a compiler error because the array size specifier should be an int.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1 is the length of the array. The question tries to confuse with the length of a long primitive.
 
Surya B
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi vasansrini
Uvnik Gupta answer is correct,from JLS
The type of each dimension expression DimExpr must be an integral type, or a compile-time error occurs. Each expression undergoes unary numeric promotion.The promoted type must be int, or a compile-time error occurs; this means, specifically, that the type of a dimension expression must not be long.
So you can say
class Test
{
public static void main(String[] args)
{
int l=3; or char l=3 or short l=3 or byte l=3-
--IS OK
int [] a=new int[l];
System.out.println(a.length);
}
}
But you cannot have float,long or double.
Surya
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Uvnik Gupta is right. ( i jumped to a conclusion too quick)
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what does it print and why ?
public class Test {
public static void main(String[] args) {
int i=0;
i=i++;
System.out.println(i);
}
}
 
mita
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it will print 0.As i is post incremented.
Please someone explain me why the answer to the 2nd. code is 9 not 8.
Thanks
mita
[This message has been edited by mita (edited July 27, 2000).]
 
hemant gome
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mita,
int i=3;
print3(i,++i,i+++i++);
Java evaluates expressions Left -> Right.
i = 3;
++i = 4;
a = i++ = 4;
i=5;
b = i++ = 5;
i=6;
so a + b = 9;
got it ??
Hemant.
 
Harry Chawla
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Surya and Uvnik
Thanx for clearing the concept of DimExp.
Surya thanx for the questions and your explanation on that was quiet good.
Plese keep the good work going. Thanks.
[This message has been edited by Harry Chawla (edited July 29, 2000).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic