DataTypes and Variables

Data Types

Integer Data Type

Floating Point Data Type

Java provides two floating-point data types as specified by the IEEE-754 standard. These are the float and double data types. The float type represents single-precision numbers while the double type is used for double-precision numbers.

You suffix the value assinged to a float variable with the character f. This is done because the decimal value assigned to a variable is taken as a double data type value by default.

Character Data Type

Useful for storing alpha numeric characters. 16-bit type. It does not accept negative values. A Character literal is assigned to a variable by using single quotation marks.

Syntax: char blahVar;

Range: 0 to 65,535.

Boolean Data Type

A variable of the boolean data type can store only two possible logical values, true and false. It is defined using the boolean keyword.

Syntax: boolean blahVar;

Boolean values are returned when the relational operators are evaluated. The boolean data type is also used to direct the conditional expressions that govern control statements.

HINT: What are control Statements?
They are used in a Java Program to guide the flow of execution to advance and branch depending on the changes made to the data values that are used in the conditional expressions.

String Data Type

Syntax: String blahVar;

A String can contain any combination of 0 or more characters and may also be null. The variable can also store characters such as the slash sign (/). the parentheses signs ( () ), the colon sign (:), and the semicolon sign (;)

Java implements strings as objects of the String data type instead of as character arrays. String objects are immutable. When an object of the String data type is created, you cannot change the characters that are part of that string.

Whenever you change an existing string, a new String object is created that contains the modifications to the existing string. In specific cases where a modifiable string is essential, there is a companion class attached to the String class called StringBuffer. StringBuffer objects represent strings that can be modified after they are created.

A String constant enclosed within double quotation marks can be assigned to a String variable. You cannot perform mathematical calculations on a String object because it does not support operators. An exception to this rule is the addition operator (+). It is used to concatenate two strings and to generate a String object as the result.

HINT: (+) Operator is the best example for polymorphism.

Variables

Declaring Variables

Syntax:

datatype variableName;

Example:

int sum;

Variable Initialization

Before using a variable in a Java program, you must assign a valid initial value to the variable. This process is called initialization. A variable must be declared and initialized before it can be used in a Java program.

Syntax:
variable_name = value;

Example:
int x=13;
float y=23.4f;
z = 92 + 123;

You can also simultaneously declare and initialize a variable.

Syntax:
datatype variableName = value;

Related Posts: Introduction to Java | Classes, Methods & Objects | Objects and Variables | Operators | Conditional Flow Control | Iterative Flow Control | Jump Statements

Advertisements

2 responses to “DataTypes and Variables

  1. deposit free bonus casino
    see to signature…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s