-
Notifications
You must be signed in to change notification settings - Fork 1
Java.lang.Integer class
Java.lang.Integer class in Java geeksforgeeks.org
Integer class is a wrapper class for the primitive type int which contains several methods to effectively deal with a int value like converting it to a string representation, and vice-versa. An object of Integer class can hold a single int value.
-
Constructors:
- Integer(int b): Creates a Integer object initialized with the value provided.
Syntax : public Integer(int b) Parameters : b : value with which to initialize
- Integer(String s): Creates a Integer object initialized with the int value provided by string representation.
Defalut radix is taken to be 10. Syntax : public Integer(String s) throws NumberFormatException Parameters : s : string representation of the int value Throws : NumberFormatException : If the string provided does not represent any int value.
- Integer(int b): Creates a Integer object initialized with the value provided.
-
Methods:
-
toString()
: Returns the string corresponding to the int value. -
toHexString()
: Returns the string corresponding to the int value in hexadecimal form, that is it returns a string -
toOctalString()
: Returns the string corresponding to the int value in octal form, that is it returns a string -
toBinaryString()
: Returns the string corresponding to the int value in binary digits, that is it returns a string representing the int value in hex characters-[0/1] -
valueOf()
: returns the Integer object initialised with the value provided.-
valueOf(String val,int radix)
: Another overloaded function which provides function similar to new -
valueOf(String val)
: Another overloaded function which provides function similar to
-
-
parseInt()
: returns int value by parsing the string in radix provided. Differs from valueOf() as it returns a -
getInteger()
: returns the Integer object representing the value associated with the given system property or null if it does not exist. -
decode()
: returns a Integer object holding the decoded value of string provided. String provided must be of the following form else NumberFormatException will be thrown- Decimal- (Sign)Decimal_Number -
rotateLeft()
: Returns a primitive int by rotating the bits left by given distance in two’s complement form of the value given. When rotating left, the most significant bit is moved to the right hand side, or least significant position i.e. cyclic movement of bits takes place. Negative distance signifies right rotation. -
rotateRight()
: Returns a primitive int by rotating the bits right by given distance in the twos complement form of the value given. When rotating right, the least significant bit is moved to the left hand side, or most significant position i.e. cyclic movement of bits takes place. Negative distance signifies left rotation.
-
//Java program to illustrate
//various Integer methods
public class Integer_test
{
public static void main(String args[])
{
int b = 55;
String bb = "45";
// Construct two Integer objects
Integer x = new Integer(b);
Integer y = new Integer(bb);
// toString()
System.out.println("toString(b) = " + Integer.toString(b));
// toHexString(),toOctalString(),toBinaryString()
// converts into hexadecimal, octal and binary forms.
System.out.println("toHexString(b) =" + Integer.toHexString(b));
System.out.println("toOctalString(b) =" + Integer.toOctalString(b));
System.out.println("toBinaryString(b) =" + Integer.toBinaryString(b));
// valueOf(): return Integer object
// an overloaded method takes radix as well.
Integer z = Integer.valueOf(b);
System.out.println("valueOf(b) = " + z);
z = Integer.valueOf(bb);
System.out.println("ValueOf(bb) = " + z);
z = Integer.valueOf(bb, 6);
System.out.println("ValueOf(bb,6) = " + z);
// parseInt(): return primitive int value
// an overloaded method takes radix as well
int zz = Integer.parseInt(bb);
System.out.println("parseInt(bb) = " + zz);
zz = Integer.parseInt(bb, 6);
System.out.println("parseInt(bb,6) = " + zz);
// getInteger(): can be used to retrieve
// int value of system property
int prop = Integer.getInteger("sun.arch.data.model");
System.out.println("getInteger(sun.arch.data.model) = " + prop);
System.out.println("getInteger(abcd) =" + Integer.getInteger("abcd"));
// an overloaded getInteger() method
// which return default value if property not found.
System.out.println("getInteger(abcd,10) =" + Integer.getInteger("abcd", 10));
// decode() : decodes the hex,octal and decimal
// string to corresponding int values.
String decimal = "45";
String octal = "005";
String hex = "0x0f";
Integer dec = Integer.decode(decimal);
System.out.println("decode(45) = " + dec);
dec = Integer.decode(octal);
System.out.println("decode(005) = " + dec);
dec = Integer.decode(hex);
System.out.println("decode(0x0f) = " + dec);
//rotateLeft and rotateRight can be used
//to rotate bits by specified distance
int valrot = 2;
System.out.println("rotateLeft(0000 0000 0000 0010 , 2) =" +
Integer.rotateLeft(valrot, 2));
System.out.println("rotateRight(0000 0000 0000 0010,3) =" +
Integer.rotateRight(valrot, 3));
}
}
Output :
toString(b) = 55
toHexString(b) =37
toOctalString(b) =67
toBinaryString(b) =110111
valueOf(b) = 55
ValueOf(bb) = 45
ValueOf(bb,6) = 29
parseInt(bb) = 45
parseInt(bb,6) = 29
getInteger(sun.arch.data.model) = 64
getInteger(abcd) =null
getInteger(abcd,10) =10
decode(45) = 45
decode(005) = 5
decode(0x0f) = 15
rotateLeft(0000 0000 0000 0010 , 2) =8
rotateRight(0000 0000 0000 0010,3) =1073741824
Some more Integer class methods are –
- byteValue() : returns a byte value corresponding to this Integer Object.
- shortValue() : returns a short value corresponding to this Integer Object.
- intValue() : returns a int value corresponding to this Integer Object.
- longValue() : returns a long value corresponding to this Integer Object.
- doubleValue() : returns a double value corresponding to this Integer Object.
- floatValue() : returns a float value corresponding to this Integer Object.
- hashCode() : returns the hashcode corresponding to this Integer Object.
- bitcount() : Returns number of set bits in twos complement of the integer given.
In a class-based object-oriented language, in general, state is carried by instances, methods are carried by classes, and inheritance is only of structure and behavior. Basic, Refactoring Techniques
Method signature: It consists of method name and parameter list (number/type/order of the parameters). methodName(parametersList y)
. An instance method in a subclass with the same signature
and return type
as an instance method in the super-class overrides the super-class's method.
Java OOP concepts
Class - Collection of a common features of a group of object [static/instance Fields, blocks and Methods]
Object - Instance of a class (instance fields)
Abstraction - Process of hiding complex info and providing required info like API, Marker Interfaces ...
Encapsulation(Security) - Class Binding up with data members(fields) and member functions.
Inheritance (Reusability by placing common code in single class)
1. Multilevel - {A -> B -> C} 2. Multiple - Diamond problem {A <- (B) -> C} [Java not supports] 3. Cyclic {A <-> B} [Java not supports]
* Is-A Relation - Class A extends B
* Hash-A Relation - Class A { B obj = new B(); } - (Composition/Aggregation)
Polymorphism (Flexibility) 1. Compile-Time Overloading 2. Runtime Overriding [Greek - "many forms"]
int[] arr = {1,2,3}; int arrLength = arr.length; // Fixed length of sequential blocks to hold same data type
String str = "Yash"; int strLength = str.length(); // Immutable Object value can't be changed.
List<?> collections = new ArrayList<String>(); int collectionGroupSize = collections.size();
Map<?, ?> mapEntry = new HashMap<String, String>();
Set<?> keySet = mapEntry.keySet(); // Set of Key's
Set<?> entrySet = mapEntry.entrySet(); // Set of Entries [Key, Value]
// Immutable Objects once created they can't be modified. final class Integer/String/Employee
Integer val = Integer.valueOf("100"); String str2 = String.valueOf(100); // Immutable classes
final class Employee { // All Wrapper classes, java.util.UUID, java.io.File ...
private final String empName; // Field as Final(values can be assigned only once) Only getter functions.
public Employee(String name) { this.empName = name; }
}
Native Java Code for Hashtable.h
, Hashtable.cpp
SQL API
.
You can check your current JDK and JRE versions on your command prompt respectively,
- JDK
javac -version [C:\Program Files\Java\jdk1.8.0_121\bin]
o/p:javac 1.8.0_121
- JRE
java -version
[C:\Program Files\Java\jdk1.8.0_121\bin]
o/P:java version "1.8.0_102"
JAVA_HOME - Must be set to JDK otherwise maven projects leads to compilation error. [ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
C:\Softwares\OpenJDK\
, 7-zip
Fatal error compiling: invalid target release: JRE and JDK must be of same version
1.8.0.XXX
Disable TLS 1.0 and 1.1
security-libs/javax.net.ssl
: TLS 1.0 and 1.1 are versions of the TLS protocol that are no longer considered secure and have been superseded by more secure and modern versions (TLS 1.2 and 1.3).
Core Java
-
Java Programming Language Basics
- Object, Class, Encapsulation, Interface, Inheritance, Polymorphism (Method Overloading, Overriding)
- JVM Architecture, Memory Areas
- JVM Class Loader SubSystem
- Core Java Interview Questions & Programs
- Interview Concepts
Stack Posts
- Comparable vs Comparator
- Collections and Arrays
-
String, StringBuffer, and StringBuilder
- String reverse
- Remove single char
- File data to String
- Unicode equality check Spacing entities
- split(String regex, int limit)
- Longest String of an array
-
Object Serialization
- Interface's Serializable vs Externalizable
- Transient Keyword
-
implements Runnable
vsextends Thread
- JSON
- Files,
Logging API
- Append text to Existing file
- Counting number of words in a file
- Properties
- Properties with reference key