Found 2617 Articles for Java

Internal Working of HashMap in Java

AmitDiwan
Updated on 04-Jul-2020 08:36:59

1K+ Views

The function ‘hashCode’ is used to get the hash code of an object in Java. This is an object of super class Object. It returns the object reference’s memory as an integer. It is a native function, which means no direct method in Java can be used to fetch the reference of the object.For better performace of HashMap, use the hashCode() properly. Basically, this function is used to calculate the bucket and index values. It is defined in the following way −public native hashCode()Since we have mentioned ‘bucket’, it is important to understand what it means. It is an element ... Read More

Interesting facts about null in Java

AmitDiwan
Updated on 04-Jul-2020 08:34:37

113 Views

There are many facts associated with null in Java. We will discuss a few of them here with examples −The default value of any reference variable in Java is always null.Example Live Demopublic class Demo{    private static Object my_obj;    public static void main(String args[]){       System.out.println("The default value of object my_obj is : " + my_obj);    } }OutputThe default value of object my_obj is : nullA class named Demo defines a static object and the main function that shows the default value of this pre-defined object.The not equal to (!=) and comparison (==) operators can be ... Read More

Interesting Facts about Java

AmitDiwan
Updated on 04-Jul-2020 08:31:16

332 Views

Java was built by sheer accident, a team of developers were busy building a set top box, and began cleaning C++. When they were winding up these changes, they ended up discovering Java and its runtime environment.Many of you might be aware of this, but for those who aren’t, Java wasn’t the original name that was decided for this language. It was ‘Oak’. Sun Marketing system changed the name later when they realized that a company named ‘Oak’ existed.It is a widely used language all over the world, and is considered to be a favourite amongst the developer group, and ... Read More

Interesting facts about Increment and Decrement operators in Java

AmitDiwan
Updated on 04-Jul-2020 08:19:58

120 Views

There are many interesting facts with respect to increment and decrement operators in Java. We will discuss a few of them with examples −The increment and decrement operators can’t be used with the ‘final’ variables. This is due to the fact that variables associated with ‘final’ keyword can’t be changed −Example Live Demopublic class Demo{    public static void main(String[] args){       final int my_val = 34;       int my_val_2 = ++my_val;       System.out.println("The value is :");       System.out.println(my_val_2);    } }Output/Demo.java:6: error: cannot assign a value to final variable my_val int my_val_2 ... Read More

Interesting facts about Array assignment in Java

AmitDiwan
Updated on 04-Jul-2020 08:18:01

126 Views

There are many facts with respect to array assignment, and we will discuss a few of them with working examples here −While creating array object type, the element that would be present inside the array can be declared as type objects or as child class’s object.Example Live Demopublic class Demo{    public static void main(String[] args){       Number[] my_val = new Number[3];       my_val[0] = new Integer(91);       my_val[1] = new Double(65.963);       my_val[2] = new Double(45.7965);       System.out.println(my_val[0]);       System.out.println(my_val[1]);       System.out.println(my_val[2]);    } }Output91 65.963 ... Read More

Initialization of local variable in a conditional block in Java

AmitDiwan
Updated on 04-Jul-2020 08:14:59

301 Views

Java compiler doesn’t allow abandoning an uninitialized local variable. When a local variable is initialized inside a conditional block, there are 3 possibilities that could potentially occur −Code compiles successfully if values are provided in the conditional block and the given condition is true.Code gives a compilation error if variables are provided (instead of values) in the conditional block and the condition is true.Code gives compilation error if the condition that needs to be checked is false.If the local variable is initialized to a default value outside of the conditional block in the code, it won’t give any error and ... Read More

Importance of HashSet in Java

AmitDiwan
Updated on 23-Nov-2023 11:42:41

305 Views

The HashSet use Hashing to manipulate data. Let us see an example − Example import java.util.*; public class Demo{ private final String f_str, l_str; public Demo(String f_str, String l_str){ this.f_str = f_str; this.l_str = l_str; } public boolean equals(Object o){ if (o instanceof Demo) return true; Demo n = (Demo)o; return n.f_str.equals(f_str) && n.l_str.equals(l_str); } public static void main(String[] args){ ... Read More

Implementing Checksum Using Java

AmitDiwan
Updated on 04-Jul-2020 08:09:40

2K+ Views

Following is the code to implement Checksum using Java −Example Live Demoimport java.util.*; public class Demo{    public static void main(String args[]){       Scanner my_scan = new Scanner(System.in);       System.out.println("Enter the input string ");       String my_in = my_scan.next();       int my_checksum = generate_checksum(my_in);       System.out.println("The checksum that has been generated is " + Integer.toHexString(my_checksum));       System.out.println("Enter the data that needs to be sent to the receiver ");       my_in = my_scan.next();       System.out.println("Enter the checksum that needs to be sent to the receiver "); ... Read More

How to convert hex string to byte Array in Java?

sudhir sharma
Updated on 03-Jul-2020 08:12:11

5K+ Views

We can convert a hex string to byte array in Java by first converting the hexadecimal number to integer value using the parseInt() method of the Integer class in java. This will return an integer value which will be the decimal conversion of hexadecimal value. We will then use the toByteArray() method of BigInteger class that will return a byte array.Example Live Demoimport java.math.BigInteger; public class Demo {    public static void main(String args[]) {       String str = "1D08A";       int it = Integer.parseInt(str, 16);       System.out.println("Hexadecimal String " + str);       ... Read More

Count divisors of n that have at-least one digit common with n in Java

Sunidhi Bansal
Updated on 06-Jun-2020 12:18:23

144 Views

We are given with a number let’s say, num and the task is to calculate the divisor of a given number thereby count the divisors of num that have at least one digit common with n.Input − num = 24Output − Count is 4Explanation − we will perform the following steps −Firstly, calculate the divisors of a given numberDivisors of 24 are − 1, 2, 3, 4, 6, 8, 12, 24Secondly, check which divisor have at least one digit that matches with the digits of a number2, 4, 12, 24 are the divisors that contain the digit that matches with ... Read More

Advertisements