Showing posts with label integer. Show all posts
Showing posts with label integer. Show all posts

Friday, 2 September 2022

Convert an integer to string in Java

In this post, I am going to explain different ways to convert an Integer object to String in Java.

 

Approach 1: Using toString() method.

String str1 = i1.toString();

 

Approach 2: Using String.valueOf method.

String str2 = String.valueOf(i1);

 

Approach 3: Concatenate int value to an empty string.

String str3 = i1.intValue()+"";

 

Approach 4: Using StringBuilder.

String str4 = new StringBuilder().append(i1).toString();

 

Approach 5: Using StringBuffer.

String str5 = new StringBuffer().append(i1).toString();

 

Find the below working application.

 

IntegerToStringDemo.java

package com.sample.app.strings;

public class IntegerToStringDemo {

    public static void main(String[] args) {
        Integer i1 = 234;

        String str1 = i1.toString();
        String str2 = String.valueOf(i1);
        String str3 = i1.intValue() + "";

        String str4 = new StringBuilder().append(i1).toString();
        String str5 = new StringBuffer().append(i1).toString();

        System.out.println("str1 : " + str1);
        System.out.println("str2 : " + str2);
        System.out.println("str3 : " + str3);
        System.out.println("str4 : " + str4);
        System.out.println("str5 : " + str5);
    }

}

 


Output

str1 : 234
str2 : 234
str3 : 234
str4 : 234
str5 : 234

 

 

Previous                                                 Next                                                 Home

Saturday, 15 May 2021

Jq: Convert integer to a string

‘jq’ command provides toString function that prints given input as string.

 

emps.json

emps.json 
[{
		"id": 1,
		"firstName": "Ram",
		"lastName": "Gurram",
		"age": 23
	},
	{
		"id": 2,
		"firstName": "Sailaja",
		"lastName": "PTR",
		"age": 31
	},
	{
		"id": 3,
		"firstName": "Venkat",
		"lastName": "IT",
		"age": 30
	},
	{
		"id": 4,
		"firstName": "Gopi",
		"lastName": "Battu",
		"age": 34
	}
]

 

a. Get all the employees firstName and lastName whose id is even number

$cat emps.json | jq '.[] | select((.id % 2 == 0)) | .firstName+","+.lastName+"," +(.id|tostring)'
"Sailaja,PTR,2"
"Gopi,Battu,4"

 

b. Get all the employees firstName and lastName whose id is odd number

$cat emps.json | jq '.[] | select((.id % 2 != 0)) | .firstName+","+.lastName+"," +(.id|tostring)'
"Ram,Gurram,1"
"Venkat,IT,3"

 

 

 

 

 

 

Previous                                                    Next                                                    Home

Sunday, 29 March 2020

How Java handles Integer overflows and underflows?

What is Integer Overflow?
Integer Overflow occurs when the result of an arithmetic operation (like Addition, Subtraction, Division, Multiplication etc.,), exceeds the maximum size of the integer type used to store it.

When integer overflows, it goes back to the minimum value and continues from there.

App.java
package com.sample.app;

public class App {

 public static void main(String args[]) {
  int number = Integer.MAX_VALUE;

  for (int i = 0; i < 10; i++) {
   System.out.println(number++);
  }
 }

}

Output
2147483647
-2147483648
-2147483647
-2147483646
-2147483645
-2147483644
-2147483643
-2147483642
-2147483641
-2147483640

What is Integer Underflow?
Integer Underflow occurs when the result of an arithmetic operation (like Addition, Subtraction, Division, Multiplication etc.,), exceeds the minimum size of the integer type used to store it.


When an integer underflows, it goes back to the maximum value and continues from there.

App.java
package com.sample.app;

public class App {

 public static void main(String args[]) {
  int number = Integer.MIN_VALUE;

  for (int i = 0; i < 10; i++) {
   System.out.println(number--);
  }
 }

}

Output

-2147483648
2147483647
2147483646
2147483645
2147483644
2147483643
2147483642
2147483641
2147483640
2147483639


You may like

Thursday, 26 March 2020

Convert a char to integer

Approach 1: Using Character.getNumericValue method.
int number1 = Character.getNumericValue(ch);

Approach 2: Subtract the character '0' from given character.
int number2 = (ch - '0');

Approach 3: Subtract 48 (It is the unicode value for 0) from given character.
int number3 = (ch - 48);

App.java
package com.sample.app;

public class App {

 public static void main(String args[]) {
  char ch = '3';

  int number1 = Character.getNumericValue(ch);

  int number2 = (ch - '0');
  
  int number3 = (ch - 48);
  
  System.out.println("number1 : " + number1);
  System.out.println("number2 : " + number2);
  System.out.println("number3 : " + number3);

 }

}

Output
number1 : 3
number2 : 3
number3 : 3



You may like

Wednesday, 11 March 2020

Integer vs int

a. int is a primitive type, whereas Integer is a reference type. More specifically Integer is a wrapper type of int type. Every primitive type in Java has corresponding wrapper type.
         byte has Byte
         short has Short
         int has Integer
         long has Long
         float has Float
         double has Double
         boolean has Boolean
         char has Character

b. Default value of int field is 0, whereas default value of Integer is null.

DemoClass.java
package com.sample.app;

public class DemoClass {
 int primitiveField;
 Integer objField;
}

App.java
package com.sample.app;

public class App {

 public static void main(String[] args) {
  DemoClass demoClass = new DemoClass();

  System.out.println("primitiveVariable : " + demoClass.primitiveField);
  System.out.println("objVariable : " + demoClass.objField);
 }

}

Output
primitiveVariable : 0
objVariable : null

c. By default int variables are mutable (unless you define it with via final modifier), whereas Integer variables are immutable.

d. Since Integer is a reference type, you can use it as a type in collections, but you can’t use int as a type in collecitons.

Example
List<Integer> list = new ArrayList<> ();
Map<Integer, String> map = new HashMap<> ();

e. Integer can allow null values, but int do not allow.

f. Performing Arithmetic operations on ‘int’ are always faster than ‘Integer’.

g. == operator is used to check equality of two ‘int’ variables, where as ‘equals’ method is used to check whether two integer objects has same int value or not.

h. Since ‘int’ is a primitive type, no methods are associated with it. Whereas Integer is a class, methods are associated with it.

Example
Integer i1 = new Integer(10);
i1.intValue();
i1.toString();

You may like

Sunday, 5 January 2020

How to print integer in hex format?

‘Integer.toHexString’ method is used to convert an integer to hex string.

Example
int i = 1027;
String hexString = Integer.toHexString(i);

App.java
package com.sample.app;

public class App {

 public static void main(String args[]) {
  int i = 1027;

  String hexString = Integer.toHexString(i);
  
  System.out.println("Hexa form of " + i + " is " + hexString);

 }
}

Output
Hexa form of 1027 is 403

You may like