Java.lang - SPLessons

Java.lang Integer and Character

Home > Lesson > Chapter 8
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.lang Integer and Character

Java.lang Integer - Character

shape Description

The functionality of java.lang.Integer is that to wrap the primitive type value int in an object, this class hire the methods from java.lang.Object class, following is the declaration of this class. [java] public final class Integer extends Number implements Comparable<Integer> [/java] The functionality of java.lang.Character is that to wrap the primitive type value char in an object, this class hire the methods from java.lang.Object class, following is the declaration of this class. [java] public final class Character extends Object implements Serializable, Comparable<Character>[/java]

java.lang Integer Class

shape Methods

Following are the common using methods of java.lang.Integer class.
Methods Description
byteValue() Integer value will be returned as Byte.
decode(String) String will be decoded into an integer.
doubleValue() Integer value will be returned as double.
toHexString(int) To make the integer argument(String) as unsigned number in base 16.

shape Fields

Fields Description
MAX_VALUE To indicate the largest value of int.
MIN_VALUE To indicate the minimum value of int.

shape Constructors

Following are the common using constructors of java.lang.Integer class.
Constructors Description
Integer(int) Develops a recently dispensed Integer object that speaks to the primitive int parameter.
Integer(String) Develops a recently allotted Integer object that indicates to the value spoke to by the string.

shape Example

Following is an example by using java.lang.Integer.toHexString() method. DemoInteger.java [java] package com.SPlessons; import java.lang.*; public class DemoInteger { public static void main(String[] args) { int i = 200; System.out.println("The Number = " + i); System.out.println("Value of Hex = " + Integer.toHexString(i)); } } [/java] To return the string representation of the unsigned integer value represented by the argument in hexadecimal (base 16), where the given value is 200. Output: The result will be as follows. [java] The Number = 200 Value of Hex = c8 [/java]

Following is an example by using java.lang.Integer.decode() method.

Following is the syntax declaration. [java]public static Integer decode(String nm) throws NumberFormatException[/java] DemoInteger.java [java] package com.SPlessons; import java.lang.*; public class DemoInteger { public static void main(String[] args) { Integer i = new Integer(10); String str = "500"; System.out.println("The Number = " + i.decode(str)); } } [/java] The decode(str) method is used to return an Integer object holding the int value represented by string str. Output: The result will be as follows. [java]The Number = 500[/java]

java.lang Character Class

shape Methods

Following are the common using methods of java.lang.character class.
Methods Description
charValue() Value of character object will be returned.
getNumericValue(char) Gives back the Unicode numeric estimation of the character as a positive whole number.
isDigit(char) Figures out whether the predefined character is a digit.
isSpace(char) Figures out whether the predetermined character is ISO-LATIN-1 white space. Belittled.

shape Fields

Following are the common using fields of java.lang.character class.
Fields Description
MAX_VALUE To indicate the largest value of int.
MIN_VALUE To indicate the minimum value of int.
CONNECTOR_PUNCTUATION It is the General case "Pc" in the Unicode determination.
CURRENCY_SYMBOL It is the General case "Sc" in the Unicode determination.

shape Example

Following is an example by using java.lang.Character.charValue() method. DemoCharacter.java [java] package com.SPlessons; public class DemoCharacter { public static void main(String[] args) { // create a Character object c Character c; // assign value to c c = new Character('V'); // create a char primitive ch char ch; // assign primitive value of c to ch ch = c.charValue(); String str = "The primitive char value is " + ch; // print ch value System.out.println( str ); } } [/java] To return the value of character charValue() will be used. Where the developer assigned the character that is V. Output: The result is as follows. [java] The primitive char value is V [/java]

By using java.lang.Character.getNumericValue(char ch) Method

Already discussed the functionality of this method in above table, following is the syntax declaration of this method. [java] public static int getNumericValue(char ch) [/java] DemoCharacter.java [java]package com.SPlessons; public class DemoCharacter { public static void main(String[] args) { // create 2 character primitives ch1, ch2 char ch1, ch2; // assign values to ch1, ch2 ch1 = 'x'; ch2 = '8'; // create 2 int primitives i1, i2 int i1, i2; // assign numeric values of ch1, ch2 to i1, i2 i1 = Character.getNumericValue(ch1); i2 = Character.getNumericValue(ch2); String str1 = "Numeric value of " + ch1 + " is " + i1; String str2 = "Numeric value of " + ch2 + " is " + i2; // print i1, i2 values System.out.println( str1 ); System.out.println( str2 ); } } [/java] The purpose of this method is to give back the int esteem that the predetermined Unicode character represents, here the developer given the character that is x. Output: In the result the character x value will be generated. [java] Numeric value of x is 33 Numeric value of 8 is 8 [/java]

By using java.lang.Character.isDigit(char ch) Method

DemoCharacter.java [java] package com.SPlessons; public class DemoCharacter { public static void main(String[] args) { // create 2 char primitives ch1, ch2 char ch1, ch2; // assign values to ch1, ch2 ch1 = '7'; ch2 = 'h'; // create 2 boolean primitives b1, b2 boolean b1, b2; // assign isDigit results of ch1, ch2 to b1, b2 b1 = Character.isDigit(ch1); b2 = Character.isDigit(ch2); String str1 = ch1 + " is a digit is " + b1; String str2 = ch2 + " is a digit is " + b2; // print b1, b2 values System.out.println( str1 ); System.out.println( str2 ); } } [/java] The functionality of this method is to know whether given number is a digit or not. if it is digit then it returns true otherwise, returns false. Output: The result will be as follows. where in code the developer is given number 7 so it is a digit. [java] 9 is a digit is true V is a digit is false[/java]

Summary

shape Key Points

  • The getType(char) method is used to give back a value showing a character classification.
  • The static int MIN_RADIX is the field of character class.
  • These both classes will hire the methods from java.lang.Object.