Chapter 2 - Java Conversion
Chapter 2 - Java Conversion
Signature
The parseInt() is the static method of Integer class. The signature of parseInt() method is given
below:
int i=Integer.parseInt("200");
200
1. Output:
2. 200100
3. 300
1) String.valueOf()
The String.valueOf() method converts int to String. The valueOf() is the static method of String
class. The signature of valueOf() method is given below:
Output:
300
200100
2) Integer.toString()
The Integer.toString() method converts int to String. The toString() is the static method of
Integer class. The signature of toString() method is given below:
Let's see the simple code to convert int to String in java using Integer.toString() method.
int i=10;
String s=Integer.toString(i);//Now it will return "10"
Output:
300
200100
3) String.format()
The String.format() method is used to format given arguments into String. It is introduced since
Jdk 1.5.
Output:
200
Java String to long
We can convert String to long in java using Long.parseLong() method.
Signature
The parseLong() is the static method of Long class. The signature of parseLong() method is
given below:
Output:
9990449935
We can convert long to String in java using String.valueOf() and Long.toString() methods.
It is used if we have to display long number in textfield in GUI application because everything is
displayed as a string in form.
1) String.valueOf()
The String.valueOf() is an overloaded method. It can be used to convert long to String. The
valueOf() is the static method of String class. The signature of valueOf() method is given below:
Output:
9993939399
2) Long.toString()
The Long.toString() method converts long to String. The toString() is the static method of Long
class. The signature of toString() method is given below:
Output:
9993939399
We can convert String to Date in java using parse() method of DateFormat and
SimpleDateFormat classes.
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateExample1 {
public static void main(String[] args)throws Exception {
String sDate1="31/12/1998";
Date date1=new SimpleDateFormat("dd/MM/yyyy").parse(sDate1);
System.out.println(sDate1+"\t"+date1);
}
}
Output:
Output:
31/12/1998 Thu Dec 31 00:00:00 IST 1998
31-Dec-1998 Thu Dec 31 00:00:00 IST 1998
12 31, 1998 Thu Dec 31 00:00:00 IST 1998
Thu, Dec 31 1998 Thu Dec 31 00:00:00 IST 1998
Thu, Dec 31 1998 23:37:50 Thu Dec 31 23:37:50 IST 1998
31-Dec-1998 23:37:50 Thu Dec 31 23:37:50 IST 1998
The format() method of DateFormat class is used to convert Date into String. DateFormat is
an abstract class. The child class of DateFormat is SimpleDateFormat. It is the
implementation of DateFormat class. The signature of format() method is given below:
String format(Date d)
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
public class DateToStringExample1 {
public static void main(String args[])
{
Date date = Calendar.getInstance().getTime();
DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
String strDate = dateFormat.format(date);
System.out.println("Converted String: " + strDate);
}
}
Output:
Output:
The charAt() method returns a single character only. To get all characters, you can use loop.
Signature
The charAt() method returns a single character of specified index. The signature of charAt()
method is given below:
Output:
Output:
Output:
Output:
We can convert char to String in java using String.valueOf(char) method of String class
and Character.toString(char) method of Character class.
char c='S';
String s=String.valueOf(c);
public class CharToStringExample1{
public static void main(String args[]){
char c='S';
String s=String.valueOf(c);
System.out.println("String is: "+s);
}}
Output:.
String is: S
Output:
String is: M
We can convert String to Object in java with assignment operator. Each class is internally a
child class of Object class. So you can assign string to Object directly.
You can also convert String to Class type object using Class.forName() method.
String s="hello";
Object obj=s;
Output:
hello
code to convert String to Class object in java using Class.forName() method. The
Class.forName() method returns the instance of Class class which can be used to get the
metadata of any class.
Output:
We can convert Object to String in java using toString() method of Object class or
String.valueOf(object) method.
convert any object to String in java whether it is user-defined class, StringBuilder, StringBuffer
or anything else.
Here, we are going to see two examples of converting Object into String. In the first example, we
are going to convert Emp class object into String which is an user-defined class. In second
example, we are going to convert StringBuilder to String.
Output:
Emp@2a139a55
Emp@2a139a55
Output:
So, you can convert any Object to String in java using toString() or String.valueOf(object)
methods.
Java Convert int to long
We can convert int to long in java using assignment operator. There is nothing to do
extra because lower type can be converted to higher type implicitly.
Output:
200
We can convert int value to Long object by instantiating Long class or calling Long.valueOf()
method.
Output:
100
100
We can convert long to int in java using typecasting. To convert higher data type into lower, we
need to perform typecasting.
Here, we are going to learn how to convert long primitive type into int and Long object into int.
Output:
500
Output:
10
We can convert char to int in java using various ways. If we direct assign char variable to int, it
will return ASCII value of given character.
If char variable contains int value, we can get the int value by
calling Character.getNumericValue(char) method. Alternatively, we can use
String.valueOf(char) method.
Output:
97
49
code to convert char to int in java using Character.getNumericValue(char) method which returns
an integer value.
Output:
another example which returns integer value of specified char value using String.valueOf(char)
method.
convert int to char in java using typecasting. To convert higher data type into lower, we need to
perform typecasting. Here, the ASCII character of integer value will be stored in the char
variable.
To get the actual value in char variable, you can add '0' with int variable. Alternatively, you can
use Character.forDigit() method.
Output:
But if you store 1, it will store ASCII character of given number which is start of heading which
is not printable. So it will not print anything on the console.
public class IntToCharExample2{
public static void main(String args[]){
int a=1;
char c=(char)a;
System.out.println(c);
}}
Output:
If you add '0' with int variable, it will return actual value in the char variable. The ASCII value of
'0' is 48. So, if you add 1 with 48, it becomes 49 which is equal to 1. The ASCII character of 49
is 1.
Output:
If you store integer value in a single quote, it will store actual character in char variable.
Output:
1
Java int to char Example: Character.forDigit()
To get the actual value, you can also use Character.forDigit() method.
To convert String into Boolean object, we can use Boolean.valueOf(string) method which returns
instance of Boolean class.
To get boolean true, string must contain "true". Here, case is ignored. So, "true" or "TRUE" will
return boolean true. Any other string value except "true" returns boolean false.
The parseBoolean() is the static method of Boolean class. The signature of parseBoolean()
method is given below:
Output:
true
true
false
The Boolean.valueOf() method converts string into Boolean object. Let's see the simple code to
convert String to Boolean in java.
Alternatively, we can use Boolean.toString(boolean) method which also converts boolean into
String.
1) String.valueOf()
The String.valueOf() method converts boolean to String. The valueOf() is the static method of
String class. The signature of valueOf() method is given below:
Output:
true
false
2) Boolean.toString()
The Boolean.toString() method converts boolean to String. The toString() is the static method of
Boolean class. The signature of toString() method is given below:
The constructor of Timestamp class receives long value as an argument. So you need to convert
date into long value using getTime() method of java.util.Date class.
You can also format the output of Timestamp using java.text.SimpleDateFormat class.
1. Timestamp(long l)
Output:
2017-11-02 01:59:30.274
The constructor of Date class receives long value as an argument. So, you need to convert
Timestamp object into long value using getTime() method of java.sql.Timestamp class.
1. Date(long l)
import java.sql.Timestamp;
import java.util.Date;
public class TimestampToDateExample1 {
public static void main(String args[]){
Timestamp ts=new Timestamp(System.currentTimeMillis());
Date date=new Date(ts.getTime());
System.out.println(date);
}
}
Output:
We can convert binary to decimal in java using Integer.parseInt() method or custom logic.
The Integer.parseInt() method converts string to int with given redix. The signature of parseInt()
method is given below:
Output:
10
public class BinaryToDecimalExample2{
public static void main(String args[]){
System.out.println(Integer.parseInt("1010",2));
System.out.println(Integer.parseInt("10101",2));
System.out.println(Integer.parseInt("11111",2));
}}
Output:
10
21
31
Java Convert Decimal to Binary
Output:
1010
10101
11111
The Integer.parseInt() method converts string to int with given redix. The signature of parseInt()
method is given below:
Output:
10
Output:
10
15
289
Output:.
a
f
121
The Integer.parseInt() method converts a string to an int with the given radix. If you pass 8 as a
radix, it converts an octal string into decimal. Let us see the signature of parseInt() method:
Output:
81
another examp//Shorthand example of Integer.parseInt() method
public class OctalToDecimalExample2{
public static void main(String args[]){
System.out.println(Integer.parseInt("121",8));
System.out.println(Integer.parseInt("23",8));
System.out.println(Integer.parseInt("10",8));
}}
81
19
8
Output:
10
23
121