Java Typecasting:
int myInt = 9;
double myDouble = myInt;
For Example:
int a=3;
long b=4;
long c=a+b; // a is implicitly cast to a long
Program:
public class Conversion{
public static void main(String[] args)
{
int i = 200;
//automatic type conversion
long l = i;
//automatic type conversion
float f = l;
System.out.println("Int value "+i);
System.out.println("Long value "+l);
System.out.println("Float value "+f);
}
}
double myDouble = 9.78;
int myInt = (int) myDouble;
For Example:
char c='A';
int s=1;
short ans= (short) (c+s);
Program:
public class Narrowing
{
public static void main(String[] args)
{
double d = 200.06;
//explicit type casting
long l = (long)d;
//explicit type casting
int i = (int)l;
System.out.println("Double Data type value "+d);
//fractional part lost
System.out.println("Long Data type value "+l);
//fractional part lost
System.out.println("Int Data type value "+i);
}
}
Expression:
- Subtraction Operator
* Multiplication Operator
/ Division Operator
% Remainder Operator
result = number1 + number2;
+= x += 5 x = x + 5
-= x -= 5 x = x - 5
*= x *= 5 x = x * 5
== equal to 5 == 3 is evaluated to false
!= not equal to 5 != 3 is evaluated to true
> greater than 5 > 3 is evaluated to true
< less than 5 < 3 is evaluated to false
>= greater than or equal to 5 >= 5 is evaluated to true
<= less then or equal to 5 <= 5 is evaluated to true
Operator Description
~ Bitwise Complement
<< Left Shift
>> Right Shift
>>> Unsigned Right Shift
& Bitwise AND
^ Bitwise exclusive OR
| Bitwise inclusive OR
Operator Meaning
+ Unary plus
- Unary minus
++ Increment operator; increments value by 1
-- decrement operator; decrements value by 1
! Logical complement operator
String name= "Sarthak";
boolean result;
result = name instanceof String;
int februaryDays = 29;
String result;
result = (februaryDays == 28) ? "Not a leap year" : "Leap year";
- Typecasting is when you assign a value of one primitive data type to another type.
- Casting refers to the conversion of one data type to another.
- When we cast a particular variable, the data type of the variable itself is not altered.
- For example:
- int num1=3, num2=4;
- double result = (double) num1 / num2;
- When we cast, num1 to a double.
- The variable num1 does not change its data type from int to double.
- Instead, a new copy of num1 is made and its data type is changed to double.
- For Example:
- A. Upcasting & Downcasting
- B. Implicit & Explicit casting
- 1. Upcasting:
- When we cast a variable to a higher data type it is known as upcasting.
- such as...
- byte -> short, int, long, float, double
- short -> int, long, float, double
- char -> int, long, float, double
- int -> long, float, double
- long -> float, double
- float -> double
- For example:
- int is a higher data type than short.
- int is 32 bits memory allocated while short which is 16 bits.
- 2. Downcasting:
- When we cast a variable to a lower data type, it is known as downcasting.
- Such as...
- byte -> char
- short -> byte, char
- char -> byte, short
- int -> byte, short, char
- long -> byte, short, char, int
- float -> byte, short, char, int, long
- double -> byte, short, char, int, long, float
- For Example:
- short a = 347;
- int d = (short) b;
- System.out.println(a);
- System.out.println(d);
- 3. Implicit - converting a smaller type to a larger type size
- It is automatically converted.
- When any casting that is performed by the compiler, it is known as implicit casting.
- It is also called as Widening casting.
- byte > short > char > int > long > float > double
int myInt = 9;
double myDouble = myInt;
For Example:
int a=3;
long b=4;
long c=a+b; // a is implicitly cast to a long
Program:
public class Conversion{
public static void main(String[] args)
{
int i = 200;
//automatic type conversion
long l = i;
//automatic type conversion
float f = l;
System.out.println("Int value "+i);
System.out.println("Long value "+l);
System.out.println("Float value "+f);
}
}
- 4. Explicit - converting a larger type to a smaller size type
- It is manually converted.
- When casting is performed by the programmer using the cast operator (), it is known as explicit casting.
- It is also called as Narrowing casting.
- double > float > long > int > char > short > byte
double myDouble = 9.78;
int myInt = (int) myDouble;
For Example:
char c='A';
int s=1;
short ans= (short) (c+s);
Program:
public class Narrowing
{
public static void main(String[] args)
{
double d = 200.06;
//explicit type casting
long l = (long)d;
//explicit type casting
int i = (int)l;
System.out.println("Double Data type value "+d);
//fractional part lost
System.out.println("Long Data type value "+l);
//fractional part lost
System.out.println("Int Data type value "+i);
}
}
Expression:
- Expressions are built using values, variables, operators and method calls.
- For Example: (a * 2)
- You can write a simple expression into a statement by adding a semi-colon.
- For Example: (a * 2);
- 1. Produce a value:
- For Example: (r*r)
- 2. Assign a value:
- For Example: area = pi * (r*r)
- It is a symbol that is used to perform operations.
- Operators are used to perform operations on variables and values.
- For example, + is an operator that performs addition.
- For Example: 10 + 20
- There are mainly three types:
- A. Unary Operators
- Unary operator means only one operator and one operand.
- For Example Assignment Operator
- B. Binary Operators
- Binary operator means two operands and one operator.
- For Example Arithmetic Operator
- C. Ternary Operators
- Ternary operator means three operands and two operators
- For Example Ternary Operator
- 1. Arithmetic operators
- Arithmetic operators are used to perform mathematical operations like +, -,*, etc.
- For Example:
- Subtraction Operator
* Multiplication Operator
/ Division Operator
% Remainder Operator
- For Example:
result = number1 + number2;
- 2. Assignment operators
- Assignment operators are used in Java to assign values to variables.
- For Example int age;
- The assignment operator assigns the value on its right to the variable on its left.
- The other type of assignment operator is called a shorthand assignment.
- For Example:
+= x += 5 x = x + 5
-= x -= 5 x = x - 5
*= x *= 5 x = x * 5
- 3. Comparison or Relational operators
- This operator determines the relationship between the two operands.
- It checks if an operand is >, <, ==, != and so on.
- Depending on the relationship, it results to either true or false.
- For Example:
== equal to 5 == 3 is evaluated to false
!= not equal to 5 != 3 is evaluated to true
> greater than 5 > 3 is evaluated to true
< less than 5 < 3 is evaluated to false
>= greater than or equal to 5 >= 5 is evaluated to true
<= less then or equal to 5 <= 5 is evaluated to true
- 4. Logical operators
- It is used to apply a logical condition.
- A. OR Operator (||)
- conditional-OR; true if either of the boolean expression is true
- For Example, false || true is evaluated to true
- B. AND Operator (&&)
- conditional-AND; true if all boolean expressions are true
- For Example, false && true is evaluated to false
- 5. Bitwise operators
- To perform bitwise and bit shift operators in Java, these operators are used.
- For Example:
Operator Description
~ Bitwise Complement
<< Left Shift
>> Right Shift
>>> Unsigned Right Shift
& Bitwise AND
^ Bitwise exclusive OR
| Bitwise inclusive OR
- 6. Unary operators
- Unary operator performs operation on only one operand.
- For Example:
Operator Meaning
+ Unary plus
- Unary minus
++ Increment operator; increments value by 1
-- decrement operator; decrements value by 1
! Logical complement operator
- 7. instanceof Operator
- Instance of the operator is used for type checking.
- It can be used to test if an object is an instance of a class, a subclass or an interface.
- For Example:
String name= "Sarthak";
boolean result;
result = name instanceof String;
- When you run the program, the output will be true.
- It's because the name is the instance of String class.
- 8. Ternary Operator
- The conditional operator or ternary operator ?: is shorthand for if-then-else statement.
- Syntax: variable = Expression ? expression1 : expression2
- If the Expression is true, expression1 is assigned to variable.
- If the Expression is false, expression2 is assigned to variable.
- For Example:
int februaryDays = 29;
String result;
result = (februaryDays == 28) ? "Not a leap year" : "Leap year";
Tags:
Core java