Variables and Data Types

Variables

  • The variable is the basic unit of storage in a Java program.
  • A variable is defined by the combination of an identifier, a type, and an optional initializer.
  • In addition, all variables have a scope, which defines their visibility, and a lifetime.

Declaring Variables

In Java, all variables must be declared before they can be used. The basic form of a variable declaration is shown here:

type identifier [= value][, identifier [= value]…];

String name = "John";
System.out.println(name);

The Scope and Lifetime of Variables

  • A block defines a scope. Thus, each time you start a new block, you are creating a new scope.
  • A scope determines what objects are visible to other parts of your program.
  • It also determines the lifetime of those objects.

Why Data Types are Important in Java?

  • Java is a strongly-typed language.
  • Every variable, expression has a type, and every type is strictly defined.
  • All assignments, whether explicit of via parameter passing in method calls, are checked for type compatibility.
  • No automatic coercions or conversions of conflicting types in Java, as in some other languages.
  • The Java compiler checks all expressions and parameters to ensure that the types are compatible.
  • Any type mismatches are errors that must be corrected before the compiler will finish compiling the class(es).

Java’s Primitive Types

Java defines eight primitive data types: byte, short, int, long, float, double, char and boolean.

GroupDescription
IntegersThis group includes byte, short, int, and long, which are for whole-valued signed numbers.
Floating PointsThis group includes float and double, which represent numbers with fractional precision.
CharactersThis group includes char, which represent symbols in a character set, like letters and numbers.
BooleanThis group includes boolean, which is a special type for representing true/false values.

Integers

Java defines four integral types: byte, short, int, and long. All of these are signed, positive and negative numbers. Java does not support unsigned, positive-only numbers.

NameWidthRange
byte8–128 to 127 
short16–32,768 to 32,767 
int*32–2,147,483,648 to 2,147,483,647 
long64–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 

*int the default type.

Floating Points

Floating-points numbers, also know as real numbers, are used when evaluating expressions that requires fractional precision. For example, calculations such as square root, or transcendentals such as sine and cosine, resulted in a value whose precision requires a floating point numbers.

NameWidthRange
float321.4e–045 to 3.4e+038 
double*644.9e–324 to 1.8e+308 

*double is the default type.

Characters

In Java, the data type used to store characters is char. At the time of Java’s creation, Unicode required 16 bits. Thus, in Java char is a 16-bit type. The range of a char is 0 to 65,536. There are no negative chars. The standard set of characters known as ASCII still ranges from 0 to 127 as always, and the extended 8-bit character set, ISO-Latin-1, ranges from 0 to 255. 

Boolean

Java has a primitive type, called boolean, for logical values. It can have only one of two possible values, true or false. 

Java Constant

  • As the name suggests, a constant is an entity in programming that is immutable.
  • In other words, the value that cannot be updated/changed/edited.
  • In Java, to accomplish this, the variable is declared using the final keyword.

Java Type Casting and Conversion

Convert a value from one data type to another type is known as type casting.

 

Type Conversion in Assignments

  • If the two types are compatible, then Java will perform the conversion automatically. For example, it is always possible to assign int to a long variable without an explicit casting.
  • However, not all types are compatible, and this, not all type conversions are implicitly allowed. For instance, there is not automatic conversion from double to byte.
  • Fortunately, it is still possible to obtain a conversion between incompatible types.
  • To do so, you must use a cast, which performs an explicit conversion between incompatible types.

Java’s Automatice Conversions

When one type of data is assigned to another type of variable, an automatic type conversion will take place if the following two conditions are met: 

  • The two types are compatible.
  • The destination type is larger than the source type.

When these two conditions are met, a widening conversion takes place. For example, the int type is always large enough to hold all valid byte values, so no explicit cast statement is required. 

For widening conversions, the numeric types, including integer and floating-point types, are compatible with each other. However, there are no automatic conversions from the numeric types to char or boolean. Also, char and boolean are not compatible with each other. 

Casting incompatible types

To create a conversion between two incompatible types, you must use a cast. A cast is simply an explicit type conversion. It has this general form: 

variable = (target-type) value;