Found 9150 Articles for Object Oriented Programming

Java Program to convert boolean value to Boolean

Samual Sam
Updated on 26-Jun-2020 10:48:25

5K+ Views

Use the valueOf() method to convert boolean value to Boolean. Firstly, let us take a boolean value.boolean val = false;Now, to convert it to Boolean object, use the valueOf() method.Boolean res = Boolean.valueOf(val);Let us now see the complete example to convert boolean value to Boolean.Example Live Demopublic class Demo {    public static void main(String[] args) {       boolean val = false;       System.out.println("Boolean Value = "+val);       Boolean res = Boolean.valueOf(val);       System.out.println("Boolean = " + res);       if (res.equals(Boolean.TRUE)) {          System.out.println("Boolean = " + res); ... Read More

Java Program to convert boolean to integer

karthikeya Boyini
Updated on 25-Jun-2024 14:39:45

13K+ Views

To convert boolean to integer, let us first declare a variable of boolean primitive. boolean bool = true; Now, to convert it to integer, let us now take an integer variable and return a value “1” for “true” and “0” for “false”. int val = (bool) ? 1 : 0; Let us now see the complete example to convert boolean to integer in Java. Example public class Demo {    public static void main(String[] args) {       // boolean       boolean bool = true;       System.out.println("Boolean Value: "+bool);       int val = (bool) ? 1 : 0;       // Integer       System.out.println("Integer value: "+val);    } }OutputBoolean Value: true Integer value: 1

Convert string to a number in Java

Samual Sam
Updated on 26-Jun-2020 10:49:12

2K+ Views

To convert a string to a number in Java, use the Integer.parseInt() method. First, let use create a String and set value.String str = "45";Now, take an Integer and use the Integer.parseInt() method.Integer i = Integer.parseInt(str);Let us see the complete example.Example Live Demopublic class Demo {    public static void main( String args[] ) {       String str = "45";       Integer i = Integer.parseInt(str);       System.out.println("Num: " + i);    } }OutputNum: 45

How to convert Decimal to Hexadecimal in Java

Samual Sam
Updated on 26-Jun-2020 10:50:01

6K+ Views

To convert decimal to hexadecimal, use any of the two methods i.e.Integer.toHexString() − It returns a string representation of the integer argument as an unsigned integer in base 16.Integer.parseInt() − It allows you to set the radix as well, for example, for hexadecimal set it as 16.Let us see an example now to convert decimal to hexadecimal using Integer.toHexString() method.Example Live Demopublic class Demo {    public static void main( String args[] ) {       int dec = 158;       System.out.println(Integer.toHexString(dec));    } }Output9eLet us see an example now to convert decimal to hexadecimal using Integer.parseInt() method.Example Live ... Read More

Java Program to convert decimal integer to hexadecimal number

Samual Sam
Updated on 26-Jun-2020 10:41:08

397 Views

Use the toHexString() method to convert decimal to hexadecimal. The method returns a string representation of the integer argument as an unsigned integer in base 16. The following characters are used as hexadecimal digits:0123456789abcdef.The following is the syntax.String toHexString(int i)It has only single parameter.i − This is an integer to be converted to a string.Example Live Demopublic class Demo {    public static void main( String args[] ) {       int dec = 45;       System.out.println("Decimal = "+dec);       // converting to hex       System.out.println(Integer.toHexString(dec));    } }OutputDecimal = 45 2d

Java program to add integers and check for overflow

karthikeya Boyini
Updated on 18-Sep-2024 21:55:48

1K+ Views

In this article, we will add integers and check for overflow using Java. To check for Integer overflow, we need to check the Integer.MAX_VALUE with the added integers result. Here, Integer.MAX_VALUE is the maximum value of an integer in Java. Let us see an example wherein integers are added and if the sum is more than the Integer.MAX_VALUE, then an exception is thrown. Problem Statement Write a Java program to add integers and check for overflow − Input a = 9897988 b = 8798798 Output Value1: 9897988Value2: 8798798Sum: 18696786 Steps to add integers and check for overflow Following are the steps to ... Read More

Java Program to convert octal number to decimal number

Samual Sam
Updated on 26-Jun-2020 10:42:10

324 Views

Use the parseInt() method with the second parameter as 8 since it is the radix value. The parseInt() method has the following two forms.static int parseInt(String s) static int parseInt(String s, int radix)To convert octal to decimal, use the 2nd syntax and add radix as 8, since octal radix is 8.Integer.parseInt("25", 8)The following is an example.Example Live Demopublic class Demo {    public static void main( String args[] ) {       // converting to decimal       System.out.println(Integer.parseInt("25", 8));    } }Output21

Java Program to convert hexadecimal number to decimal number

karthikeya Boyini
Updated on 26-Jun-2020 10:42:43

439 Views

Use the parseInt() method with the second parameter as 16 since it is the radix value. The parseInt() method has the following two forms.static int parseInt(String s) static int parseInt(String s, int radix)To convert hex string to decimal, use the 2nd syntax and add radix as 16, since hexadecimal radix is 16.Integer.parseInt("12", 16)Example Live Demopublic class Demo {    public static void main( String args[] ) {       // converting to decimal       System.out.println(Integer.parseInt("444", 16));    } }Output1092

Concatenate string to an int value in Java

Samual Sam
Updated on 22-Oct-2023 03:25:06

30K+ Views

To concatenate a string to an int value, use the concatenation operator.Here is our int.int val = 3;Now, to concatenate a string, you need to declare a string and use the + operator.String str = "Demo" + val;Let us now see another example.Example Live Demoimport java.util.Random; public class Demo {    public static void main( String args[] ) {       int val = 3;       String str = "" + val;       System.out.println(str + " = Rank ");    } }Output3 = Rank

Java Program to convert a String to int

Shriansh Kumar
Updated on 17-Apr-2025 10:42:52

478 Views

For a given String , our task is to write a Java program that converts it into an Integer. In Java, String is a class in java.lang package that stores a series of characters enclosed within double quotes. Integer is a primitive datatype that stores numerical values. Converting a String to int in Java We can use the following methods to convert a string into an integer in Java: Integer.parseInt() Integer.valueOf() Let's discuss these methods one by one with the help of example programs. Using Integer.parseInt() The Integer.parseInt() is a static method that belongs to the Integer class ... Read More

Advertisements