realLifeinfo

Nature of Java data types and variables declarations explained

Nerd, Solutions 

Everything in Java is an object. You need to wrap all the functionality of your application in an object in order to use them. A class is the type in the Java, so you need to create a variable of type of your class in order to store that class object in the particular variable.

Sponsored

Variables In Java

As many other programming languages. Java support variables, which can be used to store information and data for future reference.

There are two major types of variables in Java.

How variables are declared in Java Code

Variables either Primitives or Complex objects are declared using three parts. The first part determines the data type that will be stored in the variable. The second part determines the name that will be used to referrer to the variable, and the last part, which is optional, is used to provide an initial value to the variable.

Examples of variable declaration in Java:

Primitive: 

float price = 2.01;

int age = 22; 

These are example of primitive variables of type float and integer types respectively.

Complex Objects:

 String name = "Hello";

Date today = new Date();

Many complex objects in Java are crated using constructors, which are used to instantiate an object.

Variable scope in Java

Like many other programming languages, variable in Java have scope, which is the accessibility of the variable in the Java code.

Different programming languages have their own rules that guide the scope of the variable, and this is not exceptional to Java.

In a java code variables have two scopes, which are block based scope like in C and C++ programming languages. When a variable is declared inside a block that variable will only be accessible throughout that block.

Example, when a variable is declared inside a function, it will only be accessible to that function only.

Another type of variable scope in Java is the global scope, which is the variable that is declared outside of any function. These variables are called fields or class variables in Java terminologies, and they can be accessible by any function that belong to the class, in which they are defined.

 Numeric data types in Java

They are several numeric data types in Java. These includes:

And other two numeric data types, which they are used in representing floating points numbers.

Numeric data types initialization process in Java using numeric literals.

Sponsored

Each numeric data type has its own syntax for variable initialization using numeric literals syntax. 

Example:

As you can see from the above example, when we initialize long, float and double, we place a letter in-front of the value to tell Java virtual machine what kind of value we pass to the variable. This is a recommended practice, but if the letter is not passed, java code will continue to work without any problems. The letter can either be an upper case letter or a lower case letter both will work.

 Java Also provides what's called numeric wrapper/numeric helper classes. These are classes that can be used to represent Java primitive values, and also provide additional functionality that can be applied to manipulate these numeric values.

These wrappers have the same name as their primitive counterpart. 

Example:

One example of using wrapper class is when we want to convert a primitive value into another primitive type. This can be accomplished by wrapping the value into its wrapper class, and then invoke one of the built-in methods to convert it into a particular type. 

Example we can convert an integer into a string.

int myNumber = 2000;

Integer myNumberObject = new Integer(myNumber);

System.out.println(myNumberObject.toString());

In this code we first create an object of type integer, and then we use that object to access one of the built-in methods, which is the toString() method that will return a string representation of our number.

And when working with larger numbers that require high precision, it's recommended to use the BigDecimal class.

 Numeric data types conversion

Primitive data types can be converted from one data type into another data type in Java. Conversion can be done in one of these two ways.

The first one is an upward conversion - convert from smaller data types (byte) upward into larger data types (double). The second one is a downward conversion - convert from larger data types (double) downward into smaller data types (byte).

Sponsored

For the upward conversion we don't need to use any special syntax. This can be done simply by assigning value from a smaller data type variable into a variable of a larger data type.

Example:

int myNumber = 10;

double myDoubleNumber = myNumber;

Java will perform this conversion implicitly without programmer intervention.

For downward conversion we need to use a special syntax - explicitly type conversion - to convert from a larger data type into a smaller data type. If we try to assign value from a larger data type variable into a smaller data type variable, Java compiler will issue a compile error, and refuse to compile the code.

 This type of conversion is know as type casting, and to achieve this, we need to use this syntax. 

Example:

double myDoubleValue = 29819d; // this is a literal double

int myIntValue = (int)myDoubleValue;

The casting operation involves brackets surrounding the primitive data type we wish to convert into.

The downward conversion will lose or truncate the data most of the time.

Another method of type casting is by using the helper class syntax:

Example:

double myDoubleObject = new Double(3.27);

int myInt = myDoubleObject.intValue();

We use the built-in intValue() method to convert the double value into an integer value.

Sponsored