JAVA
Variables and Data types
Variables and Data Types  

Variables are the nouns of a programming language-that is, they are the entities (values and data) that act or are acted upon. The countChars method defines two variables-- count and in. The program increments count each time it reads a character from the other variable in. The declarations for both variables appear in bold in the following listing: 

      import java.io.*; 
      public class Count { 
          public static void countChars(Reader in) throws IOException 
          { 
              int count = 0

A variable declaration always contains two components: the type of the variable and its name. The location of the variable declaration, that is, where the declaration appears in relation to other code elements, determines its scope. 

Data Types 

All variables in the Java language must have a data type. A variable's data 
type determines the values that the variable can contain and the operations 
that can be performed on it. For example, the declaration int count 
declares that count is an integer (int). Integers can contain only whole 
number values (both positive and negative), and you can use the standard 
arithmetic operators (+, -, *, and /) on integers to perform the standard 
arithmetic operations (addition, subtraction, multiplication, and division, 
respectively). 

There are two major categories of data types in the Java language: 
primitive and reference. The following table lists, by keyword, all of the 
primitive data types supported by Java, their sizees and formats, and a brief 
description of each. 
 

Type
Size/Format
Description
byte
8-bit
Byte-length
short
16-bit
Short Integer
int
32-bit
Integer
long
64-bit
Long Integer
float
32-bit Decimal
Single-precision
floating point
double
64-bit Decimal
Double-precision
floating point
char
16-bit Unicode Character
A single character
boolean
true or false
boolean value
 
      Purity Tip: In other languages, the format and size of primitive data types may depend on the platform on which a program is running. In contrast, the Java language specifies the size and format of its primitive data types. Hence, you don't have to worry about system-dependencies. 
 

The countChars method uses one variable of reference type, in, which is a Reader object. When used in a statement or expression, the name in evaluates to a reference to the object. So you can use the object's name to access its member variables or call its methods (just as countChars does to call read). 

      Note to C and C++ Programmers: There are three C Data Types Not 
      Supported By the Java Language. They are pointer, struct, and union. 
      These data types are not necessary in Java; you use classes and objects 
      instead. 

Variable Initialization 

 Local variables and member variables can be initialized with an assignment 
 statement when they're declared. The data type of both sides of the 
 assignment statement must match. The countChars method provides an 
 initial value of zero for count when declaring it: 

         int count = 0

The value assigned to the variable must match the variable's type. 

Method parameters and exception-handler parameters cannot be initialized in this way. The value for a parameter is set by the caller.  

Final Variables 

You can declare a variable in any scope to be final, including parameters to methods and constructors. The value of a final variable cannot change 
after it has been initialized. 

To declare a final variable, use the final keyword in the variable declaration before the type: 

         final int aFinalVar = 0; 

The previous statement declares a final variable and initializes it, all at 
once. Subsequent attempts to assign a value to aFinalVar result in a 
compiler error. You may, if necessary, defer initialization of a final variable. 
Simply declare the variable and initialize it later, like this: 

         final int blankfinal; 
         . . . 
         blankfinal = 0; 

A final variable that has been declared but not yet initialized is called a 
blank final. Again, once a final variable has been initialized it cannot be set 
and any later attempts to assign a value to blankfinal result in a compile-time error. 
 
 Onto Operators and Expressions
 

This page was created by Bob Perini 
For CS 763(Web Seminar) at SUNY Buffalo