Data Types and Variables

The explanation of data types and variables has been made simple and clear.

Data Type:

Data types refer to the different sizes and values that can be stored in the variable. 

Two types of Data Types:

  • Primitive data type
    • Boolean type
      • Boolean
    • Numeric type
      • character
        • Char
      • Integral
        • Integer
          • Byte
          • short
          • Integer
          • Long
        • Floating-point
          • Float
          • Double
  • Non-primitive data type
    • Class
    • Strings
    • Interfaces
    • Arrays etc…

Primitive Data types:

Primitive data types are built-in or predefined data types and can be used directly by the user to declare variables.

To find Range : Formula

-2n-1 to 2n-1 +1

for example: Byte : 1 byte = 8bits ; so n=8 ; Apply

-128 to 127 is a byte range.

Examples:

public class Primitive{
   public static void main (String [] args){
          int num = 5; // Integer (whole number)
          long l = 1234567l; // Long
          float f= 2.76f;  // Floating point number
          double d = 21.4567 ; // no need to add 'd'
          char c ='D';//Character 
          boolean b1 = true;  // Boolean
          boolean b2 = false; // Boolean
        System.out.println(num);
        System.out.println(l);
        System.out.println(f);
        System.out.println(d); 
        System.out.println(c); 
        System.out.println(b1);
        System.out.println(b2);
}
}

OUTPUT:
5
1234567
2.76
23.4567
D
true
false
Char:  Character should be any number ,any alphabet or any special character within single quotation.

Non-Primitive Data types:

Non-primitive data structure is a type of data structure that can store the data of more than one type

String:

  • A string is a sequence of characters and can contain letters, numbers, symbols and even spaces.
  • It must be enclosed in quotation marks for it to be recognized as a string.

For example:

String S = ” Hello world”;

KEYWORDS:

  • Keywords are the reserved words in Java having certain meanings. These words are not allowed to use as variable names or object names.
  • There are 67 keywords in java

Examples:

  • abstract
  • break
  • case
  • catch
  • class
  • continue
  • default
  • do etc….

NAMING CONVENTIONS:

RULES FOR CLASS NAME:

  • It should start with the uppercase letter.
  • It should be a noun ,not verb at all (eg., running,playing)
  • If the name contains multiple word, underscore is used(eg., School_mate)
  • It should be meaningful
  • No special character allowed except $ and _.
  • Numbers are allowed in middle and end.
  • It follows Camel notation

Example: School; School_mate ; Schoolmate, School_mate_2.

Camel Notation: If the name is combined with two words, the second word will start with uppercase ( SumOfNumbers ; reverseString ).

RULES FOR DECLARING VARIABLES:

  • You can begin the variable’s name with an alphabet, a dollar or underscore symbol, or a currency symbol, but not any other special symbol.
  • A variable’s name cannot contain more than 64 characters.
  • You cannot use blank spaces while declaring a variable.
  • Java-reserved keywords cannot be used as variable names.
  • The variable name must appear to the left of the assignment operators.
SYNTAX: data_type variable_name = value;

Example: int posNumber = 100;

Types of variables (click)

Comments

Popular posts from this blog

"Don't Believe Everything You Think"-Joseph Nguyen

HOW JAVA WORKS

How Java Works (Interview Question Answers)