Java With Us
Home   |   Tutorial   |   Programs  

Java Tutorial

Introduction to Java
Hello World Program
Variables and Data types
More about data types
Displaying text using print and println
Displaying text using printf
Java Comments
Naming conventions for Identifiers
Mathematical operations in Java
Taking input from the user

Classes and Objects
Introduction to object oriented programming
The constituents of a Class
Creating objects and calling methods
Get and Set Methods
Default constructor provided by the compiler
Access Specfiers
Scope and lifetime of Variables
Call by value and Call by Reference

A few more topics
Casting
Class as a reference data type
Constants or literals
Final variables
Increment and decrement operators
Manipulating Strings
Operators
Overloading constructors and methods
Static methods and variables
The Java API
The Math class
this keyword
Wrapper classes

Control Structures
Control Statements
Repetition statements
Nested loops
Formulating algorithms
Branching Statements

Arrays
Arrays introduction
Processing arrays using 1oops
Searching and sorting arrays
Array of objects
Multi dimensional arrays
Taking input as command line arguments
Using ellipsis to accept variable number of arguments

Inheritance
Inheritance introduction
Relation between a super class and sub class
Final classes and methods
The protected access specifier
Class Object

Polymorphism
Introduction
Interfaces
Packages
Abstract classes and methods

Exception handling
Exception handling introduction
Exception hierarchy
Nested try catch blocks
Throwing exceptions


More about Data Types

Here, we will discuss more about the data types we have seen in the previous article.

Storing integers

In the previous example, we have used the int data type to store integers. We may also use the byte, short and the long data types to store integers depending on the maximum or the minimum values that are going to be stored. Following statements initialise four variables of different data tapes with integer values.

byte b = 3;
short c = 4;
int a = 34;
long d = 7L;

There is a need to mention how long integers are assigned to variables. An uppercase l or a lowercase L should be appended to the end of the integer that is going to be stored. This is because all integers are assumed to be of type int, unless specified as long. However, we did not append anything to a byte or a short integer. This is because of implicit casting. Casting refers to converting from one data type to another. 3 was automatically casted from int to byte and similar was the case with short. In the case of long integers, we need the l to specify that it is a long integer. if we omit the L at the end of 7 and compile the program, no compilation errors would be generated. Now, change the value of 7 to something that can be stored in a long variable but not an int variable and see what happens. A compilation errors is generated!

long d = 10147483648;

This happens because, the number on the right is too big for an int. Whenever an integers value if encountered in a program, it is considered as an int value. In the case of byte and short, the numbers 3 and 4 are initially considered to be of type int but since they are assigned to some other data type, they have been implicitly cast into byte and short respectively. In the case of the long integer ( 7L ), we have specified that 7 is a long integer using the letter L. This long integer was assigned to a long variable. If we had written 'long d = 7;', the integer 7 is first taken as an int and then implicitly cast to long. In the case of the number 10147483648, it is initially taken as an int value. But since 10147483648 is too long for an int, a compilation error occurs.

When we wish to store the result of an expression in a byte or short, the result needs to be explicitly cast to the corresponding data type. Following is the syntax for casting

( data type ) < variable, literal, or expression >

For example

byte a = 3;
byte b = 4;
byte c = ( byte ) ( a+b );

Note that a+b is written within parentheses. This is because, cast is applied on the variable, or a literal immediately following the cast data type. To apply it on an expression, the parentheses is required. ( A literal is a constant data item unlike a variable, such as 3 or 4). In an arithmetic expression, bytes and shorts are converted to ints before performing any operation using theose values. Therfore, a and b was first implicitly converted to an int, the resulting int should be explictly cast to a byte.

Storing real numbers

Real numbers are those which have a fractional part i.e. a decimal part. Real numbers are also known as floating point numbers. All real numbers are assumed to be of type double by default. To store a real number in a float variable, an uppercase F or a lowercase f needs to be appended at the end of the number.

float a = 3.4f;
float b = 34.7F;
double c = 64.7;

We generally use the double data type and not float. We may also represent the numbers in exponential notation. A number A*10B is written is exponential notation as AeB or AEB. Either an uppercase e or a lowercase E may be used. The number A is called the mantissa and the number B is called the exponent. Both of them may be either positive or negative. The following three statements are equivalent.

double a = 347.347;
double a = 3.47347E2;
double a = 3.47347e2;


boolean data type

A boolean data type can hold only two values- true or false. Following lines show example usages.

boolean a = true;
boolean b = false;

The char data type

The char data type is used to store a single character such as 'a', 'Z', '9', '&', '$' and so on. Character can be letters, digits or special symbols. A character can also be a non graphic symbol such as a new line or a tab. Characters are always enclosed in single quotes. If we wish to store the value 'A' in a variable x, we would write the following statement.

char x = 'A';

We can use a different variable name instead of x. for example, we could have used the variable name 'firstChar' and written the statement as

char firstChar = 'A';

There is another way to initialise a character variable. We can assign the numbers between 0 and 65,536 ( both inclusive) to a char variable. Each of these numbers corresponds to a particular variable. For example, the number 65 corresponds to uppercase A. so, the following statement is equivalent to the above declaration.

char x = 65;

Note that we do not enclose these numbers in single quotes. This correspondence between a number code and the graphic character is given by the Unicode. A small subset of these codes is the ASCII codes which contains numbers between 0 and 127 ( both inclusive) the ASCII contains the most commonly used symbols.

Following is a list of some of the most commonly used characters and their numeric representation in either the Unicode or the ASCII code.

Characters ASCII / Unicode
0 - 9 48 - 57
a - z 97 - 122
A - Z 65 - 90


Therefore, the number 67 corresponds to 'C', 48 to '0', 49 to '1'and so on.

The String data type

We also quite often use the String adapt type to store a word or sentence. String is a reference data type and not a primitive data type. String is a class and the sentences are the objects of that class. Strings are enclosed in double quotes. Following statements shows the usage of the String data type.

String s ="Java is an object oriented programming language.";

The concatenation operator + may be used in Strings. Following shows an example.

int a = 3;
String str = "The value of a is " + a;

str now stores the String "The value of a is 3"

Privacy Policy