Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Java Packages and Classes
2.1.
Java Packages
2.2.
Java Classes
3.
Components of a Java class
3.1.
Creating a Java Class
4.
Java Long Class
4.1.
Long Class Declaration
4.2.
Long Class Basic Implementation
4.2.1.
Output
4.3.
Fields in Long Class
4.4.
4.5.
Constructors in Long Class
4.6.
Methods in Long Class
4.6.1.
1. static int bitCount (long I)
4.6.2.
2. byte byteValue()
4.6.3.
3. int compareTo(Long SecondLong)
4.6.4.
4. static Long decode(String stringname)
4.6.5.
5. double doubleValue()
4.6.6.
6. float floatValue()
4.6.7.
7. boolean equals(Object objectname)
4.6.8.
8. static Long getLong(String nm)
4.6.9.
9. static Long getLong(String nm, long val)
4.6.10.
10. static Long getLong(String nm, Long val)
4.6.11.
11. boolean equals(Object objectname)
4.6.12.
12. static long parseLong(String s)
4.6.13.
13. static long parseLong(String s, int radix)
4.6.14.
14. static long reverse(long I)
4.6.15.
15. static long reverseBytes(long I)
4.6.16.
16. static long rotateRight(long i, int distance)
4.6.17.
17. static long rotateLeft(long i, int distance)
4.6.18.
18. static long lowestOneBit(long I)
4.6.19.
19. int hashCode()
4.6.20.
20. int intValue()
4.6.21.
21. long longValue()
4.6.22.
22. short shortValue()
4.6.23.
23. static int signum(long I)
4.6.24.
24. static String toBinaryString(long I)
4.6.25.
25. String toOctalString(long I)
4.6.26.
26. static String toHexString(long I)
4.6.27.
27. String toString()
4.6.28.
28. static String toString(long I)
4.6.29.
29. static String toString(long i, int radix
4.6.30.
30. static Long valueOf(long l)
4.6.31.
31. static Long valueOf(String s)
4.6.32.
32. static Long valueOf(String s, int radix)
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

Java Long Class

Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM

Introduction

Java is an object-oriented programming language as it focuses on real-life objects. In Java, everything is linked to classes and objects and their attributes and methods. You must have heard the names of various classes like Hashmap, String, etc. Similarly, “Long” is a wrapper class in Java. In simple words, it contains the primitive data type “long” in its object. 

We will learn the basics of Java classes and about Java Long class. Before learning the Java Long class, we will learn a few basic concepts on Java classes and packages. I hope you will understand the Java Long class at the end of this post.

Also see,  Swap Function in Java

Java Packages and Classes

Have you ever heard your friend using the terms “Java Packages” and “Classes”? What exactly do these terms mean? Before moving on further, let’s clear your basic understanding of these terms.

Java Packages

In Java, a package is used to group related classes. Consider it a folder in a file system. Packages help us avoid name clashes and write more maintainable code. We can use existing packages' classes as often as we need them in our program. To use a package, we must create an import class from existing packages and use it in our code.

Two types of packages are available:

  • Built-in Packages (Packages that are already present or installed)
  • User-Defined Packages (Packages created by the user)

The following are a few examples of the built-in Java packages:

  • java.lang (contains are the most basic classes)
  • java.io (contains classes for input and output functions)

Java Classes

We can define a class as a blueprint or prototype defined by the user and used to create objects. It denotes the set of properties or methods that all objects of a similar type share. 

A class in java can have:

  • Fields
  • Methods
  • Constructors
  • Blocks
  • Nested class and interface

 

Must Read Static Blocks In Java and Hashcode Method in Java

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Components of a Java class

The diagram below shows the components present in a Java class.

Creating a Java Class

We can use the class keyword in Java to construct a class. As an example,

class xyzclass {
  // fields, constructors, blocks, etc.
  // methods
}

Java Long Class

Now that we have learned about packages and classes in Java, we can now move on to the Java Long class. The Java Long class is a part of the wrapper class, which means this type of class can wrap or contain primitive data types in its object.

So, what are the advantages of wrapping a primitive data type into an object? Basically, we will be able to access all of the methods accessible in the Long class, which is quite handy.

The Java.Lang.Long class wraps a value of the primitive type long in an object. We can store a single long value in a Long object. 

Long Class Declaration

Here, we will see how a Long class is implemented in Java. Below is the sample Long class declaration code:

public final class Long extends Number
      implements Comparable<Long>

Long Class Basic Implementation

You must be curious to learn how we can implement our first Long class implementation in Java. So, here we provide below the basic implementation of the use of the Long class.

//java lang Long class implementation
import java.lang.Long;
public class LongClassDeclaration {
    public static void main(String[] args) {
        Long l = new Long(1234567890123456789L);
        System.out.println(l);
    }
}

Output

Practice by yourself on online java compiler.

Fields in Long Class

Let’s understand what we mean by a field of a Java class. A Java field is a data container that holds data. It gives dynamic access to and information about a single field of a class or interface. In simple words, a field acts as a class's descriptive properties.

The fields for java.lang.long class, along with their descriptions, are listed below:

Constructors in Long Class

In Java, there are two types of class constructors in Long class. One constructor uses a long value as a parameter, and the other uses a string.

  1. Long (long value): This creates a new Long object corresponding to the supplied long parameter.
  2. Long (String s): This creates a new Long object that corresponds to the long value specified by the String parameter.

Methods in Long Class

Now we will learn different available methods in the Long class. The Long class contains a lot of methods that are used to manipulate Java objects. This includes creating, reading, writing, and deleting them. 

Various methods in the Java Long class, along with their descriptions, are listed below:

1. static int bitCount (long I)

This method returns the number of one-bits in the two's complement binary representation of the provided long value.

2. byte byteValue()

After the conversion, it returns the value of Long as a byte.

3. int compareTo(Long SecondLong)

This method numerically compares two Long objects.

4. static Long decode(String stringname)

This method converts a String to a long value.

5. double doubleValue()

The value of Long is returned as a double.

6. float floatValue()

This method returns the float value of the Long.

7. boolean equals(Object objectname)

This method compares this object to the object passed in as a parameter.

8. static Long getLong(String nm)

The long value of the system property with the supplied name is determined by this method.

9. static Long getLong(String nm, long val)

This method determines the long value of the provided system attribute.

10. static Long getLong(String nm, Long val)

This method returns the long value of the specified system attribute.

11. boolean equals(Object objectname)

The long value of the system property with the supplied name is returned by this method.

12. static long parseLong(String s)

The string parameter is parsed into a signed decimal long by this method.

13. static long parseLong(String s, int radix)

The string input is parsed into a signed long in the radix indicated by the second parameter by this method.

14. static long reverse(long I)

This function returns the value obtained by reversing the order of the bits in the supplied long value's two's complement binary representation.

15. static long reverseBytes(long I)

This function returns the result of reversing the order of bytes in the two's complement representation of a long value.

16. static long rotateRight(long i, int distance)

This function returns the result of rotating the two's complement binary representation of the provided long value right by the number of bits specified.

17. static long rotateLeft(long i, int distance)

This function returns the result of rotating the two's complement binary representation of the provided long value left by the number of bits specified.

18. static long lowestOneBit(long I)

This method returns a single-bit long value in the lowest-order ("rightmost") one-bit place in the provided long value.

19. int hashCode()

This function returns the Long object's hash code.

20. int intValue()

This method returns Long's value as an int.

21. long longValue()

This method returns a long value as the value of this provided Long.

22. short shortValue()

This method returns a short value for the value of this provided Long.

23. static int signum(long I)

This method returns the signum function of the long value supplied. In simple words, the signum function returns the sign for the given x value. It can be +1 or -1.

24. static String toBinaryString(long I)

This method returns the long input as a string representation of an unsigned integer in base 2.

25. String toOctalString(long I)

This method returns an unsigned integer in base 8 as a string representation of the long parameter.

26. static String toHexString(long I)

This method returns an unsigned integer in base 16 as a string representation of the long parameter.

27. String toString()

This function returns a String object that represents the value of this Long.

28. static String toString(long I)

This function returns a String object with the supplied long as its value.

29. static String toString(long i, int radix

This method returns the first argument as a string in the radix given by the second parameter.

30. static Long valueOf(long l)

The provided long value is represented by a Long object which is returned by this method.

31. static Long valueOf(String s)

The given String value is returned by this method as a Long object.

32. static Long valueOf(String s, int radix)

When processed using the radix provided by the second parameter, it returns a Long object that contains the value of the specified string

FAQs

  1. What is the difference between long and int?
    Programming languages like Java offer both int and long as predefined data types. The main difference is that integers are 32 bits wide, whereas longs are 64 bits wide. 
  2. What is the difference between “Long” and “long” in Java?
    A Long is a reference type or a class defined in the standard library. It keeps track of a reference to an object with a value. On the other hand, a long is a basic data type that is built into the language.
  3. Can a long value be null in Java?
    Only Object data types are allowed to be null. Integers, longs, and other types of data can't be null. Yes, you may check for null values if the longValue variable is of type Long. Note: “Long” is the wrapper class, not the basic data type “long.”
  4. What are precisely the maximum and minimum value of long data type?
    The long data type is nothing but a signed two's complement integer of 64 bits. 
    →Minimum Value of long data type: -9,223,372,036,854,775,808 
    →Maximum Value of long data type: 9,223,372,036,854,775,807

Key Takeaways

In this post, we discussed the basics of Java Classes and Java Packages. Then we discussed Java Long Class and its implementation. Different fieldsmethods and constructors of the Java long class have been extensively discussed.

We hope that this article has improved your understanding of Java Long Class. To learn more, check out our articles on ‘print characters in a string’ and ‘selenium interview questions.’ Please do upvote this blog to help other ninjas grow.

Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!

Happy Reading!

Previous article
Java Integer Class
Next article
Java Float Class
Live masterclass