Category: 11. Java.lang Package

https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSbO_2QlUN_cTndkQ-yaopqw-cft_dSoUbQng&s

  • UnicodeBlock Class

    The Java Character.UnicodeBlock class is a family of character subsets representing the character blocks in the Unicode specification. Character blocks generally define characters used for a specific script or purpose.

    Class Declaration

    Following is the declaration for java.lang.Character.UnicodeBlock class −

    publicstaticfinalclassCharacter.UnicodeBlockextendsCharacter.Subset

    Class Methods

    Sr.No.Method & Description
    1forName()This method returns the UnicodeBlock with the given name.
    2of()This method returns the object representing the Unicode block containing the given character, or null if the character is not a member of a defined block.
    2of(int codePoint)This method returns the object representing the Unicode block containing the given character, or null if the character is not a member of a defined block.

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Getting UnicodeBlock using Block Names Example

    The following example shows the usage of Java Character.UnicodeBlock forName() method.

    Open Compiler

    packagecom.tutorialspoint;publicclassCharacterUnicodeBlockDemo{publicstaticvoidmain(String[] args){// returns the UnicodeBlock instance with blockName "BASIC_LATIN"System.out.println(Character.UnicodeBlock.forName("BASIC_LATIN"));// returns the UnicodeBlock instance with blockName "BasicLatin"System.out.println(Character.UnicodeBlock.forName("BasicLatin"));// returns the UnicodeBlock instance with specified blockName System.out.println(Character.UnicodeBlock.forName("ARABIC"));System.out.println(Character.UnicodeBlock.forName("MUSICALSYMBOLS"));System.out.println(Character.UnicodeBlock.forName("TAMIL"));}}

    Let us compile and run the above program, this will produce the following result −

    BASIC_LATIN
    BASIC_LATIN
    ARABIC
    MUSICAL_SYMBOLS
    TAMIL
    
  • Subset Class

    The java.lang.Character.Subset class instances represent particular subsets of the Unicode character set. The only family of subsets defined in the Character class is UnicodeBlock.

    Class Declaration

    Following is the declaration for java.lang.Character.Subset class −

    publicstaticclassCharacter.SubsetextendsObject

    Class Constructors

    Sr.No.Constructor & Description
    1protected Character.Subset(String name)This constructs a new Subset instance.

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Class Methods

    Sr.No.Method & Description
    1equals()This method compares two Subset objects for equality.
    2hashCode()This method returns the standard hash code as defined by the Object.hashCode() method.
    3toString()This method returns the name of this subset.

    Comparing Subset objects for Equality Example

    In the following example, we instantiate CharacterSubsetDemo class objects. The method Java Character.Subset equals() is then called on these objects to check whether they are equal or not.

    Open Compiler

    packagecom.tutorialspoint;publicclassCharacterSubsetDemoextendsCharacter.Subset{// constructor of super classCharacterSubsetDemo(String s){super(s);// invokes the immediate parent class: Object}publicstaticvoidmain(String[] args){CharacterSubsetDemo obj1 =newCharacterSubsetDemo("admin");CharacterSubsetDemo obj2 =newCharacterSubsetDemo("webmaster");CharacterSubsetDemo obj3 =newCharacterSubsetDemo("administrator");// compares Subset objects for equalityboolean retval = obj1.equals(obj1);System.out.println("Object obj1 is equal to obj1 ? "+ retval);
    
      retval = obj2.equals(obj1);System.out.println("Object obj1 is equal to obj2 ? "+ retval);
      retval = obj3.equals(obj1);System.out.println("Object obj1 is equal to obj3 ? "+ retval);}}</code></pre>

    Output

    Let us compile and run the above program, this will produce the following result −

    Object obj1 is equal to obj1 ? true
    Object obj1 is equal to obj2 ? false
    Object obj1 is equal to obj3 ? false
    
  • Byte class with Examples

    Introduction

    The Java Byte class class wraps a value of primitive type byte in an object. An object of type Byte contains a single field whose type is byte.

    Class Declaration

    Following is the declaration for java.lang.Byte class −

    publicfinalclassByteextendsNumberimplementsComparable<Byte>

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Field

    Following are the fields for java.lang.Byte class −

    • static byte MAX_VALUE − This is constant holding the maximum value a byte can have, 27-1.
    • static byte MIN_VALUE − This is constant holding the minimum value a byte can have, -27.
    • static int SIZE − This is the number of bits used to represent a byte value in two’s complement binary form.
    • static Class<Byte> TYPE − This is the Class instance representing the primitive type byte.

    Class constructors

    Sr.No.Constructor & Description
    1Byte(byte value)This constructs a newly allocated Byte object that represents the specified byte value.
    2Byte(String s)This constructs a newly allocated Byte object that represents the byte value indicated by the String parameter.

    Class methods

    Sr.No.Method & Description
    1byte byteValue()This method returns the value of this Byte as a byte.
    2int compareTo(Byte anotherByte)This method compares two Byte objects numerically.
    3static Byte decode(String nm)This method decodes a String into a Byte.
    4double doubleValue()This method returns the value of this Byte as a double.
    5boolean equals(Object obj)This method compares this object to the specified object.
    6float floatValue()This method returns the value of this Byte as a float.
    7int hashCode()This method returns a hash code for this Byte.
    8int intValue()This method returns the value of this Byte as an int.
    9long longValue()This method returns the value of this Byte as a long.
    10static byte parseByte(String s)This method parses the string argument as a signed decimal byte.
    11static byte parseByte(String s, int radix)This method parses the string argument as a signed byte in the radix specified by the second argument.
    12short shortValue()This method returns the value of this Byte as a short.
    13String toString()This method returns a String object representing this Byte’s value.
    14static String toString(byte b)This method returns a new String object representing the specified byte.
    15static Byte valueOf(byte b)This method returns a Byte instance representing the specified byte value.
    16static Byte valueOf(String s)This method returns a Byte object holding the value given by the specified String.
    17static Byte valueOf(String s, int radix)This method returns a Byte object holding the value extracted from the specified String when parsed with the radix given by the second argument.

    Methods inherited

    This class inherits methods from the following classes −

    • java.lang.Object

    Example

    The following example shows the usage of Byte class to get byte from a string.

    Open Compiler

    packagecom.tutorialspoint;publicclassByteDemo{publicstaticvoidmain(String[] args){// create a String s and assign value to itString s ="+120";// create a Byte object bByte b;// get the value of byte from string
    
      b =Byte.valueOf(s);// print the valueSystem.out.println("Byte value of string "+ s +" is "+ b );}}</code></pre>

    Let us compile and run the above program, this will produce the following result −

    Byte value of string +120 is 120