Home » Java » Java Tutorial: Data types, Variables and Arrays

Java Tutorial: Data types, Variables and Arrays

Like any other programming language, Java also has the fundamental elements like data types, variable and arrays. Java is a strictly typed language which means that every variables and expression used has a type defined and all the assignments are strictly checked for type compatibility.

To understand data type we must first know what is a variable? Variables are nothing but the basic storage units in a program. And the type of data a variable can hold is what we call the data type.

The various data types that Java supports are:

  • Integers – these are signed whole valued numbers. Data types and their size for integers are :
Java Tutorial for beginners| Data Types

Data Types

  • Floating-points – these represent a number with decimal point or factional part. Data types under this group are float (32 bits) and double (64 bits).
  • Characters – under this comes the char data type which is a Unicode to represent a character and is 16 bits long.
  • Boolean – used to store the logical values, true and false.

Thus there are total eight basic data types in Java.

Java being a strongly typed language does not allow any type mismatch or automatic type conversion. If a floating-point number or character is assigned to it, compilation error occurs.

Variable declaration:

As we know from the previous discussion that variables are storing units of the program. But before using a variable it must be defined with an identifier and the data type it will hold. This is called variable declaration. The basic format of declaring a variable is:

type identifier [=value]

type refers to the variable data type, identifier refers to the name of the variable and the value part is optional. More than one variable can be declared in the same line using just one data type.

Here are some examples of variable declaration:

int a, b, c;  // declares three ints, a, b, and c.

Byte x = 22;  // initializes z.

int d = 10, e, f = 25;  // declares three more ints, initializing d and f.

char z= ‘a’;  // the variable z has the value ‘a’.

double pi = 3.14159; // declares an approximation of pi.

Automatic type conversion occurs only when the destination type is larger than the source type. For example a byte type value can easily be assigned to an integer type variable.

 int a ;

byte b;

a=b;

The problem arises when the destination type is smaller than the source type. For example if we want to assign an int value to a byte variable it becomes incompatible. Here we have to type cast in order to create a conversion between incompatible types. Casting is nothing but explicitly changing the type by writing the desired type in a first bracket.

(target-type) value

For example:

int a;

byte b;

b = (byte) a; // since here ‘b’ is a byte which is smaller than ‘a’( an int), ‘a’ has to be explicitly made byte (casting).

Array

Array is a group of similar type variables referred to by a common name. Each variable in an array is denoted by an index number starting from zero. Arrays are declared as follows: 

type variable-name[]; 

By this we just declare the name of the array and the type of variables it will hold. But no space is actually allocated in the memory. We need to use the new keyword and mention the size of the array in order to actually create the array.

Array-variable= new type[size]; 

 For example if we want to create an integer type one-dimensional array of four elements we will write:

int arr[];

arr = new int[4];

Each element of the above array created is denoted as arr[0], arr[1], arr[2], arr[3]. Or the values can be directly given within the curly braces separated by commas. The second method is called array initializer and new keyword is not needed here.

int arr[]= {10,20,30,40};

Two-dimensional array is an array within an array. It is declared as:

int arrTwo[][]= new int[4][6];

This will create a matrix having 4 rows and 6 columns.

 Similarly arrays within arrays create multidimensional arrays.

An example to illustrate the concept of arrays:

Java tutorial for beginners | Java Variables and Arrays

Java Variables and Arrays

Output:  

Second element of arr1 is: 8

Third element of arr2 is: 10

It must be remembered always that the index counting of array element starts with zero.

NOTE:

  1. According to Digital Millennium Copyright Act Title 17 Chapter 512 (c) (3) www.speed4career.com  has exclusive DIGITAL RIGHT over this format and content. Please read the legal Notice here…
  2. Reproduction or re-publication of this content is prohibited without written permission from www.speed4career.com
  3. While every attempt has been made to verify the information provided here; however www.speed4career.com assumes no responsibility for errors, omissions, results or contradictory interpretation of the content.
  4. Also, you should use this information as you see fit. Your particular situation may not be exactly suited to the information illustrated here.
  5. This tutorial is for informational and education purpose only. www.speed4career.com will not encourage any FINANCIAL terms and conditions from any party. We believe that every industry experts are doing the tutorials with us with consciousness of their individual social responsibilities.
  6. This article is about Java tutorial on Data types, Variables and arrays.

Related Posts

About

Hi everyone... this is Debasmita, a Computer Science Engineer working with Tier 1 company in India and a passionate technology lover. My craving for knowledge in Java Programming and Website Designing has motivated me to share the same with the world. Being a budding engineer I am well acquainted with the difficulties and queries faced by the students and new learners. Willing to help people in learning things in a easy and interesting way.

commentscomments

  1. Hi Debasmita,

    This is a great and informative post, it’s more or less a tutorial. Thanks for sharing this buddy
    Adesanmi Adedotun recently posted…8 Reasons why you are not getting search engine trafficMy Profile
    Twitter:

  2. Margarita says:

    Hi Debasmita,

    I enjoyed your new post on Java data types, variables and arrays. I took Java at the local community college, but did not get proficient in using the code. I did my projects, and I have the foundation to understand your content. You teach in a very easy and funny way. I bet many young bloggers will benefit from your tutorials. Keep up the great work.
    Margarita recently posted…Did I Hear You Correctly? You Don’t Have $25 To Start Your business?My Profile
    Twitter:

  3. debasmita says:

    Hey Adesanmi,

    Thanks for your comment :)
    debasmita recently posted…Java Tutorial: Data types, Variables and ArraysMy Profile
    Twitter:

  4. debasmita says:

    Hey Margarita,

    So good to see see that you follow my tutorial and like it.

    Thanks a lot for stopping by. :)
    debasmita recently posted…Java Tutorial: Data types, Variables and ArraysMy Profile
    Twitter:

  5. Rahul says:

    Hi admin, Nice tips. Good research. All the best for your future writting. Thank you for sharing.
    Rahul recently posted…Tips to Enhance Your Business PlanMy Profile

  6. Baskar says:

    Hi Debasmita,

    It is a good article about Java Basics (Data types, variables and arrays). Since you are good at explaining the basics. I am expecting more article on Java from you

    Thanks and keep going …!

  7. städfirma says:

    Hi there, the whole thing is going nicely here and ofcourse every one is sharing information,
    that’s in fact excellent, keep up writing.
    städfirma recently posted…städfirmaMy Profile

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CommentLuv badge

This blog uses premium CommentLuv which allows you to put your keywords with your name if you have had 3 approved comments. Use your real name and then @ your keywords (maximum of 2)

Google+ Badge