• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

++ operator in java

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi All,

I am new to java and preparing for SCJP. I have a small doubt aboout ++ operator in java.

Is it behave same as C? Or different?


class C
{
public int f1(int i)
{
System.out.println(i + "in f\n");
return 0;
}
public static void main (String[] args)
{
C obj = new C();
int i = 0;

i = i++ + obj.(i);

System.out.println("in main() "+i);
}}

Above program gives different output in C and Java.

Can you tell me why?

Thanks In Advance.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java does not work exactly the same as the C programming language, so don't count on it that the result in Java will be the same as in C.
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In C it is undefined what exactly ++ operator does in some situations (when exactly the increment occurs).
So the C program can have different output using different C compilers (or on different platforms)!

Fortunately Java has no such nasty surprises (except bugs in JVM, compiler and your code).
 
vaidehi Agate-keskar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks A Lot!
 
I'm full of tinier men! And a tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic