- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9321 Articles for Object Oriented Programming
300 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
25K+ 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
290 Views
To convert a String to int in Java, we can use the two built-in methods namely parseInt() and valueOf(). These static methods belong to the Integer class of java.lang package and throws a NumberFormatException if the string is not a valid representation of an integer. In Java, String is a class of 'java.lang' package that stores a series of characters enclosed within double quotes. And, an integer is a primitive datatype that stores numerical values. In this article, we will discuss a few Java programs that illustrate how to convert a given String to integer. Java Program to Convert a ... Read More
10K+ Views
To check two numbers for equality in Java, we can use the Equals() method as well as the == operator.Firstly, let us set Integers.Integer val1 = new Integer(5); Integer val2 = new Integer(5);Now, to check whether they are equal or not, let us use the == operator.(val1 == val2)Let us now see the complete example.Example Live Demopublic class Demo { public static void main( String args[] ) { Integer val1 = new Integer(5); Integer val2 = new Integer(5); Integer val3 = new Integer(10); System.out.println("Integer 1 = "+val1); ... Read More
8K+ Views
The Equals() method compares this object to the specified object. The result is true if and only if the argument is not null and is an Integer object that contains the same int value as this object.Let us first set Integer object.Integer val1 = new Integer(30); Integer val2 = new Integer(60); Integer val3 = new Integer(55); Integer val4 = new Integer(30);Now let us check their equality using the Equals() method.val1.equals(val2);In the same way, check it for different Integers.Let us see the complete example.Example Live Demoimport java.util.Random; public class Demo { public static void main( String args[] ) { ... Read More
2K+ Views
To generate random numbers in Java, use.import java.util.Random;Now, take Random class and create an object.Random num = new Random();Now, in a loop, use the nextInt() method since it is used to get the next random integer value. You can also set a range, like for 0 to 20, write it as.nextInt( 20 );Let us see the complete example wherein the range is 1 to 10.Example Live Demoimport java.util.Random; public class Demo { public static void main( String args[] ) { Random num = new Random(); int res; for ( int i = 1; i
18K+ Views
To read integers from console, use Scanner class.Scanner myInput = new Scanner( System.in );Allow a use to add an integer using the nextInt() method.System.out.print( "Enter first integer: " ); int a = myInput.nextInt();In the same way, take another input in a new variable.System.out.print( "Enter second integer: " ); Int b = myInput.nextInt();Let us see the complete example.Exampleimport java.util.Scanner; public class Demo { public static void main( String args[] ) { Scanner myInput = new Scanner( System.in ); int a; int b; int sum; System.out.print( ... Read More
10K+ Views
To convert Boolean to String in Java, use the toString() method. For this, firstly, we have declared two booleans.boolean bool1 = false; boolean bool2 = true;Now, convert Boolean to String using the toString() method in Java as shown below −String str1 = new Boolean(bool1).toString(); String str2 = new Boolean(bool2).toString();Now, when you will display the values “str1” and “str2”, the output would be in String.Let us now see the complete example to convert Boolean to String.Example Live Demopublic class Demo { public static void main(String[] args) { boolean bool1 = false; boolean bool2 = true; ... Read More
15K+ Views
To convert String to Boolean, use the parseBoolean() method in Java. The parseBoolean() parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".Firstly, declare a string.String str = "false";Now, use the Boolean.parseBoolean() method to convert the above declared String to Boolean.boolean bool = Boolean.parseBoolean(str);Let us now see the complete example to learn how to convert String to Boolean.Example: Convert String to Boolean Live Demopublic class Demo { public static void main(String[] args) { // string ... Read More
2K+ Views
To create a Boolean object from Boolean value is quite easy. Create an object with Boolean and set the Boolean value “true” or “false”, which are the Boolean literals.Let us now see how “true” value is added.Boolean bool = new Boolean("true");In the same way, “False” value is added.Boolean bool = new Boolean("false");The following is an example displaying how to create a Boolean object from Boolean value.Example Live Demopublic class Demo { public static void main(String[] args) { Boolean bool = new Boolean("true"); System.out.println(bool); bool = new Boolean("false"); System.out.println(bool); ... Read More