This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer (Exam 1Z0-830) Java SE 17 Developer (Exam 1Z0-829) Programmer’s Guide and have Khalid Mughal and Vasily Strelnikov on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

doubt over venu's notes

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1] i got this one from venu's notes
public class Precedence {
final public static void main(String args[]) {
int i = 0;
i = i++;
i = i++;
i = i++;
System.out.println(i); // prints 0, since = operator has the lowest precedence.
i thought that precedance worked only for single expressions.
i am neither understanding this explanation nor the answer.
help me please
2] what is the diff between instanceof operator and instanceOf
3] again from venu's notes, where Ajith says when a=b=c=d=0;
d=0 is executed first then c=d (which is now 0) and so on...
but i believe that a=0 will be assigned first ,then b= 0 and so on....
am i correct?
 
Ranch Hand
Posts: 645
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI SHILPA AGAIN IT'S ME
well about u r first dought

when u say
int i=0;
i=i++;
the ++ operator is used as postincrement
which say's " i am not in hurry first do u task & then give me chance"
so in this case i is assigned first & then incrmented
so when it is incremented to 1 there is nothing to do with
that 1 so it is becomes prey for the hungry garbage collector
no again if u say i=i++;
again the old value of i=0 is taken & same thing is
perform
while if u use i=++i;
u will get the desired result;
now about ur 3) dought
well i am sorry to say but in this case u r wrong.
the precidence for '=' operator is from right to left.
try following code for more clear vision
int a[] ={100,200}//so a[0]=100 & a[1]=200
int b=4;
a[b]=b=0;
S.O.P("a[0]="+a[0]);
the o/p for above code is
a[0]=0;
cause first b=0 takes place
then the vale 0 of b is assigne to a[0];
got it???
[email protected]
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Praful
u r correct about right to left evaluation, but the addresses for arrays are calculated before evaluation so, a[4] will cause ArrayIndexOutOfBoundsException.
Please check.

Originally posted by praful thakare:
int a[] ={100,200}//so a[0]=100 & a[1]=200
int b=4;
a[b]=b=0;
S.O.P("a[0]="+a[0]);
the o/p for above code is
a[0]=0;


 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could anybody explain what effect the final modifier in the main method would have on the program!!!
Whilst is't wrong that the signature itself is not in tune with the accepted procedure!!!
Grateful for more information
venkat
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Venkatesan,
The effect of using 'final' when declaring 'main' is the same as for any other method ... it can't be modified by a subclass.
The trick with main() is to remember it is subject to the same rules that apply to all methods except it has the special status of being the conventional starting point of an application.
The standard convention is to declare it as <code>public static void main(String[] args)</code>. However, this is just a convention and there are variants that are legal:

The key points are:

  • it must be named 'main'
  • it must be 'static'
  • it must have a return type of 'void'
  • the return type must immeadiately preceed the method name
  • it must take a String array as an argument

  • Hope that helps.
    ------------------
    Jane Griscti
    Sun Certified Java 2 Programmer
    [This message has been edited by Jane Griscti (edited February 12, 2001).]
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi shilpa & venkat,
go through this:
public class shift{
public static void main(String args[]){
shift s=new shift();
s.cal();
}
public void cal(){
int i=0;
i++;
i++;
System.out.println(i);
}
}
as per u o/p should be "0"
but try this o/p is 2;
this is because at i++ ie i=i+1,
it may be first assignment BUT after assigning it is increasing the value of i to 1, and same for second case giving i=2.
shashank
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic