Skip to main content

Introduction to Java Programming

  What is Java? Java is a programming language and a platform. Java is a high-level, robust, object-oriented and secure programming language. Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java. Firstly, it was called  Greentalk  and the file extension was  .gt . After that, it was called  Oak . Initially Java was designed for small, embedded systems in electronic appliances like set-top boxes. Platform:  Any hardware or software environment in which a program runs, is known as a platform. Since Java has a runtime environment (JRE) and API(Application Programming Interface), it is called a platform. The principles for creating Java programming were "Simple, Robust, Portable, Platform-independent, Secured, High performance, Multithreaded, Architecture neutral, Object-oriented, Interpreted, and Dynamic". Currently, Java is used in internet programming, mobile devices, games, e-business so

5. Variables, Data Types and Escape Sequences in Java

 

Variables

  • A variable is a container that stores a value.
  • A variable is a name of the memory location and used to store data.
  • Its value can be changed and it can be reused many times.

Rules for defining Variables

  • A variable can have alphabets, digits and underscore.
    Mahi_7 is a valid variable name.
  • A variable name can start with the alphabet, and underscore only. It can't start with a digit.
    7_Mahi is an invalid variable name.
  • No whitespace is allowed within the variable name.
    MS Dhoni is an invalid variable name.
  • Variable name is case sensitive.
    Mahi and mahi are different valid variable names.
  • A variable name must not be any reserved word or keyword.
    void is a reserved word, so it is an invalid variable name.

Types of Variables

There are three types of variables in Java:

  1. Local variable
  2. Instance variable
  3. Static variable

1. Local Variable

  • A variable declared inside a function is called local variable.
  • Local variable is accessible only within that function.
  • Local variables are inaccessible to the functions of even same class outside the function they are declared in.
class Local {
    int method() {
        int m = 10;
        return m;
    }

    public static void main(String[] arr) {
        Local obj = new Local();
        System.out.println(obj.method());
    }
}

Output :

10

2. Instance Variable

  • A variable declared inside the class but outside the body of the method.
  • It value is instance specific and is not shared among instances.
  • Their values are unique to each instance of a class.
class Instance {
    int m = 10;

    public static void main(String[] arr) {
        Instance obj = new Instance();
        System.out.println(obj.m);
    }
}

Output :

10

3. Static Variable

  • A variable that is declared with the static keyword is called a static variable.
  • We can create a single copy of the static variable and share it among all the instances of the class.
  • Memory allocation for static variables happens only once when the class is loaded in the memory.
  • Static variables can be accessed by calling with the class name.
class Static {
    static int m = 10;

    public static void main(String[] arr) {
        System.out.println(Static.m);
    }
}

Output :

10

Data Types

A data type specifies the type of data that a variable can store such as integer, float, character etc.

Boolean Data Type

  • The Boolean data type is used to store only two possible values: true and false.
  • Default value is false.
  • The Boolean data type specifies one bit of information, but its size can't be defined precisely.

Byte Data Type

  • It is an 8-bit (1 byte) signed two's complement integer.
  • Its minimum value is -128, maximum value is 127 and default value is 0.
  • The byte data type is used to save memory in large arrays and can also be used in place of int data type.

Short Data Type

  • The short data type is a 16-bit (2 bytes) signed two's complement integer.
  • Its minimum value is -32768, maximum value is 32767 and default value is 0.
  • The short data type can also be used to save memory just like byte data type.

Int Data Type

  • The int data type is a 32-bit (4 bytes) signed two's complement integer.
  • Its minimum value is -2147483648, maximum value is 2147483647 and default value is 0.
  • The int data type is generally used as a default data type for integral values unless if there is no problem about memory.

Long Data Type

  • The long data type is a 64-bit (8 bytes) two's complement integer.
  • Its minimum value is -9223372036854775808, maximum value is 9223372036854775807 and default value is 0.
  • The long data type is used when we need a range of values more than those provided by int.

Float Data Type

  • The float data type is a single precision 32-bit (4 bytes) floating point.
  • Its value range is unlimited and default value is 0.0f.
  • It is recommended to use a float (instead of double) to save memory in large arrays of floating point numbers.

Double Data Type

  • The double data type is a double-precision 64-bit (8 bytes) floating point.
  • Its value range is unlimited and default value is 0.0d.
  • The double data type is generally used for decimal values just like float.

Char Data Type

  • The char data type is a single 16-bit Unicode character.
  • Its minimum value is '\u0000' or 0, maximum value is '\uffff' or 65535 and default value is '\u0000'.
  • The char data type is used to store characters.

Program to display size of Data Types

class Size {
    public static void main(String[] arr) {
        System.out.println("Size of byte: " + (Byte.SIZE / 8) + " bytes");
        System.out.println("Size of short: " + (Short.SIZE / 8) + " bytes");
        System.out.println("Size of int: " + (Integer.SIZE / 8) + " bytes");
        System.out.println("Size of long: " + (Long.SIZE / 8) + " bytes");
        System.out.println("Size of float: " + (Float.SIZE / 8) + " bytes");
        System.out.println("Size of double: " + (Double.SIZE / 8) + " bytes");
        System.out.println("Size of char: " + (Character.SIZE / 8) + " bytes");
    }
}

Output :

Size of byte: 1 bytes
Size of short: 2 bytes
Size of int: 4 bytes
Size of long: 8 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 2 bytes

Escape Sequences in Java

An escape sequence in Java language is a sequence of characters that doesn't represent itself when used inside string literal or character.
It is composed of two or more characters starting with backslash \. For example : \n represents a new line.

List of Escape Sequences in Java

Escape SequencesMeaning
\tInsert a tab in the text at this point.
\bInsert a backspace in the text at this point.
\nInsert a newline in the text at this point.
\'Insert a single quote character in the text at this point.
\"Insert a double quote character in the text at this point.
\\Insert a backslash character in the text at this point.

Previous

Next

Comments