C++ Operators and Type Casting

 Operators in Java


Operator in java is a symbol that is used to perform operations.For example +,-,*,/ etc 


There are many types of operators in java which are given below:


o Unary Operator,

o Arithmetic Operator,

o shift Operator,

o Relational Operator,

o Bitwise Operator,

o Logical Operator,

o Ternary Operator and

o Assignment Operato

 

Operator Hierarchy


Operator Precedence


Note: java also allows us to use logical operators in nested if statements. Java provides many features this is the main function of java programming which allows us to do nesting with logical operations. for examples 


if(name=stuat || age=8)
    {
        if(class=5 || addr = new_york)
            { 
                system.out.println("You can join our program");
            }
    }
else
{ .... }



Expressions in java :

Expressions are used to produce a new value, and sometimes they are simply used to assign a new value to a variable, They can be created using values, variables, operators, and method calls.


Types of expression 


There are three types of expressions in java:

  • Those that produce a value, for example the result of (1+1)
  • Those who assign a variable, for example (age = 10)
  • Those who have no result but might have a "side effect" because an expression can include a wide range of elements such as method invocations or increment operators that  modify the state (i.e. memory) of a program

Java Typecasting and Type conversion

Winding or Automatic type conversion

Winding conversion happens when two data types are automatically converted. This happens when: 
  • The two data types are compatible
  • When we assign value of  smaller data type to bigger data types

For Example, in java the numeric data types are compatible with each other but no automatic conversion is supported from numeric type to char or boolean. Also, char and boolean are not compatible with each other.


Byte -> Short -> Int -> Long -> Float -> Double
Winding or Automatic Conversion


Narrowing or Explicit Conversion

If we want to assign a value of larger data type to a smaller data type then we perform explicit type casting in other words narrowing.
  • This is useful for incompatible data types where automatic conversion cannot be done.
  • Here, target type specifies the desired type to convert the specified value to.

Double-> Float-> Long-> Int-> Short-> Byte
Narrowing or Explicit Conversion


Post a Comment (0)
Previous Post Next Post