Java Data Types & Operators.

Ad Code

Java Data Types & Operators.

 


Java Data Types

Data types are divided into two groups:

  • Primitive data types - Under it you will use byte, short, int, long, float, double, boolean and char
  • Non-primitive data types - Under it you will use String, Arrays and Classes
  • Primitive Data Types: A primitive data type specifies the size and type of variable values, and it has no additional methods. There are eight primitive data types in Java:
Data TypeSizeDescription
byte1 byteStores whole numbers from -128 to 127
short2 bytesStores whole numbers from -32,768 to 32,767
int4 bytesStores whole numbers from -2,147,483,648 to 2,147,483,647
long8 bytesStores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float4 bytesStores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double8 bytesStores fractional numbers. Sufficient for storing 15 decimal digits
boolean1 bitStores true or false values
char2 bytesStores a single character/letter or ASCII values

Literals in Java:
A literal is a fixed value that we assign to a variable in a Program.
int n=10;
Here value 10 is a Integer literal.
Integer literals are assigned to the variables of data type byte, short, int and long.
Float Literals are assigned to the variables of data type float and double.
Char and String Literal Used for char and String type.

  • Non-Primitive Data Types: Non-primitive data types are called reference types because they refer to objects. Non-primitive types are created by the programmer and is not defined by Java (except for String).
  • Examples of non-primitive types are Strings, Arrays, Classes, Interface, etc.
    Java Operators

    Operators are used to perform operations on variables and values. Java divides the operators into the following groups:

    • Arithmetic operators
    • Assignment operators
    • Comparison operators
    • Logical operators
    • Bitwise operators

    Java Arithmetic Operators

    These operators are used to perform mathematical operations.

    OperatorNameDescriptionExample
    +AdditionAdds together two valuesx + y
    -SubtractionSubtracts one value from anotherx - y
    *MultiplicationMultiplies two valuesx * y
    /DivisionDivides one value by anotherx / y
    %ModulusReturns the division remainderx % y
    ++IncrementIncreases the value of a variable by 1++x
    --DecrementDecreases the value of a variable by 1--x


    Java Assignment Operators

    These operators are used to assign values to variables.

    OperatorExampleSame As
    =x = 5x = 5
    +=x += 3x = x + 3
    -=x -= 3x = x - 3
    *=x *= 3x = x * 3
    /=x /= 3x = x / 3
    %=x %= 3x = x % 3
    &=x &= 3x = x & 3
    |=x |= 3x = x | 3
    ^=x ^= 3x = x ^ 3
    >>=x >>= 3x = x >> 3
    <<=x <<= 3x = x << 3


    Java Relational Operators

    These operators are used to compare two values:

    OperatorNameExample
    ==Equal tox == y
    !=Not equalx != y
    >Greater thanx > y
    <Less thanx < y
    >=Greater than or equal tox >= y
    <=Less than or equal tox <= y


    Java Logical Operators

    These operators are used to determine the logic between variables or values:

    OperatorNameDescriptionExample
    && Logical andReturns true if both statements are truex < 5 &&  x < 10
    || Logical orReturns true if one of the statements is truex < 5 || x < 4
    !Logical notReverse the result, returns false if the result is true!(x < 5 && x < 10)


    Java Bitwise Operators

    These operators are used to perform binary logic with the bits of an integer or long integer.

    Ope-
    rator
    Desc-
    ription
    Exa-
    mple
    Same asResultDec-
    imal
    &AND - Sets each bit to 1 if both bits are 15 & 10101 & 00010001 1
    |OR - Sets each bit to 1 if any of the two bits is 15 | 10101 | 00010101 5
    ~NOT - Inverts all the bits~ 5 ~01011010 10
    ^XOR - Sets each bit to 1 if only one of the two bits is 15 ^ 10101 ^ 00010100 4
    <<Zero-fill left shift - Shift left by pushing zeroes in from the right and letting the leftmost bits fall off9 << 11001 << 100102
    >>Signed right shift - Shift right by pushing copies of the leftmost bit in from the left and letting the rightmost bits fall off9 >> 11001 >> 1110012
    >>>Zero-fill right shift - Shift right by pushing zeroes in from the left and letting the rightmost bits fall off9 >>> 11001 >>> 101004





    Post a Comment

    0 Comments

    Ad Code