
public class HelloWorld { // class

	public static int value = 89;

	public static void main(String[] args) { // method

		// single-line comment
		// this line will display Hello World!
		System.out.println("Hello World!");

		/*
		 * this is a multiple-line commen if you have a long descriptions, use this
		 * comment
		 */
		System.out.println("Welcome to Java Programming.");

		/**
		 * this is javadoc
		 */

		// declare primitive data types variables
		int num = 8; // default type
		System.out.println(num);

		long bigNum = 9L;
		System.out.println(bigNum);

		double unitPrice = 19.99; // default type
		float totalPrice = 29.99F;
		System.out.println(unitPrice);
		System.out.println(totalPrice);

		// use + concatenator operator
		System.out.println("Unit Price: " + unitPrice);

		char color = 'G'; // single-quote to define char
		System.out.println(color);

		boolean flag = true;
		System.out.println(flag);

		System.out.println("Value: " + value);

		test(); // call the method

		// variable's values can be updated from time to time
		// constant variables are the one that cannot be updated
//		final int age = 9;
//		age = 10;
//		System.out.println("age: " + age);

		// use casting
		totalPrice = (float) (unitPrice * 2);
		System.out.println("totalPrice: " + totalPrice);

		int a = 9;
		long b = a; // ok
		a = (int) b; // casting the ok

		float c = 9.8f;
		double d = c; // ok
		c = (float) d; // casting the ok

		// get user input
		String input = "12345a";

		// try to convert to integer
		try {
			int intInput = Integer.parseInt(input);
			System.out.println("intInput: " + intInput);
			
		} catch (NumberFormatException e) {
			System.out.println("Error converting input: " + e.getMessage());
		}
		
		int x = 5;
		int y = ++x;
		System.out.println("x = " + x + " and y = " + y);
	}

	// define another method that try to access variables defined in main
	public static void test() {
//		System.out.println("Unit Price: " + unitPrice);
		System.out.println("Value: " + value);

		// increment
		value++; // value = value + 1
		System.out.println("Value: " + value);

		// decrement
		value--; // value = value - 1
		System.out.println("Value: " + value);

		value = value + 100;
		System.out.println("Value: " + value); // 189

		// alternatively
		value += 100;
		System.out.println("Value: " + value); // 289

		// pre-increment and post-increment
		int result = value++; // post-increment: assign first then increment
		System.out.println("Value: " + value);
		System.out.println("Result: " + result);

		result = ++value; // pre-increment: increment first then assign
		System.out.println("Value: " + value);
		System.out.println("Result: " + result);

	}

}
