[ PDF ] Type Casting in Java With Example Program | JAVA by TechWithCode


Hello Friends, This is Deepak From TechWithCode.com. Today we will learn an important topic of Java Programming which is Type Casting. The followings are the Points which we will study today. I will try my best to keep the language as simple as possible so you can easily understand this.

At the end of this, you can download a report 

  1. Type Casting 
  2. Operators & Expressions 
  3. Looping statement 
  4. Decision making 

1. Type Casting

Casting is an operation that converts a value of one data type into a value of another data type. 

Casting a type with a small range to a type with a larger range is known as widening a type. 

Casting a type with a large range to a type with a smaller range is known as narrowing a type. 

Java will automatically widen a type, but you must narrow a type explicitly.

Syntax of Type Casting

The syntax for casting a type is to specify the target type in parentheses, followed by the variable’s name or the value to be cast. For example, the following statement


displays 1. 

When a double value is cast into an int value, the fractional part is truncated.
The following statement

System.out.println((double)1 / 2);

displays 0.5, because 1 is cast to 1.0 first, then 1.0 is divided by 2. 

However, the statement System.out.println(1 / 2);
displays 0, because 1 and 2 are both integers and the resulting value should also be an integer


Let Understand all the concepts with an example program.

So I am writing a Java program that will cover  all the following things
  • Type Casting 
  • Operators & Expressions 
  • Looping statement 
  • Decision making 

Source Code ⤵️

class FirstP { 
 public static void main(String[] args) { 
 int a=11; 
 int b=16; 
 int c; 
System.out.println("ARITHMETIC OPERATIONS:--------->"); 
 System.out.println("Add= "+(a+b)); 
 System.out.println("Sub= "+(a-b)); 
 System.out.println("Mul= "+(a*b)); 
 System.out.println("Quotient= "+(a/b)); 
 System.out.println("Remainder= "+(a%b)); System.out.println("\nLOGICAL OPERATIONS:----------->"); 
 if(a>10 && b>10) 
 System.out.println("The Value of A= "+a+"\nThe Value 
of B= "+b); 
System.out.println("\nRELATIONAL OPERATOR"); 
 System.out.println("A is greater than B"); 
 System.out.println("A is less than B"); 
System.out.println("\nWHILE LOOP:----------->"); 
 int i=0; 
 System.out.print(a+" , "); 
System.out.println("\n\nDO WHILE LOOP:----------->"); 
 do  { 
 System.out.print(a+" , "); 
System.out.println("\n\nDO WHILE LOOP:----------->"); 
 System.out.print(a+" , "); 
System.out.println("\n\nDECISION MAKING STATEMENTS:----------->"); 
 System.out.println("A is less than B"); 
 System.out.println("A is not less than B"); 
System.out.println("\nTYPE CASTING:----------->"); 
 float m=7.0867f; 
 int n=(int)m;  System.out.println("After type casting value o
 double p=(double)m;
 System.out.println("After type casting value of p= "+p);

Output ⤵️

TypeCasting in Java Report with source code

Download Now

No comments:

Do not add any link in the comments.
For backlink, contact us.

Powered by Blogger.