From CS160

Main: JavaExpressions

// ******************************************************************
// Expressions.java
// Author: Asa Ben-Hur
// Examples of using expressions, variables and arithmetic operators
// ******************************************************************

public class Expressions {
    public static void main(String[] args) {
	System.out.println("*********************************");
	System.out.println("print the number 42");
	System.out.println(42);
	System.out.println("you can print expressions as well");
	System.out.println("Here's what happens when you print 3 + 5 * 7");
	System.out.println(3 + 5 * 7);
	System.out.println("*********************************");

	// Integer operations
	System.out.println("14 / 4");
	System.out.println(14 / 4);	
	System.out.println("14 % 4");
	System.out.println(14 % 4);

	//floating point numbers
	System.out.println("*********************************");
	System.out.println("floating point numbers have limited precision");
	System.out.println("0.1 + 0.2");
	System.out.println(0.1 + 0.2);
	System.out.println("1.0/5 + 1.0/5 + 1.0/5 - 0.6");
	System.out.println(1.0/5 + 1.0/5 + 1.0/5 - 0.6);

	System.out.println("*********************************");
	System.out.println("casting");
	System.out.println("compare the result of 19 / 5 with (double) 19 / 5");
	System.out.println(19 / 5);
	System.out.println((double) 19 / 5);
	System.out.println("Rounding using casting");	
	int i = (int) (10.3 * 2);
	System.out.println("int i = (int) (10.3 * 2);");
	System.out.println(i);

	System.out.println("*********************************");
	System.out.println("declarations and operators");
	int x = 3;
	System.out.println("declartion/initialization:   int x = 3;");
	System.out.println("we can check the value of x using println(x)");	
	System.out.println(x);
	System.out.println("adding to the value of x using the += operator:  x = x + 3;");
	x = x + 3;
	System.out.println(x);
	System.out.println("adding to the value of x using the += operator:  x += 3;");
	x += 3;
	System.out.println(x);
	System.out.println("shortcut when you just want to add 1:  the ++ operator");
	x++;
	System.out.println(x);

	System.out.println("*********************************");
	System.out.println("Java does not complain when integer values overflow your int variable");
	int big = 100000 * 100000;
	System.out.println("int big = 10000 * 10000;");
	System.out.println(big);
    }
}
Retrieved from http://www.cs.colostate.edu/~asa/courses/cs160/fall08/pmwiki/pmwiki.php/JavaExpressions
Page last modified on September 03, 2008, at 02:25 PM MST