Data types and Operators

Datatypes of Java:



Java Syntaxes

Constants:
Constants are those value do not change during the execution of the Program.
Ex: 55, 3.14,'A', "World"
Variables:
Variables are those whose value can be changed during the execution of the program.
Ex: int n; float area; char c;

The Conventions used to declare Identifiers

Identifiers are programmer designed tokens. They are used for naming classes, methods, variables, objects, labels, packages and interfaces in a program.
Java identifiers follow the following rules:-

  • They can have alphabets, digits, underscore and the dollar sign characters.
  • They must not begin with a digit.
  • Uppercase and lowercase letters are distinct.
  • They can be of any length.

Java developers have followed some naming conventions and they are as follows

  • Names of all public methods and instance variables start with a leading lowercase letter.
    e.g.: marks, sum
  • When more than one word is used in a name, the second and subsequent words are marked with leading uppercase letters.
    e.g.: dayOfWeek, dayTemperature
  • All private and local variables use only lowercase letters combined with underscores.
    e.g.: width, basic_sal
  • All classes and interfaces start with a leading uppercase letter and each subsequent word with a leading uppercase letter.
    e.g.: HelloWorld, SwitchUse
  • Variables that represent constant values use all uppercase letters and underscores between the words

Operators

Increment and decrement operators
     ++, --
Arithmetic
    +, -, *, /, %
Shift
    >>, <<
Comparison
    ==, !=, <, >, <=, >=
Bitwise
    &, |, ^, ~
Logical
    &&, ||, !
Assignment
    +=, -=, *=, /=,%=,&=, |=, ^=, <<=, >>=, >>>=

Bit wise operators

Shift operators

Left Shift:
op1<<op2 ; shifts bits of op1 left by distance op2.
EX: 1010<<1 Result: 10100.
Left shift operation is multiplication by two operation.
Right Shift:
op1>>op2; shifts bits of op1 right by distance op2.
EX: 1010>>1 Result: 0101.
Right shift operation is division by two operation.
  • Bit wise And
    1011&1000=
  • Bit wise OR
    1011|1000=
  • Bit wise Exclusive OR
    1011^1000=
  • Bit wise Complement
    ~1011=

Operator Precedence

When more than one operator is used in an expression, then an operator precedence rule is followed to determine the order in which the operators are executed.
The order is

  • parenthesis
  • Increment, decrement operators
  • Arithmetic
  • Comparison
  • Logical
  • Assignment
Ex: 2+5*4/2-3=
3*(2+3)/5+2=
(3+2)>(2*1) && (2-2)<=(5-5)

Ternary Operator Pair

?:
Syntax:
expression1?expression2:expression3;

If expression1 is true then expression2 is evaluated else expression3 is evaluated
Ex:
(a>b)?System.out.println("a is greater"):System.out.println("b is greater");


Java Introduction<< Previous

Next>>Control Flow Statements in Java

Our aim is to provide information to the knowledge seekers. 


comments powered by Disqus






Footer1