Casting

The process of converting one data type to another is called casting. Casting is often necessary when a function returns a data of type in different form then we need to perform an operation. Under certain circumstances Type conversion can be carried out automatically, in other cases it must be “forced” manually (explicitly). For example, the read() member function of the standard input stream (System.in) returns an int. If we want to store data of type int returned by read() into a variable of char type, we need to cast it :

import java.util.Scanner;

public class binaryAddition {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in); 

        System.out.println("Enter binary input: ");
        String input1 = sc.nextLine(); 
        System.out.println(input1);

        System.out.println("Enter another binary input: ");
        String input2 = sc.nextLine(); 
        System.out.println(input2);

        int sum = Integer.parseInt(input1, 2) + Integer.parseInt(input2, 2); 
        System.out.println();
        System.out.println("Your output is: ");
        System.out.println(Integer.toBinaryString(sum)); 
    }
}


binaryAddition.main(null);
Enter binary input: 
1001
Enter another binary input: 
0010

Your output is: 
1011

Wide type casting example

class Main {
    public static void main(String[] args) {
      // create int type variable
      int num = 10;
      System.out.println("The integer value: " + num);
  
      // convert into double type
      double data = num;
      System.out.println("The double value: " + data);
    }
  }

  
Main.main(null)
The integer value: 10
The double value: 10.0

Narrow type casting

Casting with rounding

The English meaning of truncate is to trim or prune, or cut something and the process of trimming is called truncation. In the computer science field, the term is often used in reference to data-types or variables (like String, floating-point numbers, etc.). It is a way of approximation. Let's discuss what is truncation in Java and how can we truncate a floating or double-type number through a Java program.

Truncation In Java programming, truncation means to trim some digits of a float or double-type number or some characters of a string from the right. We can also truncate the decimal portion completely that makes it an integer. Remember that after truncation, the number will not be round to its nearest value. Hence, truncation is a way of approximation.

It is usually used in computing (especially in database and programming) when division is done with integers and the results must be an integer.

class Main {
    public static void main(String[] args) {
      // create double type variable
      double num = 10.99;
      System.out.println("The double value: " + num);
  
      // convert into int type
      int data = (int)num;
      System.out.println("The integer value: " + data);
    }
  }
  Main.main(null)
The double value: 10.99
The integer value: 10

Operator Operation

  • Addition
  • Subtraction
  • Multiplication / Division % Modulo Operation (Remainder after division)
// 2A
public double purchasePrice(double x){
  x *= 1.3; 
  return x;
}

purchasePrice(6.50);
8.450000000000001

Casting truncated

public class NearestInt //class name
{
   public static void main(String[] args)
   {
     double number = 5.0 / 3;
     int nearestInt = (int)(number + 0.5);
     System.out.println("5.0/3 = " + number);
     System.out.println("5/3 truncated: " + (int)number );
     System.out.println("5.0/3 rounded to nearest int: " + nearestInt);
     double negNumber = -number;
     int nearestNegInt = (int)(negNumber - 0.5);
     System.out.println("-5.0/3 rounded to nearest negative int: " + nearestNegInt);
   }
 }

 NearestInt.main(null)
5.0/3 = 1.6666666666666667
5/3 truncated: 1
5.0/3 rounded to nearest int: 2
-5.0/3 rounded to nearest negative int: -2
// 3A
public class Customer {
    private String name;
    private int id;
    
    public Customer(String name, int idNum){
        this.name = name;
        this.id = idNum;
        
    };

    public String getName(){
        return name;
    };

    public int getID(){
        return id;
    };

    public void compareCustomer(Customer x){
        System.out.println(id - x.id);
        
    };
    
}

Customer c1 = new Customer("Smith", 1001);
Customer c2 = new Customer("Anderson", 1002);
Customer c3 = new Customer("Smith", 1003);

c1.compareCustomer(c1);
c1.compareCustomer(c2);
c1.compareCustomer(c3);
0
-1
-2

Wrapper Classes

Wrapper classes provide a way to use primitive data types (int, boolean, etc..) as objects.

The table below shows the primitive type and the equivalent wrapper class:

Why Wrap It???

Sometimes you must use wrapper classes, for example when working with Collection objects, such as ArrayList, where primitive types cannot be used (the list can only store objects):

They convert primitive data types into objects. Objects are needed if we wish to modify the arguments passed into a method (because primitive types are passed by value). The classes in java.util package handles only objects and hence wrapper classes help in this case also. Data structures in the Collection framework, such as ArrayList and Vector, store only objects (reference types) and not primitive types. An object is needed to support synchronization in multithreading.

Primitive Data Type Wrapper Class

KEY

byte Byte short Short int Integer long Long float Float double Double boolean Boolean char Character

Creating wrapper objects

public class Main {
    public static void main(String[] args) {
      Integer myInt = 5;
      Double myDouble = 5.99;
      Character myChar = 'A';
      System.out.println(myInt);
      System.out.println(myDouble);
      System.out.println(myChar);
    }
  }

  Main.main(null);
5
5.99
A

Concatenation

Concatenation is the process of combining two or more strings to form a new string by subsequently appending the next string to the end of the previous strings. In Java, two strings can be concatenated by using the + or += operator, or through the concat() method, defined in the java. lang. String class

Example with strings

class Main{  
    public static void main(String args[]){  
      String s=" Sachin"+" Tendulkar";  
      System.out.println(s);//Sachin Tendulkar  

      String f="Aryan"+" Maulik"+" Shah"; 
      System.out.println(f);


      // Concatenation with direct variables
String g=f+s;
System.out.println(g);

    }  
   }  
   Main.main(null)
 Sachin Tendulkar
Aryan Maulik Shah
Aryan Maulik Shah Sachin Tendulkar

Also Possible to do Concatenation with integers into a string

class Main{  
    public static void main(String args[]){  

      
      String s=20+30+" Aryan "+95+40;  
      System.out.println(s);  
    }  
   }  

   Main.main(null)
50 Aryan 9540