Skip to main content

Variables and data types

 Variables and data types

    A variable is an identifier that does not store location. It use to store data values. Unlike constant that remain unchange during the execution of program, but a variable may take different value or can change value different times during the execution of program.

    A variable name can be chosen by programmer in meaningful way so it can reflect what it represent in the program. Some example are as follows:

  •     average
  •     height
  •     total_height
  •     classStrength

    Variable name may consist of alphabets, digits, the underscore ( _ ) and dollar ( $ ) characters.  It has some conditions those are as follows:

  •     They must not begin/start with digit.
  •     Uppercase and lower case are distinct. That means TOTAL is not same as total.
  •     It should not be a keyword.
  •     White space is not allowed.
  •     Variable name can be any length.


Data Types

    Every variable in java has data type. Data type specifies the size and type of value that can be stored. There are many data types are present in java so programer can chosen the data type according to his need or requirement.


Integer Types:

    Integer type can hold whole number such as 123, -96, and 5689. The size of the values that can be stored depends on integer data type we choose. Java support 4 types of interger.  They are byte, short, int and long.


    It should be remember that wider data types required more time for manipulation and there for it is advisable to use smaller data type. For example, instead of storing a number 50 in an int typs variable, we must use byte variable to handle this number. This will improve the speed of execution of program.

Floating point:

    Integer type can hold only whole number and therefore we use another type known as floating point type to hold number containing fractional parts such as 12.50 and -1.324.


    The float type values are single-precision numbers while the double type represent double-precision numbers. Floating point numbes are treated as double-precision quantities. To force them into single-precision mode, we must append f or F to the number. For example,

    1.235f

    7.524568F

Double-precision type are use when we need grater precision in storage like all mathematical function such as sin, cos and sqrt returns double type value.

Chatacter type:

    In order to store character constants in memory, Java provides a charactor data type call char. The size of char is 2 bytes which can hold one single character.

Boolean type:

    Boolean type is use when we want to test a aprticular condition during execution of program. There are only two values true and false. Boolean is denoted by the keyword boolean.

Comments

Popular posts from this blog

Introduction of Java

 What is Java? Java is an object-oriented programming language developed by Sun Microsystem in 1995. It is a combination of C and C++ features with some essential and additional concepts. Java is suited for both standalone and web application development. Java can run on any hardware OR software environment which make it platform independent. Java Example:                                                                                           class Simple{     public static void main(String args[]){          System.out.println("Welcome In Java World");     } } In above example the 'Simple' is a Class name, public is access specifier , Main is a method with argument, St...

Java Package

 Introduction      A java package is a group of similar types of classes, interfaces and sub-packages. Package in java can be categorized in two form, built-in package and user-defined package. There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc. Here, we will have the detailed learning of creating and using user-defined packages. Simple example of java package The package keyword is used to create a package in java. //save as Simple.java   package mypack;   public class Simple{    public static void main(String args[]){       System.out.println("Welcome to package");      }   }   How to access package from another package? There are three ways to access the package from outside the package. 1. import package.*; 2. import package.classname; 3. fully qualified name. 1. Using packagename.*      If you use package.* the...

Java Classes, Object and Methods, Constructors

 Defining a class     A class is a user-defined data type or group of object with a template or blueprint that serves to define its properties. It is a logical entity. Once a class type has been define, we can create " Variables " which are similar to basic type declaration. In java, variables are termed as instance of class, which actual object. The basic form class defination is as follows: class classname [extends superclassname]{     [fields declaration]     [method declaration] } The classname and superclassname are valid java identifiers. The keyword extends indicate that superclassname properties extended into classname. It is a pert of inheritance. (We will discuss inheritance in later part.) Everything inside square brackets is optional that means we can use as per the requirements. For example, class Empty{ } In above example, the Empty class is class name. This class does not have any properties and therefor cannot do anything. A java cla...