
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 7442 Articles for Java

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

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

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

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

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

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

11K+ 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

10K+ 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

3K+ 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

20K+ 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