Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects Meet Objects Creating Classes

Can Some one explain this to me

My question is not about this top I wrote this code and I was expecting the answer to be zero but instead I got six

int t,s=6; t=(8*s++)%7; System.out.println(t);

6 Answers

 int t,s=6;
 t=(8*s++)%7;
 System.out.println(t);
 //   t = (8 * 6) % 7 = 6;
 //   t = (48) % 7 = 6; 

s is incremented after the value was passed to the function calling it;
if you want to increment before, use ++s . And use parenthesis around ++s , just as preliminary (pre/post -increment)

But 8 * 6 = 48

And 48%7 = 6.

ok thanks

One last question.

for(int i=1;i<=5;i++) System.out.println(i);

1 2 3 4 5

should'n this also follow the post increament rule eg: when i = 1 and i++ then i = 1

when i = 2 and i++ then i = 2

It is a post increment. // i is incremented at the end of the loop

i = 1;

1 == i++ // true i = 1

1 == i // false i = 2

The for loop, it is divided into 3 parts. the initialization is done first and once only. int i=1;

then the condition is checked, i<=5; Then the program pointer or next execution is the inside of the loop. At the end of the loop, increment: i++

Can you explain to me what is a constructor and why do we need a constructor,I didn't understand it. Also why do we have to initialise our variable in the contructor and not in the class.Sorry I keep saying this is my last question

classes are plans for building objects, and the constructor construct the object. In the C programming language which a lot of programming languages are based on, you have to manage memory yourself. Oop made a lot of things easier. if not specify, default constructor is used.

Because it keeps all initialization in one place. Look up java best practices