Soluții

How to install Snapchat on Samsung Galaxy Watch 4 or 5

The Samsung Galaxy Watch 4 and Watch 5 run Google’s Wear OS, which opens many capabilities, including the option to download apps from Play Store directly on the watch. And, if an app isn’t available on the watch’s Play Store, you can sideload it, which we’ve used to install Snapchat on the Galaxy Watch 4.

Snapchat is not officially available for Galaxy Watch yet, but you can easily sideload the APK and use it on your Galaxy watch 4 and 5. And while it’s a little tough to operate on such a small circular screen, you can still use many features like watching snaps, friends list, messages, sharing snaps, etc.

[mai mult...]

How to Generate Random Number in Java

In Java programming, we often required to generate random numbers while we develop applications. Many applications have the feature to generate numbers randomly, such as to verify the user many applications use the OTP. The best example of random numbers is dice. Because when we throw it, we get a random number between 1 to 6.

In this section, we will learn what is a random number and how to generate random numbers in java

Random numbers are the numbers that use a large set of numbers and selects a number using the mathematical algorithm. It satisfies the following two conditions:

  • The generated values uniformly distributed over a definite interval.
  • It is impossible to guess the future value based on current and past values.
Generating Random Number in Java

In Java, there is three-way to generate random numbers using the method and classes.

  • Using the random() Method
  • Using the Random Class
  • Using the ThreadLocalRandom Class
  • Using the ints() Method (in Java 8)
Using the Math.random() Method

The Java Math class has many methods for different mathematical operations. One of them is the random() method. It is a static method of the Math class. We can invoke it directly. It generates only double type random number greater than or equal to 0.0 and less than 1.0. Before using the random() method, we must import the java.lang.Math class.

[mai mult...]

How to Compare Two Objects in Java

Java Object class is the super class of all the Java classes. All Java classes implements the Object class by default. The Java Object class provides the two important methods to compare two obiects in java, i.e. equals() and hashCode() method. In this section, we will learn how equals() and hashCode() method works. Along with this, we will also learn how to compare two objects in Java with proper examples.

Java provides the two methods of the Object class to compare the objects are as follows:

  • Java equals() Method
  • Java hashCode() Method
Java equals() Method

The equals() method of the Object class compare the equality of two objects. The two objects will be equal if they share the same memory address.

Syntax:

  1. public boolean equals(Object obj)

The method parses a reference object as a parameter. It returns true if the objects are equal, else returns false.

It is also possible that an object is equal to another given object, then the equals() method follow the equivalence relation to compare the objects.

    • Reflexive: If x is a non-null reference, the calling of x.equals(x) must return true.
    • Symmetric: If the two non-null references are x and y, x.equals(y) will return true if and only if y.equals(x) return true.
    • Transitive: If the three non-null references are x, y, and z, x.equals(z) will also return true if x.equals(y) and y.equals(z) both returns true.
    • Consistent: If the two non-null references are x and y, the multiple calling of x.equals(y) constantly returns either true or false. It does not provide any information used in the comparison.
    • For any non-null reference x, x.equals(null) returns false.

In short, for any non-null reference say x and y, it returns true if and only if both references refer to the same object.Remember: When we override the equals() method, it is necessary to override the hashCode() method. Overriding follow the convention for the hashCode() method that states, the equal object must have equal hash code.

Example of equals() method

In the following example, we have created constructor of the Double and Long class and passes corresponding values, as an argument that stored in their objects, respectively.

After that, in the first println statement, we have invoked equals() method and parse an object y as a parameter that compares the object x and y. It returns false because x holds the double value and y holds the long value that is not equal.

Similarly, in the second println statement, we have invoked equals() method and parse the same value as in the constructor of the Double class. It returns true because the object of double class i.e. x holds the same value as we have passed in the equals() method.

[mai mult...]

How to Create Object in Java

The object is a basic building block of an OOPS language. In Java, we cannot execute any program without creating an object. There is various way to create an obiect in java that we will discuss in this section, and also learn how to create an object in Java.

java provides five ways to create an object.

  • Using new Keyword
  • Using clone() method
  • Using newInstance() method of the Class class
  • Using newInstance() method of the Constructor class
  • Using Deserialization
Using new Keyword

Using the new keyword is the most popular way to create an object or instance of the class. When we create an instance of the class by using the new keyword, it allocates memory (heap) for the newly created object and also returns the reference of that object to that memory. The new keyword is also used to create an array. The syntax for creating an object is:

  • Using new Keyword
  • Using clone() method
  • Using newInstance() method of the Class class
  • Using newInstance() method of the Constructor class
  • Using Deserialization
[mai mult...]

How to Print ASCII Value in Java

ASCII acronym for American Standard Code for Information Interchange. It is a 7-bit character set contains 128 (0 to 127) characters. It represents the numerical value of a character. For example, the ASCII value of A is 65.

In this section, we will learn how to print ASCII value or code through a Java program.

There are two ways to print ASCII value in JAVA

  • Assigning a Variable to the int Variable
  • Using Type-Casting
Assigning a Variable to the int Variable

To print the ASCII value of a character, we need not use any method or class. Java internally converts the character value to an ASCII value. In the following program, we have assigned two characters a and b in the ch1 and ch2 variables, respectively. To find the ASCII value of a and b, we have assigned ch1 and ch2 variables to the integer variables asciivalue1 and asciivalue2, respectively. Finally, we have printed the variable asciivalue1 and asciivalue2 in which ASCII values of the characters are stored.

[mai mult...]