Variables in Java

Variables in Java are used to store information that may change through the course of the program, either through computation or user input. There are two main types of variables: primitive and reference. The concepts of declaring a variable (giving it a type and name) and initializing a variable (giving it an initial value) remain the same in java.
When naming variables, programmers should choose names that indicate the value that the variable holds. For example, itemCost is much more descriptive than x. Also, when naming a variable in Java the following rules apply:
Primitive type variables are native to Java and include byte, char, short, int, long, float, double and boolean. Byte, short, int and long are used to store integer values of different ranges. Char type variables store single character entities, such as 'G' or '&'. Float and double type variables store decimal numbers of different ranges. Boolean type variables store a value of true or false. Primitive type variables store the value in the variables memory location. The most commonly used variable types in learning programming are int, double, char and boolean. Below, you will see examples of variable initializations for each of these types: int zip=19462; double weight=1.5; char initial='M'; boolean isMale=true;
Reference type variables are object instances of classes. When declared, they need to be instantiated using the keyword new and the class constructor. Reference type variables store a reference to the objects memory location in the variables memory location.
A new term in Java is intantiation. Instantiating a variable means to call a constructor method (used for reference type variables). origin=new Point(0,0); Scanner reader=new Scanner(System.in); //combining the declaration and instantiation

The middle ground between these two types of variables is the String type. In reality, the String class is a reference type variable, but the creators of Java realized its importance and allow it to often work more like a primitive. For example, a String type variable can be declared and initialized without a constructor call. String name=new String("Jim"); String name="Jim";
When assigning a variable, we use the = assignment operator. The variable should be on the left side with the value on the right. There are other assignment operators such as +=, -=, /=, *=, and %=. An example of use for these operators is that total=total + num is the same as total += num. Variables can be assigned to literals, input results, or computations. Basic computation operators include +, -, *, /, and %. Modulus (%) is an operation that gives the remainder when two values are divided.

Some variables, we want to make sure that their values do not change through the course of the program. These are called constants and are denoted in all caps and use the keyword final in their declaration. An example is: final double SALES_TAX = 06;

Console Output

Basic console output is a simple concept in Java. You have a choice of two methods: System.out.print() and System.out.println(). The only difference is that the println() method attaches a line break to the end of your output. To use these methods, you simply insert a string, calculation, method call or variable inside the parenthesis as a parameter of the method. Examples: System.out.print("Hello"); System.out.println("What is your name?"); System.out.println(firstName); System.out.print(4*8); System.out.println(5.00*salesTax);
To show a string and the value of a variable, use the '+' symbol to concatenate the information together. Either side of the concatenation operator needs to be either a quoted string, variable name, number or calculation. However, since the '+' operator is overloaded in Java, there can be side effects as the order of operations dictates that '+' be completed from left to right. To avoid such issues, be sure to place addition in parenthesis so that it happens prior to any concatenation.

In Java, there are some special characters that we can place inside of quotation marks to produce some special characters. For example, if we wanted to display This is a quotation mark - ". We would have some difficulty with Java because Java would see the second quotation mark and end the string. So, we need to 'escape' the rules temporarily. The way that we escape the rules is using the '\' character. When the computer sees this character, it looks at the next character to complete the code. Below is a list of escape sequences:
Note the need for \\ to show a backslash. This is because if you show '\' in the code, the computer will be looking for an escape sequence.