Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java is there an equivalent of the Convert class from C#?

Tags:

java

In C# I liked using the Convert class. It made converting from one type to another easy and consistent. I was thinking about writing a similar class in Java, but I don't want to reinvent the wheel. So I googled around to see if such a thing exists and wasn't getting good results. So is anyone aware of something like this either in the standard libs, google guava, or apache commons?

like image 858
Jason Thompson Avatar asked Dec 21 '12 16:12

Jason Thompson


People also ask

What is a convert in Java?

Converter is an interface describing a Java class that can perform Object-to-String and String-to-Object conversions between model data objects and a String representation of those objects that is suitable for rendering. Converter implementations must have a zero-arguments public constructor.

How do I convert a class to another class in Java?

First convert class A's object to json String and then convert Json string to class B's object. Save this answer.

Is convert a class in C#?

Convert class provides different methods to convert a base data type to another base data type. The base types supported by the Convert class are Boolean, Char, SByte, Byte, Int16, Int32, Int64, UInt16, UInt32, UInt64, Single, Double, Decimal, DateTime, and String.

How can we convert the object of one class to the another class?

Through class conversion, one can assign data that belongs to a particular class type to an object that belongs to another class type. where '=' has been overloaded for objects of class type 'B'. Class conversion can be achieved by conversion function which is done by the use of operator overloading.


2 Answers

There is no class like this in java.

The accepted practice in java is to just cast primitives to each other. This is an easy and consistent way of converting from one type to another.

float bar = 4.0f;
int foo = (int) bar;
like image 105
Erick Robertson Avatar answered Oct 27 '22 20:10

Erick Robertson


You can create your own Convert class easily

package com.abc;

public class Convert {
  public static int ToInt(Object obj) {
    try{
      return Integer.parseInt(obj.toString());
    }
    catch(Exception ex){
      return 0;
    }
  }
  public static float ToFloat(Object obj) {
    try{
      return Float.parseFloat(obj.toString());
    }
    catch(Exception ex){
      return 0f;
    }
  }

  public static boolean ToBoolean(Object obj){
    try{
      if(obj.getClass() == Boolean.class)
        return (Boolean)obj;

      return Boolean.parseBoolean(obj.toString());
    }
    catch(Exception ex){
      return false;
    }
  }
}

The above class passing following unit test:

package com.abc;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class ConvertTest {

  @Test
  public void ConvertToInt() {
    assertEquals(1, Convert.ToInt(1));
    assertEquals(0, Convert.ToInt("Suresh"));
    assertEquals(0, Convert.ToInt(null));
    assertEquals(0, Convert.ToInt(true));
    assertEquals(0, Convert.ToInt(3.3f));
  }

  @SuppressWarnings("deprecation")
  @Test
  public void ConvertToFloat() {
    assertEquals(1f, Convert.ToFloat(1), 0.001f);
    assertEquals(0f, Convert.ToFloat("Suresh"), 0.001f);
    assertEquals(0f, Convert.ToFloat(null), 0.001f);
    assertEquals(0f, Convert.ToFloat(true), 0.001f);
    assertEquals(3.3f, Convert.ToFloat(3.3f), 0.001f);
  }

  @Test
  public void ConvertToBoolean() {
    assertEquals(false, Convert.ToBoolean(1));
    assertEquals(false, Convert.ToBoolean("Suresh"));
    assertEquals(false, Convert.ToBoolean(null));
    assertEquals(true, Convert.ToBoolean(true));
    assertEquals(false, Convert.ToBoolean(false));
    assertEquals(false, Convert.ToBoolean(3.3f));
  }
}
like image 40
Suresh Prajapati Avatar answered Oct 27 '22 21:10

Suresh Prajapati