Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes Just a Basic Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Just a Basic Question" Watch "Just a Basic Question" New topic

Just a Basic Question

Ryan Medrano

Joined: Apr 29, 2007
Posts: 11
Hello everyone... I've just started java programing a couple days ago and i really love it so far... but i just want to ask somebody a real quick and easy question just to see if im tracking... My question has to do with the "while" loop command i made small app called Loopy and this is what the code is.. its real small

public class Loopy {
public static void main (String[] args) {
int x = 1;
System.out.println("Before the loop");
while (x < 4) {
System.out.println("In the Loop");
System.out.println("Value of x is " + x); //--THIS IS WERE MY QUESTION IS
x = x +1;

Ok i hope you see were my question is... ok so far x is 1 and the loop will run because 1>4.. got it.. now.. the part that i need to know if im tracking is were it says "value of x(meaning=1) is "(what does this mean?does is mean its recalling the value of x?) + x)

at the end does it mean all together that x + x then? which would be 2 right?

then on the next command

x = x +1;

ok so that first x on the line is going to be the 'new' value of x right?
and the secound x is going to be the current value which is 2 so its 2 + 1 new value = 3?

so i guess the loop ends because of the } that stops the loop right?
because the while loop ran because of the boolean test was true to begin with... but the loop can stop anytime with the '}' curly brace?

please comment on this...
marc weber

Joined: Aug 31, 2004
Posts: 11343

In Java, the + operator is overloaded to concatenate Strings. The expression "Value of x is " + x is a String literal (the part inside the quotation marks) being concatenated with the value of x as a String. This does not change the value of x at all.

(Behind the scenes, the primitive int value is first converted to an Integer object, which is then converted to type String, which is then contatented to produce a new String. See JLS - 15.18.1 and

The line x = x + 1 is a simple assignment. The expression on the right side of the equals sign is evaluated, and the result is assigned to the variable on the left side. So if x is 1, then the right side evaluates to 1 + 1 = 2, and this result is assigned back to x.

Within a while loop, the test is performed only at the "top" (beginning) of the loop. If the condition is true, then the body of the loop executes. The condition might change to false while the body is executing, but the while loop won't "know" that until it tests again before the next iteration.
[ May 03, 2007: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
I agree. Here's the link:
subject: Just a Basic Question
It's not a secret anymore!