Advertisement
smeacham

VariableDemo.java

Jan 26th, 2015
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.71 KB | None | 0 0
  1. /**
  2.    This example demonstrates variables and assignments.
  3. */
  4.  
  5. public class VariableDemo
  6. {
  7.    public static void main(String[] args)
  8.    {
  9.       int width = 10; // Declares width and initializes it with 10
  10.       System.out.print("width: ");
  11.       System.out.println(width);
  12.  
  13.       width = 20; // Changes width to 20
  14.       System.out.print("width: ");
  15.       System.out.println(width);
  16.  
  17.       int height = 20;
  18.       width = height + 10; // The right hand side can be an expression
  19.       System.out.print("width: ");
  20.       System.out.println(width);
  21.  
  22.       width = width + 10; // The same variable can occur on both sides
  23.       System.out.print("width: ");
  24.       System.out.println(width);      
  25.    }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement